# crontab with mysqldump issue



## wolfan (Jan 7, 2010)

I'm having an issue with an automatic mysqldump using cron.  I have written the following (what I think) is very simple bash script. The database is very small and simple, and I already have automatic backups setup, I am just using this mysqldump as an extra backup.


```
#!/usr/local/bin/bash
mysqldump -u username -ppassword database_name > /root/mysql_backup/PIS_Backup
```

So the issue is, that when I run this bash script everything happens just as normal, a file is created called PIS_Backup. However, when I run it through cron it creates the file, but the file is empty.  The cron entry looks like this.


```
0 0,12 * * * /root/mysql_backup/backup
```

Thanks in advance.


----------



## Alt (Jan 7, 2010)

1. Drop this damned bash
2. Call mysqldump with full path (use whereis to locate it)


----------



## dh (Jan 7, 2010)

3. set PATH env variable in crontab. By default PATH is /usr/bin:/bin according to man 5 crontab


----------



## phoenix (Jan 7, 2010)

As mentioned, either set the PATH in the crontab, set the PATH in the script, or use full pathnames in your script.

Alternatively, if it's a single line like that, just put it directly into the crontab.


----------



## wolfan (Jan 8, 2010)

Fixed, thank you very much.


----------



## Wolfi83 (Sep 8, 2011)

Hi!!

I got the exactly the same problem 
Can you please explain the exactly details to fix it, please? 
It's very important for me


----------



## Sylhouette (Sep 8, 2011)

Well it is told in the above comments what to do.

first

edit the script so that the script can find mysqldump outside its normal PATH like /usr/bin/ /bin /usr/sbin and /sbin
it would look like the following.

```
#!/usr/local/bin/bash
/usr/local/bin/mysqldump -u username -ppassword database_name > /root/mysql_backup/PIS_Backup
```

or do not call a script and let cron do it all on its own!
It is one command so cron could do it on its own.

```
0 0,12 * * * root /usr/local/bin/mysqldump -u username -ppassword database_name > /root/mysql_backup
```

or make sure cron can find /usr/local/bin

edit the crontab file
If you open the crontab file you will see the following lines

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

So add /usr/local/bin to it

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

The choice is yours 

regards
Johan


----------



## Wolfi83 (Sep 8, 2011)

So if i add /usr/local/bin/ in PATH at crontab i don't need the real path of mysqldump and can use *MYSQLDUMP=$(which mysqldump)* so via which command and that works?


----------



## DutchDaemon (Sep 8, 2011)

Again: leave /etc/crontab alone. It's the system crontab. Use the root crontab (*crontab -e*) for your own alterations.


----------



## Sylhouette (Sep 8, 2011)

I will not instruct people any more to alter the /etc/crontab file. :r


```
MYSQLDUMP=$(which mysqldump)
```
if you are using the above, then you cannot ad that in the crontab line.
This is for a script.

Can you post your mysqldump script. (make sure the password is not copied along).

regards
Johan


----------

