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?

Erol Guzoğlu
  • 121
  • 1
  • 4

1 Answers1

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
glenn jackman
  • 85,964