Pass Parameters In Cron URL - PHP

Posted October 19, 2015 23.3k views

How to pass parameters to file path? This is what I’m trying to do:

*/2 * * * * /usr/bin/php /var/www/mysite/public_html/cron/cron.php?group=3 >> /var/www/mysite/public_html/cron/cron.log 2>&1

I also tried without the question mark ’?’ but the cron does not run.

*/2 * * * * /usr/bin/php /var/www/mysite/public_html/cron/cron.php?group=3 >> /var/www/mysite/public_html/cron/cron.log 2>&1

To to pass the parameter?


These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
2 answers

Generally you would not use this method to pass parameters to a CLI PHP script. Instead you would do something like:

*/2 * * * * /usr/bin/php /var/www/mysite/public_html/cron/cron.php 3 >> /var/www/mysite/public_html/cron/cron.log 2>&1

Using $argv[0] to get the value inside your script.

  • Actually $argv[0] returns path to the script, passed arguments are available at $argv[1], $argv[2], $argv[3] and so on.

    php /home/user/script.php some_argument secret_key other_argument
    echo $argv[0]; // /home/user/script.php
    echo $argv[1]; // some_argument
    echo $argv[2]; // secret_key
    echo $argv[3]; // other_argument

As the file is inside the public_html directory you can also use curl to issue a HTTP request via URL:

*/2 * * * * curl -s >> /var/www/mysite/public_html/cron/cron.log 2>&1