Bug #32859
closedforeman 2.5 /etc/cron.d/foreman broken
Description
See also here: here
The foreman crontab in /etc/cron.d/foreman changed to include a timestamp to the output, e.g.
*/30 * * * * foreman /usr/sbin/foreman-rake ldap:refresh_usergroups | gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }' >>/var/log/foreman/cron.log 2>&1
instead of
*/30 * * * * foreman /usr/sbin/foreman-rake ldap:refresh_usergroups >>/var/log/foreman/cron.log 2>&1
However, there are two problems here:
1. The percent sign % has special meaning in a crontab file and is replaced with a newline and the remainder is sent as standard input.
From 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 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.
2. The redirection only applies to the second command not the pipe as a whole. This means that standard error output of the rake command now is sent to standard error and it not redirected. It ends up in a cron mail instead of cron.log.
Correct would be:
*/30 * * * * foreman /usr/sbin/foreman-rake ldap:refresh_usergroups 2>&1 | gawk '{ print strftime("[\%Y-\%m-\%d \%H:\%M:\%S]"), $0 }' >>/var/log/foreman/cron.log 2>&1
or
*/30 * * * * foreman /usr/sbin/foreman-rake ldap:refresh_usergroups |& gawk '{ print strftime("[\%Y-\%m-\%d \%H:\%M:\%S]"), $0 }' >>/var/log/foreman/cron.log 2>&1