2.12.7. Passing parameters to script being launched

When launching a script with predefined variable values, it is more convenient to pass them in the task launch string so that you do not have to make changes to the script itself each time.

To do this, specify the cron task as follows:

path_to_interpreter -f path_to_script variables_separated_by_space

Example:

/usr/local/bin/php -f /home/example/path/to/script.php 15 test 23

At the beginning of the script (from the second line), you need to add the assignment of values to the necessary variables through the variable $argv: $argv[1] is the first parameter, $argv[2] is the second, and so on.

Example:

$a=$argv[1];
$some_text=$argv[2];
$age=$argv[3];

In this example, after these lines, the variable $a inside the script will be equal to 15, the variable $some_text will be equal to test, and the variable $age will be equal to 23.

If you need to use settings of a specific host (site), you can use the following variant:

path_to_interpreter -c path_to_host_ini_file -f path_to_script variables_separated_by_space

Example:

/usr/local/bin/php -c /home/example/.system/php/www.example.com.ini -f /home/example/path/to/script.php 15 test 23
Contente