Cron Error with Using Backquotes

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



Leave a reply



Submit