Mark Mark - 5 months ago 48
Bash Question

Passing parameter in crontab through shell to php script

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

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):

echo 'Arg 1: ' . $argv[1] ."\n";
echo 'Arg 2: ' . $argv[2] ."\n";

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

./ 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/ one two

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


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

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