2.12.10. Non-standard cron tasks
The cron schedule allows to start tasks only with accuracy to the minute, accuracy to the second is not supported. Also, tasks cannot be started one by one, when the next task is started after the previous one is finished. You can use Bash useful features to implement such scripts.
Sequential launch of commands
To run multiple commands sequentially, you must specify them in one task with ;:
command1 ; command2 ; ... ; commandn
Execution of the next command will occur anyway, even if the previous command returned an error.
If you need to check for an error or successful execution of a previous command, use delimiters:
&&— next command will be executed only if the previous command is successfully executed.||— next command will be executed only if the previous command was not executed successfully and returned an error.
Example:
command1 ; command2 && command3 || command4
Execution order: command1 and command2 will be executed in any case, but command3 will be executed only if command2 was executed without errors. command4 will be executed only if command3 was executed with an error.
Launch with split-second accuracy
Important points:
- Cron tasks are not started at the very beginning of a minute. Often a task can be started in the first seconds of a minute or later, which makes it difficult to specify the exact start time.
- It is not recommended to set too large a value for wait, as the command execution time is limited.
- It is not recommended to launch tasks with too small an interval, as this may lead to excessive consumption of server resources.
As mentioned above, cron doesn't have the ability to customize execution to the exact second, so you need to use various tricks, such as sequentially running with wait commands:
sleep 30 ; command1
The second command will be run after 30 seconds.
If you want to launch two tasks at the beginning of a minute and at 30 seconds of the same minute, then add two tasks with the same interval, but specify one of them as sleep 30 ; your_command. In this case, one task will launch at the beginning of the minute, and the second one will launch 30 seconds later.
Send requests without using DNS
Important points:
- You need to know the IP address of the server with the target site in advance.
- SSL issues may occur if the target IP does not match the domain specified in the certificate.
This method is used when you need to access a site directly by its IP address, without using DNS to resolve the target site's domain name to its IP address. This can be useful, for example, to bypass Cloudflare protection, test a connection to a specific server, or check your hosting configuration.
wget
The wget utility uses DNS by default. However, if you specify the Host header manually using the –header parameter, you can send the HTTP request directly to the IP address.
Attention!
This method is only applicable for HTTP requests. When using HTTPS, SSL certificate verification errors will occur because the IP does not match the domain specified in the certificate.Command example:
/usr/bin/wget -t 1 -O --header='Host: example.com' http://12.34.56.78/path/to/script.php
example.com— target domain.12.34.56.78— server IP address./path/to/script.php— path from the URL where the request should be made.
cURL
The cURL utility provides more flexible options for working with IP addresses without using DNS, including full support for HTTPS. With the –resolve parameter, you can explicitly specify which IP address to use for a given domain and port.
Command example:
/usr/bin/curl --resolve example.com:443:12.34.56.78 https://example.com/path/to/script.php
example.com— target domain (used in headers and when checking the certificate).443— port (for HTTPS). For HTTP, use port80andhttp://scheme.12.34.56.78— server IP address./path/to/script.php— path from the URL where the request should be made.
You can also use a simpler method — specify the header manually, as in wget, but it also does not work with HTTPS:
curl -H 'Host: example.com' http://12.34.56.78/path/to/script.php