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.
See http://php.net/manual/en/function.getopt.php
Source: stackoverflow.com
Answered By: Stuart
To pass parameters from crontab to a PHP script, you can use command line arguments. Here's an example of how to do it:
- Edit the crontab file by running the following command:
crontab -e
- Add a new line to the crontab file with the following format:
ruby* * * * * /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.
- In your PHP script, you can retrieve the command line arguments using the
$argv
and$argc
variables. For example:
php<?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.
Comments
Post a Comment