Search code examples
phpcronscheduled-tasksplesk

How to get arguments from PLESK Cron jobs


I have created a Cron Job/Scheduled Task in PLESK 12 which I am passing the arguments 33 and On through using the arguments box. I am struggling to pick these up in the PHP document on the end of the cron job.

PLESK and Arguments

In the PHP document I have tried a number of things including $arg[0] and $argv[0]

$arg returned as being an undefined variable whilst $argv[0] does not error but also does not pass the arguments through successfully as the desired changed has not been made.

I have checked to ensure the PHP script is working and it works fine when the arguments are hard coded into the program but I want this to be dynamic.

<?PHP
include_once('xxx/xxx/xxx/db.php');
include('xxx/xxx/xxx/xxx/db.php');
$query = "UPDATE SQLCommand SET argument1 = '$argv[1]' WHERE argument2= $argv[0]";
$result = mysqli_query($connection,$query);

Can anyone explain why these are still not passing the arguments through.

Thanks


Solution

  • $argv[0] always contains the name of the script file, as it passed to the PHP binary. As per screenshot, $argv[1] is '33' and $argv[2] is 'On'. You can easily check with:

    echo $argv[1];
    

    Or you can list all arguments as an array by:

    var_dump($argv);
    

    Basically, the following task is added to crontab, when scheduled via Plesk:

    /usr/bin/php5 -f '/test.php' -- '33' 'On'
    

    If test.php contains mentioned commands, the result of its' execution will be the following:

    # cat /test.php
    <?php
    echo "The first argument is $argv[1]\n";
    echo "Here the full list of arguments:\n";
    var_dump($argv);
    ?>
    # /usr/bin/php5 -f '/test.php' -- '33' 'On'
    The first argument is 33
    Here the full list of arguments:
    array(3) {
      [0]=>
      string(6) "/test.php"
      [1]=>
      string(2) "33"
      [2]=>
      string(2) "On"
    }