2.11.2. Adding a cron task

Important points:

  • cron does not use a variable PATH, therefore, specify full paths to files not only when adding a task, but also in the scripts being launched.
  • If the task is triggered exclusively when the page is opened in a browser, start it with wget.
  • If the problem uses the symbol $ as a simple character (not a special character), add a backslash before it \.
  • The task length must not exceed 255 characters. To run long commands, put them in a Bash script and run it via cron.
When adding a task, all double quotes in the command are automatically escaped with the character \... This does not affect the work of the team and is not visible in list of added tasks, but it will be visible in the console during the test launch of the task and in the subject of the email with the report when it is launched according to the schedule.
  1. Open the section "Schedule tasks (cron)".
  2. Clickon "Add task" at the top of the page.
  3. Specify the required settings and click "Save":
    • "Task" - specify the command to be executed according to the schedule. It is recommended to run it first manuallyto make sure it works exactly as intended.
      When specifying a task:
      • As absolute file paths:
        • For files with the extension .php and without extension:
          • If the script is located in the site directory — after saving, the path to the interpreter will be automatically added to the beginning of the command established on the site's PHP version and the path to the PHP config file for that site. For example, the task /home/example/example.com/www/script.php with the added site www.example.com with PHP 8.1 installed it will turn into /usr/local/php81/bin/php -c /home/example/.system/php/www.example.com /home/example/example.com/www/script.php.
          • If the script is outside of site directories - after saving, the path to the standard interpreter will be automatically added to the beginning of the command. For example, the task /home/example/script.php will turn into /usr/local/bin/php /home/example/script.php.
        • For files with the extension .sh - add the path to the Bash interpreter /bin/sh.
        • For files with the extension .pl — the path to the Perl interpreter is added /usr/bin/perl.
        • For files with the extension .sql - the path to the mysqldump utility is added /usr/local/mysql/bin/mysqldump.
      • As URL — a standard wget command of the form is formed /usr/bin/wget -O - -q -t 1 'specified_URL'.

      In all other cases, the task is added in the form in which it was specified.

    • "Simultaneous launch" - select the behavior for the situation when a previously started process does not have time to complete before starting a new one.
    • "Launch time" - specify the time when the task should start. Use one of the ready-made presets or specify your version. Note In the selection and verification of your option will help "Help with setup"which opens our cron calculator, or use third-party services like crontab guru.
    • "Comment" - if necessary, provide an arbitrary comment to the task. Used for convenience. Comments can be useful when there are a lot of added tasks.
    • "Send report on email" — if you want to receive cron task execution reports to the mail, specify the mailbox to which they need to be sent, if the reports are not needed, leave the field blank.
  4. Wait approximately 30-60 minutes for the changes to take effect.

To specify when to start Cron tasks, a specific syntax is used for each value of the specified time. Syntax elements can be combined with each other by separating them with a comma, for example: */30, 1-10, 53 — the task will be launched at: 00, 01 and all numbers up to 10, 30, 53.

  • */X — launching the task at a time multiple of the specified in X. For example, an indication */20 for seconds and minutes will run the task in the following values: 00, 20, 40.
  • 1,2,3 — enumeration of the required values separated by commas.
  • 1-10 — an indication of the interval of the desired values, in the example all numbers from 1 to 10 will be used.
  • * — any value.

All added tasks are displayed in the list:

  • "Launch time" - the task launch schedule in the standard cron format.
  • "Next launch" - date and time of the next scheduled task triggering. Attention! The launch at the specified time may not take place if less than 30-60 minutes have passed since the task was added or changed.
  • "Task" — the full text of the command, a comment to the task and the launch schedule in a human—readable form.
  • "Report" - mail address where they are sent task progress reports, or a dash.
  • "Control" - task control buttons:
    • "Suspend task execution" or "Restore task execution" — stop or resume the launch of a scheduled task.
    • "Test run of the task" — starting the task manually. It can be used to check the operation of a command or launch it ahead of schedule before the scheduled time. After launching, a console window will appear on the page, where the output of the command in the process of work and the result of its execution will be displayed in real time.
    • "Edit" - changing task settings.
    • "Delete" - deleting a task.
The video shows the old form of adding a task, but the general principles of cron operation have remained unchanged.