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
This should work:
* * * * * ack -o "https??://cdn.host.com\S+?\.mp4" /home/me/.dump.sql </dev/null > /home/me/.mp4-matches.txt
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
Linux Shell Script to Add Leading Zeros to File Names
Change Path Permanently on Ubuntu
Nuget on Linux: Error Getting Response Stream
Can't Clone a Github Repo on Linux via Https
Replace Whitespace with a Comma in a Text File in Linux
Best Way to Get MAChine Id on Linux
Bash: Silently Kill Background Function Process
Linux Shell Sort File According to the Second Column
Specifying Udp Receive Buffer Size at Runtime in Linux
How to Get the Percentage of Memory Free with a Linux Command
How Do Applications Resolve to Different Versions of Shared Libraries at Run Time
Converting Jiffies to Milli Seconds
How to Attach a File Using Mail Command on Linux
Bash Capturing Output of Awk into Array
Why Do We Need a Bootloader in an Embedded Device
What Is an Anonymous Inode in Linux
How to Make Mv Create the Directory to Be Moved to If It Doesn't Exist