How can I run a cron job with arguments and pass results to a log?

49,819

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.

Share:
49,819
Ryan
Author by

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, 2020

Comments

  • Ryan
    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
    Ryan over 12 years
    Sorry, 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
    mik over 12 years
    Now the command looks ok, may be there is a write permissions problem ? Also, try the cd /path/to/script , it might help.
  • Keith Thompson
    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 containing script.php and then invoke the script. The /path/to/script.php is not literal; it represents the name of whatever directory contains the script.