Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error on cron job, but working fine on shell

I am configuring Cron to backup my sql automatically. However I think that Cron has some issues and it's not working well.

This is the command I am running:

mysqldump --opt -Q -uhereisthename -p'hereisthepasswordwithstrangecharactersthatmustbeescaped' databasename | gzip > /home2/username/backups/backupnamefolder/backupdbwebsitename.`date +"%Y-%m-%d"`.gz

When I run it via SSH it works fine and generates the backup. However if I run it via Cron, I get the following error:

/bin/sh: -c: line 0: unexpected EOF while looking for matching `''
/bin/sh: -c: line 1: syntax error: unexpected end of file

Anybody can suggest what's wrong?

like image 505
Pikk Avatar asked Oct 12 '13 21:10

Pikk


People also ask

Why is my crontab not working and how can I troubleshoot it?

Why is crontab not working in your system? Crontab might fail for a variety of reasons: The first reason is that your cron daemon might not be working for any reason, resulting in your crontab failing. There also exists a possibility that your system's environment variables are not settled correctly.

Why is cron job not working?

Check permissionsAnother user might have created the cron job and you may not be authorized to execute it. Note that the root must own jobs added as files in a /etc/cron. */ directory. That means that if you don't have root permissions, you might not be able to access the job.

What does 30 * * * * mean in crontab?

30 * * * * your_command. this means "run when the minute of each hour is 30" (would run at: 1:30, 2:30, 3:30, etc) example #2. */30 * * * * your_command. this means "run when the minute of each hour is evenly divisible by 30" (would run at: 1:30, 2:00, 2:30, 3:00, etc)


1 Answers

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.

like image 159
joews Avatar answered Sep 21 '22 01:09

joews