# sh script not executing in cron job



## motty (Oct 6, 2010)

hello Hello, 

I have a directory that i I want to backup along with a MySQL database using a sh script. if i If I ran the script manually if works fine but if i I run it through a cron job it only works partially meaning it backs up the directory but not the database. Have any idea what could be wrong with my scrip[t]? i I had set the right permissions, `chmod +x backup.sh`


----------



## Beastie (Oct 6, 2010)

Are you using absolute paths?

Are you setting the path inside the crontab?

```
PATH=/bin:/sbin:/usr/bin:/usr/sbin:etc.
```


----------



## motty (Oct 6, 2010)

you You meant path to the sh script? here Here is how my crontab looks

```
*/5     *       *       *       *       root   /usr/local/scripts/ezu/backup.sh
```


----------



## Beastie (Oct 6, 2010)

Yes, but also the *PATH* variable, as mentioned before. In other words, when you edit the crontab, write

```
SHELL=/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin
```
before the command. And add locations to the PATH variable as you see fit.


----------



## motty (Oct 6, 2010)

Thanks for your reply Beastie, below is my crontab patch variable seems to be different than your suggestion. I changed it to your suggested variable but not success. 


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

Here is my script

```
#!/bin/sh
#
dbhost=localhost
dbuser=root
dbpass='password123'
dbname=mydatabase
webrootdir=/var/www/joomla
tempdir=/usr/local/backups/anew
tarnamebase=backup
email=
confdir=/usr/local/scripts/ezu/functions.sh
attach=
. $confdir
```

I believe my problems are permission issues. because Because if I log in as user to the machine without root rights and ran the script i get the same result as cron job. I get the result only if ran script manually and as root.  
Thanks, 
-Motty


----------



## AndyUKG (Oct 7, 2010)

motty said:
			
		

> Thanks for your reply Beastie, below is my crontab patch variable seems to be different than your suggestion. I changed it to your suggested variable but not success.
> 
> 
> ```
> ...



It is probably the PATH, mysql normally installs under /usr/local so if you are relying upon mysql commands in your script you should include /usr/local/bin... ie:



```
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
```


----------



## SirDice (Oct 7, 2010)

Best thing to do is to never rely on PATH. Always use the full path to commands in scripts.


----------



## gordon@ (Oct 7, 2010)

The key thing to know about cron(8) is that it runs with a very restricted environment. Notably, your PATH is set /usr/bin:/bin. If you want to run any commands outside of that restricted set, you will likely need to either adjust your PATH or use fully qualified paths in your scripts.

This is why when you login as a normal user and run the script, it works just fine, your PATH likely has many other components that you are unknowingly relying upon when you test the script by hand.


----------



## motty (Oct 7, 2010)

Yes, I included the PATH of mysqldump command in my script but no success. 
Here is my crontab:

```
*/5     *       *       *       *       root   /usr/local/scripts/octs/uzb.sh
```

I set uzb.sh to chmod +x uzb.sh, also, my script 

`sh -c "/usr/local/bin/mysqldump -u root -ppassword database1 > /usr/local/backups/test/backup-`date +%y%m%d`.sql"`

It works fine if I ran that manually but I can't get it to work with crontab.


----------



## motty (Oct 7, 2010)

I got it to work I edit crontab and added the line below:


```
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
```
It works fine after adding two lines above.


----------



## Niso86 (Aug 18, 2011)

*i need yur help*

I have similar case where i'm running a crobtab on a script that run "df -k" and fills the data in a table on another host,

my script is as below


```
df -k | nawk 'NR > 1 {
$1 = $1; printf "insert into df values ( \47%s\47,", h
for (i = 0; ++i <=NF;)
printf "\47%s\47,", $i
print " current_timestamp );"
}' OFS=, h="SMS_JAR" |
mysql -h192.168.55.22 -uroot -prootp kpi
```



my crontab command


```
00 08 * * * /export/home/jinny/diskspace.sh
```


but it is not being executed.. i'm not very good in linux, i tried adding the below in the crontab before the related command, but i got an error 


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


----------



## Niso86 (Aug 18, 2011)

btw the script works perfectly well when running "sh diskspace.sh"
but it doesnt work at all using the crontab


----------



## SirDice (Aug 18, 2011)

It's missing the shebang line.


----------



## mobilitysd (Nov 18, 2013)

If you need additional information about the crontab command: Crontab command in FreeBSD


----------



## allen.konstanz (Jun 16, 2018)

For me works doing this: 

First set you password and DBDescription to a encrypted file with this command bellow:
`mysql_config_editor set --login-path=DBDescription --host=DB#.pair.com --user=DBUser --port=3306 --password`

See if that works:
`mysql_config_editor print --login-path=DBDescription`

The output will be something like this:

```
[DBDescription]
user = root
password = *****
host = localhost
port = 3306
```

Now create a shell script:


```
#!/bin/sh
#Set paths
MYDUMP=/usr/local/bin/mysqldump
BACKUPDIR=/usr/local/www/apache24/data
MYGZIP=/usr/bin/gzip
MYFIND=/usr/bin/find

#Dump
$MYDUMP --login-path=DBDescription DBNAME > $BACKUPDIR/mysql-DBNAME.`date '+%d-%B-%Y--%Hh'`.sql

#GZIP the file
$MYGZIP $BACKUPDIR/mysql-DBNAME.`date '+%d-%B-%Y--%Hh'`.sql
```

And finally create a job with cron.
Here I use: 
`# nano /etc/crontab`
At the final line of the file add:

```
00 04 * * * root /usr/local/www/apache24/data/mybackup.sh
```

Based on the information of the page: link


----------



## SirDice (Jun 18, 2018)

allen.konstanz please look at the dates before responding. The last post in this thread was almost 5 years ago. The thread itself started three years before that (2010!).


----------

