Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi, I'm experiencing serious trouble with a shell script which is supposed to be run by cron (mailbackup). When I run the script from bash it runs perfect well, but when the script is invoked by cron it starts but it stops randomly without giving any reason or leaving any entrys in my logs.
My Script:
Code:
#!/bin/bash -x
#Sichert alle Mailverzeichnisse
#v0.1 11.01.2007
#
#Variablen deklarieren
startzeit=`date +%s`
LOCKFILE=/var/lock/mailbackup.lock
MAILDIR=Maildir
BACKUPDIR=/media/bigdickhomes
#Mit rsync wegschreiben
if [ -f $LOCKFILE ]
then
echo $LOCKFILE existiert, ich breche ab!
exit 0
else
touch $LOCKFILE
for HOMEDIR in `ls /home/`; do rsync --archive /home/$HOMEDIR/$MAILDIR $BACKUPDIR/$HOMEDIR; done
rm $LOCKFILE
endzeit=`date +%s`
diff=`expr $endzeit - $startzeit`
minuten=`expr $diff / 60 `
echo "Startzeit: $startzeit"
echo "Endzeit: $endzeit"
echo "Dauer: $diff Sekunden"
echo "Dauer: $minuten Minuten"
echo Feddich!
fi
This is the entry in roots crontab:
Code:
0 4 * * * /usr/local/sbin/mailbackup.sh
I tried to change the starttime to be sure that the Script doesn't collide with...well something else (whatever), but it doesn't make no difference.
By default, if crontab produces output (and your "-x" should cause that), it is sent to the owner of the crontab by mail. Are you seeing anything there?
The most common cause for commands to execute normally when entered from the keyboard and fail from cron is differences in your environment. Your script itself is a /bin/bash script. So, is there anything in your interactive environment that is different from the environment of a new bash environment run non-interactively? There could be environment changes made that are conditional upon whether or not the environment is an interactive shell, usually by testing if you are assigned a tty.
Looking at your script, the only thing suspicious to me is the "ls" command. If you are expecting one line per /home directory entry, you may want to change it to "ls -1". The output of the "ls" command without the "-1" depends on the width of the terminal, and since the TERM variable is normally different running interactively than under cron, this may be the difference.
I had a CRON job that also ran well from the command line, but did not run well from CRON. The shell script located a bunch of reports in one directory, turned those reports into pdf files; emailed the pdf files, and moved away both the reports and pdf files to a "printed" directory. It sounds simple, and should have been.
After pealing away a lot of cockpit error problems some of which were as blunt as two identical entries for the same job in /etc/crontab and some slightly more subtle errors involving using absolute and relative paths, I was finally left with one problem I could not explain. My mailer complained it could not create temporary files.
I put in a pwd at the start of the shell script, and realized that CRON did not execute my user's login script, and hence the default directory in CRON was /. I fixed the problem by modifying the shell script to set its default directory to the shell script's user's directory.
So, does your shell script depend on being in a directory, which it is when you run it from the command line?
I also have a question to go along with this answer. Why doesn't cron execute the login script. If you login to a system remotely, this is done for you, but my understanding is that the remote listener causes your login to run, when the shell is launched.
Last edited by cmnorton; 02-08-2008 at 04:54 PM.
Reason: grammar
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.