I want to track each crontab output to separated file. Normally, I use this as crontab entry:

1  *    * * *   root    php /mix/dostuff.php > /tmp/dostuff.txt

Now, I want to make it to write instead of /tmp/dostuff.txt, to /tmp/dostuff-YYYY-MM-DD-HH-II-SS.txt

I could get the date part, using this:

TIMESTAMP=`date +%Y-%m-%H\ %k:%M:%S`

Now, how do I stick this TIMESTAMP into output file?

4 Answers 4


crontabs are run in a different environment than the default shell, so you can use a small shell script under your home directory with something like this in it (we'll name it script.sh):

php /mix/dostuff.php > /tmp/dostuff-`date +%Y-%m-%H\ %k:%M:%S`.txt

then change this scripts permissions to ensure it is executable, 755 should be fine. Then in your crontab:

1  *    * * *   root    /home/yourusername/script.sh
  • 1
    Beware %Y-%m-%H isn't the usual year-month-date combination - try %Y-%m-%d !
    – shuckc
    Aug 1, 2014 at 6:03
1  *    * * *   root    php /mix/dostuff.php > "/tmp/dostuff-`date +\%Y-\%m-\%H\ \%k:\%M:\%S`.txt"

Simple as that.

  • wont work, crontabs aren't evaluated in the same environment as a default shell.
    – John T
    Sep 9, 2009 at 10:54
  • 2
    I don't see how this would be a problem here. The cron environment is sufficient for this command. Besides, it works, I just tested it myself. Sep 9, 2009 at 11:02
  • cron parses the file, not /bin/bash, thats why.
    – John T
    Sep 9, 2009 at 11:07
  • 3
    crontab(5): "The ``sixth'' field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the crontab file." You can even write entire shell scripts in a crontab line if you want! Sep 9, 2009 at 11:12
  • 1
    the space between date and + is very important, just for the next guy who comes along
    – jcollum
    Mar 15, 2012 at 15:27

On CentOS 6.6, I use

*/10 * * * * /usr/bin/php -q /var/www/script.php > cron-job-log-$(date +\%Y\%m\%d\%H\%M\%S).txt

it will create a file every 10 min


Try it like following one. It will work perfectly.

1 * * * * root /home/yourusername/script.sh > /var/log/foldername/output_filename_$(date +\%Y-\%m-\H%).log

By before create backup folder into the log path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.