2

I try to run git pull command with loging output message to file every 5 minutes.

But i got error like this,

(root) CMD (cd /var/www/sites/ && git pull origin master | sed -e "s/^/$(date +\")
(CRON) error (grandchild #1111 failed with exit status 2)

My cronjob command as follows.

*/5 * * * * cd /var/www/site/ && git pull origin master | sed -e "s/^/$(date +\"%d-%m-%y\ %T\"), /" >> /var/log/crond/site.log

How can fix it?

  • Do you know what's causing the error? – amphetamachine Jan 7 '15 at 22:29
  • I'm not sure but i think pipe operator cause the error. – Erol Guzoğlu Jan 7 '15 at 22:38
  • It's the unescaped % probably. – glenn jackman Jan 7 '15 at 22:40
  • 1
    This is not a duplicate of unix.stackexchange.com/questions/29578/… . That question is about how to add a date to the output file. This one is about how to insert a date into that file without changing the name. – Simon Woodside Nov 10 '16 at 4:07
6

Note, for some systems a % is special in a crontab: from crontab(5) on my system

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 a "%" character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile. A "%" character in the command, unless escaped with a backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.

I strongly recommend using the date format %Y-%m-%d -- not only is it a standard, it sorts the same lexically and chronologically. Some strftime implementations have a shorthand for it: %F

See if your system has a ts command

cd /dir && git pull origin master 2>&1 | ts "\%F \%T" >> /var/log/crond/site.log

To get "real-time" timestamps, you might have to unbuffer the command: (starting to get really ugly)

cd /dir && stdbuf -oL sh -c 'git pull origin master 2>&1' | ts "\%F \%T" >> /var/log/crond/site.log
  • yes, it works on the command line, but cron treats % specially – glenn jackman Jan 7 '15 at 23:52
  • Now, i understood correctly. :) And i fixed my problem. Thanks @glenn :) – Erol Guzoğlu Jan 7 '15 at 23:54
  • fixed my cronjob timestamp issue * * * * * www-data sh -c 'php /var/www/example.com/current/artisan schedule:run' | ts "\%F \%T" >> /var/log/laravel_scheduler.log 2>&1 – James M Jun 24 at 15:21

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