Tell me more ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

This question already has an answer here:

I have the following bash script:

#!/bin/bash
mysqldump -u ******** -p********  --all-databases | gzip > /home/srvlinux01/MySQLBackups/database_$(date +\%Y-\%m-\%d).sql.gz

which is located in /home/srvlinux01/MySQLBackups/ as backup.sh with the following permissions

-rwxr--r-- 1 root       root           134 feb 27 12:48 backup.sh

I've set up a cronjob on sudo crontab -e to run it every day, at night

#Automatic MySQL backup
30 3 * * * sh /home/srvlinux01/MySQLBackups/backup.sh

But I get emailed the following error:

sh: 0: Can't open /home/srvlinux01/MySQLBackups/backup.sh

I've been trying different setups, but can't figure out what is wrong. I can run the script manually and everything goes perfectly, so I guess there is something wrong with my cronjob entry, but can't really understand what. Could you please help me figure it out? Thanks!

share|improve this question
Can you try it using sudo: sudo sh /home/srvlinux01/MySQLBackups/backup.sh – Guru Mar 6 at 8:24
Do you tried to change owner? If you run this script as normal user, chown user:user /home/srvlinux01/MySQLBackups/backup.sh (replace user:user with your username and name of your user group). With that you can normaly run script as regular user. Also, if owner is root, do what @Guru saw, run script with sudo. – ZDroid Mar 6 at 8:25
So, now I changed my script owner to srvlinux01 (main user) and moved the cronjob to crontab -e instead of sudo crontab -e. Still no luck though, wierdly enough it still can't find the file. If I just copy the command and paste it into the terminal it runs normally and makes the backup! – Mateusz Kapusta Mar 6 at 8:35
Could it be that you need to give your cron a PATH? For instance: SHELL=/bin/sh PATH=/bin:/sbin:/usr/bin:/usr/sbin In your case, try putting: SHELL=/bin/sh PATH=/bin:/sbin:/usr/bin:/usr/sbin:/home/srvlinux01/MySQLBackups/ before your command. – Private Mar 6 at 8:47
Or could you try changing the shebang to #!/bin/sh? – Private Mar 6 at 8:48
show 7 more comments

marked as duplicate by Mitch, hhlp, Jai, Stephen Myall, Eliah Kagan Mar 6 at 22:29

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4 Answers

up vote 1 down vote accepted

You need to give your cron a PATH. For instance:

SHELL=/bin/sh PATH=/bin:/sbin:/usr/bin:/usr/sbin 

In your case, try putting this before your command. Check the community wiki in this question for more information on why the PATH variable is needed. Here is an excerpt; essentially the idea is that cron does not read /etc/environment:

A common "gotcha" is the PATH environment variable being different. Maybe your cron script uses the command somecommand found in /opt/someApp/bin, which you've added to PATH in /etc/environment? cron does not read that file, so running somecommand from your script will fail when run with cron, but work when run in a terminal. To get around that, just set your own PATH variable at the top of the script.

share|improve this answer

I can see one mistake in your crontab file configuration. In the below config you are trying to call backup.sh as same as in your shell prompt with sh prefix which may not work in cron.

#Automatic MySQL backup
30 3 * * * sh /home/srvlinux01/MySQLBackups/backup.sh
Solution:
  1. change the owner as said in comment, if needed.
  2. Make it as executable.
    chmod a+x <filename>
  3. Update your crontab to reflect this. (calling the file directly, shell is used as per shebang line inside the file)

    #Automatic MySQL backup
    30 3 * * * /home/srvlinux01/MySQLBackups/backup.sh
    

I hope this would help.

share|improve this answer
chmod +x /home/srvlinux01/MySQLBackups/backup.sh             

try to run your script with full path on commandline:

/home/srvlinux01/MySQLBackups/backup.sh

if it is not running - there is something wrong (path error)

Make sure this is your crontab

crontab -e 

no sudo :

sudo crontab -e

is root crontab - and root is not able to find your script ;)

remove "sh" in crontab just write:

30 3 * * * /home/srvlinux01/MySQLBackups/backup.sh
share|improve this answer

You forgot the dot '.' before the execution path.

30 3 * * * sh ./home/srvlinux01/MySQLBackups/backup.sh             
              ^
share|improve this answer
Hi, didn't change much, the error is the same unfortunately. sh: 0: Can't open ./home/srvlinux01/MySQLBackups/backup.sh – Mateusz Kapusta Mar 6 at 8:23

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