I am using a Debian 7 Server. I created a shell script to be executed by crontab to save a mysqldump every day.

mysqldump *database* -u *mysql-user* -p*mysql-password* > /backup/mysql_backup/*filename*.sql

If typed directly on the command line, everything works fine. But if I execute the shell script itself (./backup.sh), I get no resulting file, but no error. The folder hat rights 777, the shell script is executable. I didn't even try to put the script into the crontab, when the script doesn't even work executed on the command line.

Does anybody know what to do?

  • 3
    The first line of the script should be #!/bin/bash. You can try #!/bin/bash -x to see what bash is doing. – Jonathan Ben-Avraham May 9 '15 at 20:17
  • 1
    Tell us what is in the script (the full script). Replace the names and password by dummy ones, but don't introduce punctuation, because we can't tell what * should be there and what * you added to indicate metavariables. For example, write mysqldump mydatabase -u foo -pswordfish > /backup/mysql_backup/mydatabase.backup. Also the permissions 777 are wrong — that's probably not the cause of the script not working, but you do need to fix that. One of 755 or 700 or 775 or 770 or 750 is right, it's up to you to determine what the permissions should be. – Gilles 'SO- stop being evil' May 9 '15 at 22:07
  • My guess is that there is already a file in the directoy /backup/mysql_backup/ that matches *filename*.sql? How about a unique filename that includes date and time? – ott-- May 9 '15 at 23:28
  • use echo $? afetr mysqldump – PersianGulf May 10 '15 at 3:45

Adding #!/bin/bash did the thing, I just forgot to add that line. Can't really tell, why it does not work on the shell, but somehow it now works inside the script and inside the cronjob. Thanks for the comments.


keep in mind crontab does not have the same environment variables you do when logged into a bash shell, most notably $PATH, so get in the habit of using FULL paths to commands where crontab is involved

/usr/bin/mysqldump > /full/path/backup/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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