3.2.7 CRON – Execution of script under schedule

TheHost CRON Banner

CRON is an important tool that allows the server to execute specific tasks or requests on a predefined schedule.

You can trigger the execution of your desired script using the CRON scheduler in two ways: by emulating the loading of a specific URL of your website, which leads to the execution of your script, and by directly running the script without accessing web services via the HTTP/HTTPS protocol. Below, we will explore both methods.

Emulating Website URL Load

This method is straightforward and initiates the script within the normal functioning environment of your website on the hosting server. The significant advantage of this method is the ability to pass additional GET parameters to your script, such as launch keys, identifiers, tokens, etc. The downside is that the script is accessed through an HTTP request to a web service, imposing additional limitations on the request execution time.

In ISPManager’s control panel under Tools - Scheduler (cron), add a task in the following format::

  1. wget -O /dev/null "http://your_domain/your_script?param1=data&param2=data"

Warning: Use double quotes for the URL of your CRON script; otherwise, not all parameters of the HTTP GET requests will be transmitted.

Example: CRON task when the website operates over HTTP

In this example, the cron.php script will be called using the PHP interpreter installed for the website.com site. The script will receive the HTTP GET parameter action with the value perform.

  1. wget -O /dev/null "http://website.com/cron/cron.php?action=perform"
Example: CRON task for a website operating over HTTPS

If your website operates using the secure HTTPS protocol, it is highly recommended to use the --no-check-certificate parameter with wget to disable SSL certificate verification.

  1. wget -O /dev/null --no-check-certificate "https://website.com/cron/cron.php?action=perform"

Direct CRON Call to Script

The described method runs your desired script through the direct execution of the interpreter, eliminating any interaction with web services. The strength of this method lies in the absence of any intermediary web services during execution. However, a drawback is the need to explicitly specify the interpreter for running a particular script. For PHP scripts, it also requires specifying the interpreter version, such as PHP 7.1 or 8.0. Another limitation is the inability to pass POST/GET parameters. The only information that can be passed is through ENV variables, as explained below.

To utilize this method, in ISPManager’s control panel under Tools - Scheduler (cron), add a task in the following format:

  1. php-версия /var/www/your_username/data/www/domain_name/file_path
Example: CRON task with direct invocation of a PHP script

In this example, the cron.php script will be executed using PHP version 7.4.

  1. php-7.4 /var/www/superuser/data/www/website.com/cron/cron.php
Example: CRON task passing an ENV parameter to the PHP script

As mentioned earlier, with this method of execution, the only way to pass an additional parameter is by using ENV variables in the following format:

  1. VARIABLE=value php-версия /var/www/your_username/data/www/domain_name/file_path

In this example, the cron.php script receives the ENV parameter MODE with the value production.

  1. MODE=production php-8.0 /var/www/superuser/data/www/website.com/cron/cron.php

CRON Task Execution Syntax

The basic syntax can be explained using the following example:

* * * * * command
- - - - -
| | | | |
| | | | +-- Day of the week (0 - 7, where 0 and 7 are Sunday)
| | | +---- Month (1 - 12)
| | +------ Day of the month (1 - 31)
| +-------- Hour (0 - 23)
+---------- Minute (0 - 59)

For instance, a schedule like 0 2 * * * command will execute daily at 2:00 AM.

CRON syntax includes additional arguments for time configuration apart from simple *:

Additionaly: CRON task syntax

Comma , — to specify two or more options for executing the same command at different times. For example, 0 1,3,5 * * * command - the command will execute at 1 AM, 3 AM, and 5 AM.

Hyphen - — to define a range of time for executing a command. For example, 1-5 * * * * command will execute from the 1st to the 5th minute of every hour.

Slash / — to create specific time intervals within a given range. For example, */15 * * * * command will execute every 15 minutes.

Last L — for specific purposes, to determine the last day of the week in the current month. For example, 0 12 * * 3L command will execute on the last Wednesday of the month at 12:00 PM.

Workday W — in the day and month tabs, this symbol can be used to determine the nearest workday of the week to the specified time. For example, 0 9 * * 1W command will execute at 9:00 AM on every workday of the month, starting from the nearest to the 1st.

Pound # — to specify a specific weekday within the month, allowing you to select a number from 1 to 5. For example, 0 12 * * 2#1 command will execute on the first Tuesday of the month at 12:00 PM.

Question mark ? — to select a specific day to run the script, either the day of the month or the day number, we cannot specify both values simultaneously. The question mark allows leaving one value empty. For example, 0 3 15 ? * MON-FRI will execute at 3:00 AM on the 15th day of every month, if it’s not a weekend (Monday to Friday).

Warning: on our hosting, creating tasks with intervals shorter than every 5 minutes is prohibited. CRON tasks with such frequency will not execute correctly. In practice, this means you should avoid using * in the “minutes” option. Using */4 or less will result in the same outcome.

Displaying Added CRON Task

After adding a CRON scheduler task, it should appear in the list of tasks. To prevent users from receiving a large number of irrelevant messages related to CRON execution, >/dev/null 2>&1 will be automatically added to each task. Having this construction at the end of the command is normal behavior for the hosting control panel and CRON scheduler.