Why Percent Signs (%) Do Not Work in Crontab

Percent sign % not working in crontab

% is a special character for crontab. From man 5 crontab:

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
.

So you need to escape the % character:

curl -w "%{time_total}\n" -o /dev/null -s http://myurl.com >> ~/log

to

curl -w "\%{time_total}\n" -o /dev/null -s http://myurl.com >> ~/log
^

How is % special in crontab?

The actual problem of your crontab line is not the $() or the backquotes. The problem is the percent sign %. It has a special meaning in crontabs.

From the manpage:

...
Percent-signs (%) in the command, unless escaped with backslash (\),
will be changed into newline characters, and all data after the
first % will be sent to the command as standard input.
...

If you escape the percent sign with \ it should work as expected:

* * * * * echo $(date +\%F) >> /tmp/date.txt

or

* * * * * echo `date +\%F` >> /tmp/date2.txt

both work on my site.

Is there a special restriction on commands executed by cron?

You have to escape percent signs with a backslash:

0 0 * * * pg_dump DB_NAME > /path/to/dumps/`date +\%Y\%m\%d`.dmp

From man 5 crontab:

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. Percent-signs (%) in the
command,
unless escaped with backslash (\), will be changed into newline
characters, and all data after the first % will be sent to the command
as
standard input. There is no way to split a single command line
onto
multiple lines, like the shell’s trailing "\".

Why does bash short curcuit fail in crontab?

percent sign has a special meaning in crontab, must be escaped \%

man 5 crontab /percent

Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as  standard input.

ack fails in cronjob but runs fine from commandline


  1. This should work:

    * * * * * ack -o "https??://cdn.host.com\S+?\.mp4" /home/me/.dump.sql </dev/null > /home/me/.mp4-matches.txt
  2. Also you probably can use your line with the the --nofilter option

cron syntax for date

Escape the percent signs with a backslash (\%).



Related Topics



Leave a reply



Submit