Saturday, May 4, 2024
 Popular · Latest · Hot · Upcoming
19
rated 0 times [  19] [ 0]  / answers: 1 / hits: 165915  / 2 Years ago, sun, january 9, 2022, 9:33:09

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!


More From » bash

 Answers
2

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.



[#32340] Monday, January 10, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bsorbedeldy

Total Points: 315
Total Questions: 110
Total Answers: 108

Location: Reunion
Member since Mon, Dec 28, 2020
3 Years ago
;