Passing parameter in crontab through shell to PHP script

✔ Recommended Answer

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

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



Answered By: Stuart

Method #2

To pass parameters from crontab to a PHP script, you can use command line arguments. Here's an example of how to do it:

  1. Edit the crontab file by running the following command:
crontab -e
  1. Add a new line to the crontab file with the following format:
* * * * * /path/to/php /path/to/script.php argument1 argument2

This will run the PHP script every minute and pass two arguments (argument1 and argument2) to the script.

  1. In your PHP script, you can retrieve the command line arguments using the $argv and $argc variables. For example:
<?php // Get the number of arguments passed $argc = $_SERVER['argc']; // Get the arguments themselves $argv = $_SERVER['argv']; // Use the arguments in your script $arg1 = $argv[1]; $arg2 = $argv[2]; // ... rest of your code ?>

Note that $argv[0] contains the name of the script itself.


Most Popular

Remove Unicode Zero Width Space PHP

PhpStorm, return value is expected to be 'A', 'object' returned

Laravel file upload returns forbidden 403, file permission is 700 not 755