Search code examples
phpshellcroncron-task

Passing parameter in crontab through shell to php script


From my shell script test.sh I would like to pass some parameters to PHP script that would read them like:

test.sh

php -q /home/user/files/test.php "$1" "$2"
php -q /home/user/files/test.php $1 $2

To test the passing of arguments I just read them like this (test.php):

<?php
   echo 'Arg 1: ' . $argv[1] ."\n";
   echo 'Arg 2: ' . $argv[2] ."\n";
?>

Problem is, that if I run test.sh from shell with command:

./test.sh one two

my php script reads both parameters just fine (both times):

Arg 1: one
Arg 2: two
Arg 1: one
Arg 2: two

But if I run it via crontab it can't read parameters and I get only:

Arg 1: 
Arg 2: 
Arg 1: 
Arg 2: 

Cron job looks like this:

20  13  *   *   *   /home/user/files/test.sh one two

How can I correctly pass arguments via a cron job to the shell script and thereafter to php?


Solution

  • You may not have register_argc_argv enabled in your php.ini.

    Use php's getopt to get the command line arguments.

    See http://php.net/manual/en/function.getopt.php