# shell script that won't run from crontab



## bovler (Jan 26, 2011)

Hi,

I have a script called backup.sh that will run fine from the command line, but doesnt run from the crontab.

The script rsyncs a directory from another server to this server, it is as follows


```
#!/bin/sh

LOCKFILE=/var/lock/backup.lock


if [ -f ${LOCKFILE} ]
then
   exit 0
fi

touch ${LOCKFILE}

cd /usr/backups/backup_files

rsync -avz --delete --rsh=/usr/bin/ssh [email]root@my_webserver.mydoma[/email]in:/path/to/directory/to/be/backed/up/.

rm -f ${LOCKFILE}
```

and in the crontab I have called it using


```
20 * * * *      /usr/sbin/backup.sh
```

The /var/log/cron says that the cron daemon tries to run the script


```
Jan 26 18:21:00 backup1 /usr/sbin/cron[8826]: (root) CMD (/usr/sbin/backup.sh)
```

There are no error messages in the /var/log/messages file and the script doesn't rsync the directory.

If I launch it from the command line then it works fine.

The system is running 6.2


```
FreeBSD backup 6.2-RELEASE FreeBSD 6.2-RELEASE #0: Fri Jan 12 11:05:30 UTC 2007     root@dessler.cse.buffalo.edu:/usr/obj/usr/src/sys/SMP  i386
```

This is my first post. Apologies if I have put it in the wrong place or misformatted it.

Regards,
Oliver


----------



## wblock@ (Jan 26, 2011)

Short answer: see #2 of The Interrupted Unix FAQ.

Longer answer:

```
% grep PATH /etc/crontab
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin
% which rsync
/usr/local/bin/rsync
```


----------



## UNIXgod (Jan 26, 2011)

wblock said:
			
		

> Short answer: see #2 of The Interrupted Unix FAQ.



wblock that was one of the best faqs I have had the pleasure to read. You should get a domain just for it. =)


----------



## Alt (Jan 26, 2011)

This FAQ really rocks xD


----------



## DutchDaemon (Jan 27, 2011)

It just gets asked again and again, and any forum search involving 'crontab' would have produced the answer. Here's one: http://forums.freebsd.org/showthread.php?p=120354&#post120354


----------



## rbelk (Jan 27, 2011)

I always add this as a header to all user crontabs.


```
MAILTO=""
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/X11R6/bin:/usr/local/sbin:/usr/local/bin:/$HOME/bin:/usr/local/scripts

# <min>   <hr>   <dom>   <month>   <dow>   <what to run>
```

This take care of the PATH issues, and unwanted emails.
Also, a remark for to help users set the time/date of the entries.


----------

