Bash script not executing from crontab
Solution 1
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 toPATH
in/etc/environment
? cron does not read that file, so runningsomecommand
from your script will fail when run with cron, but work when run in a terminal. To get around that, just set your ownPATH
variable at the top of the script.
Solution 2
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
Solution 3
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:
- change the owner as said in comment, if needed.
- Make it as executable.
chmod a+x <filename>
-
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.
Related videos on Youtube
Mateusz Kapusta
Updated on September 18, 2022Comments
-
Mateusz Kapusta over 1 year
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/
asbackup.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!
-
Guru about 11 yearsCan you try it using sudo: sudo sh /home/srvlinux01/MySQLBackups/backup.sh
-
Admin about 11 yearsDo 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. -
Mateusz Kapusta about 11 yearsSo, 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!
-
don.joey about 11 yearsCould 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. -
don.joey about 11 yearsOr could you try changing the shebang to
#!/bin/sh
? -
zwets about 11 yearsWhy doesn't your
backup.sh
have execute permissions? Given that it is world readable, removing the x permissions for group and other is pointless as world could just docat backup.sh | sh
and achieve the same. Apart from, obviously, being able to simply read the username and password ... -
Mateusz Kapusta about 11 yearsI've discovered that the server has home encryption enabled (sigh!) so i tried moving the script into a separate directory, but it still doesn't work. So looks like it is not the issue. I've tried changing the shebang but still no luck.
-
don.joey about 11 yearsdid you try using
PATH
? -
Mateusz Kapusta about 11 yearsBrilliant! The PATH trick did the job! Could you just explain what it does so I can use it in the future? Maybe post is as an answer so I can mark it as correct! ;)
-
Mateusz Kapusta about 11 yearsAnd thanks zwets, changed permissions, must have messed up while trying to make it work.
-
don.joey about 11 yearsCould you try whether simple setting it to
PATH=/bin:/sbin:/usr/bin:/usr/sbin
already does the trick? Now I'm wondering... I moved the comment to an answer. -
Mateusz Kapusta about 11 yearsYes, even without the home dir segment it works perfectly!
-
Vadim about 4 yearsPlease check this one askubuntu.com/a/1223213/297387
-
-
Mateusz Kapusta about 11 yearsHi, didn't change much, the error is the same unfortunately.
sh: 0: Can't open ./home/srvlinux01/MySQLBackups/backup.sh
-
Eliah Kagan almost 10 years
./
makes a path relative to the current directory, which is not desirable here and will even prevent the script from running unless cron's CWD happens to be/
. Also, the purpose of./
before a script or other executable name is when it's in the current directory (instead of searchingPATH
). It's primarily used when running the executable as the command being executed (e.g.,./script
), sometimes necessary (and often recommended) when "sourcing" (e.g.,. ./script
), but never useful when passing it as an argument to the shell (i.e.,sh script
always works as well assh ./script
). -
fusion27 over 9 years
chmod +x
!! I forget to permit my script execution permissions. thanks for posting, @Naha -
GrayedFox about 6 yearsscritps vs scripts?
-
Manohar Reddy Poreddy over 4 yearsNot "sudo crontab -e", but it is "crontab -e" is solution for my issue. Thank you
-
Manohar Reddy Poreddy over 4 yearsSee this too - askubuntu.com/questions/264607/…