Cron error with using backquotes
Try it with $()
instead of backticks. And you probably do need to escape the percent signs since cron
converts them to newlines otherwise.
* 0 * * * /usr/bin/mysqldump -uUser -pPass Db_name > /var/www/db_backup/db.$(date +\%Y\%m\%d\%H\%M).sql
Also, you should store the password in an option file with secure permissions (eg. 600 or 640) instead of passing it on the command line.
CRONTAB syntax error
Cron needs to escape the % sign - http://www.hcidata.info/crontab.htm
Try it with a backslash:
57 1 * * 2-6 ET=`date --date 'yesterday' +\%Y\%m\%d`;echo $ET
Error on cron job, but working fine on shell
Cron treats % as a special character (meaning "new line", hence the references to lines 0 and 1 in the error message). You need to escape it:
date "+\%Y-\%m-\%d"
By the way, the posix $( )
syntax is generally better than backticks - it allows nested commands.
cron task not writing to file
The problem is that cron
treats %
as newlines. You need to escape them
From crontab POSIX man
page:
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.
* * * * * date +\%Y\%m\%d\%H\%M\%S >> /home/user/time2.txt
/bin/sh: 1: Syntax error: EOF in backquote substitution
The problem is that cron
treats %
as newlines. From crontab POSIX man
page:
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.
Also use Command Substitution syntax as $()
over the legacy `` syntax as
You could change your command to something like,
*/2 * * * * mongodump --db prodys --out /backup/databases/mongoDatabases/$(date +'\%m-\%d-\%y')
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
^
Syntax error only when command is run from cron
The easiest fix is probably to put the whole command in a shell script and just have that be run. So make a scriptName.sh file that contains the command you listed and have crontab call that script. That gets around all these odd problems.
Related Topics
How to Replace Finding Words with the Different in Each Occurrence in Vi/Vim Editor
Coqide 8.5: No Syntax Highlighting on Linux
Initiating Dynamic Variables (Variable Variables) in Bash Shell Script
Why Does If [ !$(Grep -Q) ] Not Work When If Grep -Q Does
What Really Is the "Linger Time" That Can Be Set with So_Linger on Sockets
Provide Password Using Shell Script
Docker Oci Runtime Create Failed: Container_Linux.Go:349: Starting Container Process Caused
Dbus_Bus_Request_Name (): Connections Are Not Allowed to Own the Service
What's the Difference Between 'Push' and 'Pushq' in At&T Assembly
Identifying the Preferred Ipv6 Source Address for an Adapter
Automake Subdir-Objects Is Disabled
The New Line Characted in the String Constant Isn't Being Recognized by Nasm
How to Get Environment Variables of Remote Host
Unterminated Address Regex While Using Sed
G++ Conio.H: No Such File or Directory