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 11-17-2006, 08:12 PM   #1
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Rep: Reputation: 45
Backup script won't run in cron job. Why?


I have a simple backup script that I put in /etc/cron.daily. It won't run! However, it runs fine when I do it manually.
Code:
sudo ./backup.cron
It is copying from the hard drive to an external hard drive (VFAT, ROOT ownership).

here is the backup script
Code:
#!/bin/sh
# full and incremental backup script
# created 07 February 2000
# Based on a script by Daniel O'Callaghan <danny@freebsd.org>
# and modified by Gerhard Mourani <gmourani@videotron.ca>

#Change the 5 variables below to fit your computer/backup

COMPUTER=server                           	# name of this computer
DIRECTORIES=/projects                     	# directoris to backup
BACKUPDIR=/media/usbdisk                      	# where to store the backups
TIMEDIR=/media/usbdisk/last-full              	# where to store time of full backup
TAR=/bin/tar                            		# name and locaction of tar

#You should not have to change anything below here

PATH=/usr/local/bin:/usr/bin:/bin
DOW=`date +%a`              		# Day of the week e.g. Mon
DOM=`date +%d`              		# Date of the Month e.g. 27
DM=`date +%d%b`             	# Date and Month e.g. 27Sep

# On the 1st of the month a permanet full backup is made
# Every Sunday a full backup is made - overwriting last Sundays backup
# The rest of the time an incremental backup is made. Each incremental
# backup overwrites last weeks incremental backup of the same name.
#
# if NEWER = "", then tar backs up all files in the directories
# otherwise it backs up files newer than the NEWER date. NEWER
# gets it date from the file written every Sunday.


# Monthly full backup
if [ $DOM = "01" ]; then
        NEWER=""
        $TAR $NEWER -cf $BACKUPDIR/$COMPUTER-$DM.tar $DIRECTORIES
fi

# Weekly full backup
if [ $DOW = "Sun" ]; then
        NEWER=""
        NOW=`date +%d-%b`

        # Update full backup date
        echo $NOW > $TIMEDIR/$COMPUTER-full-date
        $TAR $NEWER -cf $BACKUPDIR/$COMPUTER-$DOW.tar $DIRECTORIES

# Make incremental backup - overwrite last weeks
else

        # Get date of last full backup
        NEWER="--newer `cat $TIMEDIR/$COMPUTER-full-date`"
        $TAR $NEWER -cf $BACKUPDIR/$COMPUTER-$DOW.tar $DIRECTORIES
fi
So any ideas why it runs when I do it manually, but not automatically by cron.daily? I have tested that my other cron jobs are running daily by putting simple scripts in the /etc/cron.daily, so I don't know why this backup script won't run by itself.
 
Old 11-17-2006, 08:45 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Check the cron log (usually /var/log/cron) for errors.
 
Old 11-17-2006, 09:26 PM   #3
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian, Arch
Posts: 2,331

Rep: Reputation: 357Reputation: 357Reputation: 357Reputation: 357
When something runs from the commandline but not from cron, first, suspect environment. Then look into environment problems. Finally, investigate what might be different in the environment!

Joking aside, it's almost always a command you're calling based on something in your PATH, but that PATH environment is not the same under cron. So the command is not found. Maybe it's not even finding the most basic thing - your backup script itself! Or it could be some other environment variable that is missing. Cron gives you a very truncated environment to work with. Occassionally you'll run into a problem where your commandline shell is not the same shell cron gives you by default. I haven't seen this so much under Linux, but under Solaris you can run into issues with /bin/ksh as you commandline and plain old /bin/sh being handed to you by cron.
 
Old 11-17-2006, 09:55 PM   #4
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Original Poster
Rep: Reputation: 45
Quote:
Originally Posted by macemoneta
Check the cron log (usually /var/log/cron) for errors.
No such cron log.

Quote:
Originally Posted by haertig
When something runs from the commandline but not from cron, first, suspect environment. Then look into environment problems. Finally, investigate what might be different in the environment!

Joking aside, it's almost always a command you're calling based on something in your PATH, but that PATH environment is not the same under cron. So the command is not found. Maybe it's not even finding the most basic thing - your backup script itself! Or it could be some other environment variable that is missing. Cron gives you a very truncated environment to work with. Occassionally you'll run into a problem where your commandline shell is not the same shell cron gives you by default. I haven't seen this so much under Linux, but under Solaris you can run into issues with /bin/ksh as you commandline and plain old /bin/sh being handed to you by cron.
That's what I suspect. I think there is something in the environment that is not being recognized. I will try copying my current environment settings into the script. Should I also change the script to a #!/bin/bash ????
 
Old 11-18-2006, 04:22 AM   #5
keithweddell
LQ Newbie
 
Registered: May 2006
Posts: 9

Rep: Reputation: 1
What directory is your script in? Try adding that directory to the line:

PATH=/usr/local/bin:/usr/bin:/bin

Also I vaguely remember reading that CRON doesn't like files with an extension. Try changing the file name to something like backup_cron.

Keith
 
1 members found this post helpful.
Old 11-18-2006, 07:32 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Quote:
Originally Posted by Micro420
No such cron log.
I find my cron output in /var/log/syslog
 
Old 11-19-2006, 02:41 AM   #7
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Original Poster
Rep: Reputation: 45
Quote:
Originally Posted by matthewg42
I find my cron output in /var/log/syslog
Wow, this is weird! This is what I get:
Code:
 sudo cat syslog.0 | grep cron.daily
Nov 17 07:35:57 ubuntu anacron[27202]: Job `cron.daily' terminated
Nov 18 06:25:01 ubuntu /USR/SBIN/CRON[28634]: (root) CMD (test -x /usr/sbin/anacron || run-parts --report /etc/cron.daily)
Nov 18 07:30:01 ubuntu anacron[28700]: Will run job `cron.daily' in 5 min.
Nov 18 07:35:01 ubuntu anacron[28700]: Job `cron.daily' started
Nov 18 07:35:01 ubuntu anacron[28705]: Updated timestamp for job `cron.daily' to 2006-11-18
In other words, everything seems to be running fine with cron, but there is something not right in the backup script, even though I am able to manually run it. Any other ideas??
 
Old 11-19-2006, 05:49 AM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
How are you making cron execute the script? Do you have a copy of the script in /etc/cron.daily/, or did you add an entry in the user crontab for some user (e.g. root).

If you entered a line into a user crontab, can you paste that line here?

In either case if a program called by cron produces output, that output should be mailed to your admin user, so having local mail delivery working is useful if you've not set that up already. (look in /var/mail to see if there are mail files which you aren't picking up. You'll be able to read them just with less (although it's ugly), or you can point your mail client to the files there and say it's the mbox format - this should split the messages up and format them a little better).

Last idea, umm, if you launch from a crontab for a user, try appending "> /tmp/my.log 2>&1", wait for the command to execute and then see what's in this logfile, /tmp/my.log
 
Old 11-19-2006, 11:22 PM   #9
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Original Poster
Rep: Reputation: 45
You guys won't believe this, but I found the problem. The problem was the filename I gave the script. I called it /etc/cron.daily/backup.cron. I decided to just rename it to /etc/cron.daily/backup and now my daily backups are happening. I don't see why the filename would be the problem, but I guess it is. Very odd! Now my next task is learning how to rsync the data to another remote server on a daily basis.
 
Old 11-20-2006, 02:06 AM   #10
keithweddell
LQ Newbie
 
Registered: May 2006
Posts: 9

Rep: Reputation: 1
Told you.

Keith
 
Old 11-20-2006, 01:51 PM   #11
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Original Poster
Rep: Reputation: 45
Quote:
Originally Posted by keithweddell
Told you.

Keith
Most awesome! Couldn't have figured it without your help! I can't believe it was just a little simple filename change that made the difference. Bleh!
 
Old 11-20-2006, 01:58 PM   #12
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
I never knew cron had a preference for some names. [checks manual page]
Quote:
Originally Posted by cron_manual_page
...Such files should be named after the package that supplies them. Files must conform to the same naming convention as used by run-parts(8): they must consist solely of upper- and lower-case letters, digits, underscores, and hyphens.
So there we are. Far out. I have to say I was skeptical that a filename change would make a difference (not enough to say so, but heh). I stand corrected. Nice one.
 
Old 11-20-2006, 02:04 PM   #13
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Yup, learn something new every day. Thanks!
 
Old 11-21-2006, 02:20 AM   #14
keithweddell
LQ Newbie
 
Registered: May 2006
Posts: 9

Rep: Reputation: 1
Glad I could help. I think I picked it up from a random comment on the Ubuntu forums.

Keith
 
Old 11-21-2006, 09:57 AM   #15
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian, Arch
Posts: 2,331

Rep: Reputation: 357Reputation: 357Reputation: 357Reputation: 357
Quote:
Originally Posted by macemoneta
Yup, learn something new every day. Thanks!
Hmmm. I'll have to try this on my Debian box. I don't have many cronjobs there, but they all DON'T have extensions. Not by plan, but by coincidence.

I know this cron limitation/anomoly does not apply to Solaris. I just checked my crontabs on a few of those servers and ALL of my jobs have extensions. Typically *.sh or *.pl

Interesting to note that Linux might handle things differently. I must say, I'm surprized by this. What possible reason would there be?
 
  


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
How to improve my mysql backup cron job stardotstar Programming 8 05-25-2006 06:11 AM
cron backup job not running dsschanze Linux - General 9 07-29-2005 08:47 PM
How to write a script to run bitdefender antivirus as a cron job? ginda Linux - Software 1 03-10-2005 08:08 PM
shell script fo run auto job in cron JolynnMarie LinuxQuestions.org Member Intro 0 04-28-2004 11:21 AM
cron job - backup medamnit Linux - Newbie 4 05-24-2002 03:37 AM

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

All times are GMT -5. The time now is 03:53 AM.

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