Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

If I run this command from the shell, it works as expected:

cd ~ && mysqldump --databases --skip-dump-date --host=localhost --user=admin --password=xxxxx --lock-all-tables --result-file=xxxxx_dump_tmp.sql mydb && gzip xxxxx_dump_tmp.sql && mv xxxxx_dump_tmp.sql.gz xxxxx_dump_`date +"%Y-%m-%d__%H.%M.%S"`.sql.gz

However, if I set up a cronjob from plesk with the exact same command (copy-pasted), it fails with the following errors (I receive the notification via email):

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

What's wrong?

share|improve this question
add comment

2 Answers

up vote 10 down vote accepted

What's wrong?

According to 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 cronfile. 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.

…», you simply have written it wrong. So, for crontab is should be written as date +"\%Y-\%m-\%d__\%H.\%M.\%S"

share|improve this answer
add comment
  1. Don't put a complex command like this directly in the crontab file.
    Put it in a script and call the script from cron.

    By default cron-jobs get run by sh (unless configured otherwise). When you go through a script you can set the proper shell to use by putting a #!/bin/bash or something along those lines in the first line of the script.

  2. cd ~ is dangerous as hell. It totally depends on under which userid the crontab is running. Not necessarily your own userid !
    So use cd ~userid.

  3. If cron runs as a different user can that user access your home-dir? And has that user the same environment as your account? See also 4.

  4. cron-jobs starts without loading the normal environment. So a lot of normal environment vars (like $PATH) don't exist or just contain limited info. That may cause programs called from cron to fail. See also item 1. You may have to explicitly call your .bashrc (or whatever other startup script you normally use) to setup the proper environment for anything else you put in your cron-called script.

share|improve this answer
    
@jscott Thanks for the edit. Looks much better now. I had some trouble with the browser on the tablet I'm now using, because the PC is re-installing. –  Tonny Mar 16 '13 at 11:40
1  
+1 and thanks for all the valuable information, though it doesn't answer the question. Regarding ~ and the user, thanks for the hint, but I've set up the cronjob from Plesk where cronjobs are associated to the user you choose (in this case root), so I was pretty sure it would run as the correct user and it does. –  matteo Mar 16 '13 at 15:30
1  
@matteo Poige has indeed the correct answer in this specific case. Mine is more general advice about the common pitfalls many people run into when trying to do something with cron. Putting it into a separate script would have worked too by the way as that also makes the extra escaping of the % signs superfluous. –  Tonny Mar 16 '13 at 17:12
    
@Tonny, sure. Thanks for credits. As to me I think answers are to be given exactly to the questions asked. Advices are to be put in addition, not as a replacement (in rare cases there're exceptions though). –  poige Mar 16 '13 at 18:41
    
@poige I agree. In my defence: I couldn't see the entire command line. The browser on the tablet I was using earlier didn't handle the scrollbars properly. So I had to go with the generic advice. –  Tonny Mar 16 '13 at 19:07
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.