LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 02-13-2007, 07:49 AM   #1
nonik
LQ Newbie
 
Registered: Jan 2004
Posts: 4

Rep: Reputation: 0
Trouble with cron


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.
 
Old 02-13-2007, 08:46 AM   #2
wpn146
Member
 
Registered: Jan 2005
Distribution: Solaris, Linux Fedora Core 6
Posts: 170

Rep: Reputation: 30
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.

Last edited by wpn146; 02-13-2007 at 09:13 AM.
 
Old 02-13-2007, 09:02 AM   #3
nonik
LQ Newbie
 
Registered: Jan 2004
Posts: 4

Original Poster
Rep: Reputation: 0
Nope, as I said, the script seems to die in silence =[
 
Old 02-13-2007, 09:15 AM   #4
wpn146
Member
 
Registered: Jan 2005
Distribution: Solaris, Linux Fedora Core 6
Posts: 170

Rep: Reputation: 30
I was editing my reply when you replied. You may want to read my response again in case you missed it.
 
Old 02-08-2008, 04:54 PM   #5
cmnorton
Member
 
Registered: Feb 2005
Distribution: Ubuntu, CentOS
Posts: 585

Rep: Reputation: 35
CRON Default Directory

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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble with cron GavB Linux - Newbie 1 11-09-2006 01:39 PM
bash script causes trouble with cron Suinatsa Programming 10 06-14-2006 03:20 AM
upgrade trouble (exim4 + mutt + cron) naruto273 Debian 5 03-09-2006 12:10 PM
How to trouble shoot cron jobs ? bowie101 Linux - General 3 01-26-2006 11:34 AM
Trouble with perl script in Cron job thack111 Linux - General 7 11-25-2004 02:44 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration