How can I run a cron job with arguments and pass results to a log?
Solution 1
It looks like you are searching for the log file in the wrong folder. Try this
* * * * * cd /path/to/script.php ; ./script.php arg1 arg2 >> logfile.log
Then look for your log file in the /path/to/script folder. It can also be a write permission problem. Also, check your script for errors. Your crontab command seems OK.
Solution 2
Does the command work correctly from the command line? It may be possible that the arguments contain some characters treated specially by shell. In that case you need to quote the whole command to prevent shell from tinkering with the arguments.
Ryan
BY DAY: Ninja. BY NIGHT: Ninja. FOR FUN: "If you see scary things, look for the helpers-you'll always see people helping."-Fred Rogers
Updated on July 02, 2020Comments
-
Ryan almost 4 years
Example:
* * * * * /usr/bin/php /full/path/to/script.php arg1 arg2 > /full/path/to/logfile.log
The script runs and accesses the arguments just fine, but the results are never printed to the logfile.log. Also, my logfile.log is chmod 777, so I know it has write access.
Can you fix my syntax?
-
Ryan over 12 yearsSorry, my question wasn't entirely clear. I've been using root path the whole time. I just updated question to clarify. So you're saying that this is the correct format though, right?
-
mik over 12 yearsNow the command looks ok, may be there is a write permissions problem ? Also, try the cd /path/to/script , it might help.
-
Keith Thompson about 8 years@rcode74: Your edit broke the command. I'm going to roll it back. It needs to
cd
to the directory containingscript.php
and then invoke the script. The/path/to/script.php
is not literal; it represents the name of whatever directory contains the script.