Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-06-2009, 08:53 PM   #1
LQ Newbie
Registered: Jun 2003
Posts: 10

Rep: Reputation: 0
Question Shell Script works different from cron

I wrote a simple shell script for backing up my home directory using the tar command. If I run the script from the command line then it backs up my files as required. Everything seems to work fine. However, when I run it from cron my results are different. It still creates a backup file however, all of the directories in the .tar file are empty. No actual files are backed up.

Here's the part of the script that actually does the backing up...


if test -f /home/necrolin/.homebackup/snapshots/l0snapshot
# Copy snapshot files to avoid errors
cp /home/necrolin/.homebackup/snapshots/l0snapshot /home/necrolin/.homebackup/l1snapshot

# Run backup
tar -cvf /home/backup/level1.tar \
--exclude=".*" \
--exclude="/home/necrolin/Videos" \
--exclude="/home/necrolin/Music" \
--exclude="/home/necrolin/examples.desktop" \
--label="Level 1 backup executed on: `date`" \
-g /home/necrolin/.homebackup/l1snapshot /home/necrolin

# Copy snapshot file for use with level 2 backups
cp /home/necrolin/.homebackup/l1snapshot /home/necrolin/.homebackup/snapshots/l1snapshot
echo "You must run a level 0 backup before you can run a level 1 backup"

Any ideas?

Last edited by necrolin; 05-06-2009 at 08:55 PM.
Old 05-06-2009, 11:18 PM   #2
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 572

Rep: Reputation: 56
The environment (PATH etc) that is set from cron is quite different from the interactive login environment. Add "set -x" to the start of your script to see what the script is doing. The output will be emailed to you by cron, and feel free to post it if you need help deciphering it.
Old 05-07-2009, 10:52 AM   #3
LQ Newbie
Registered: Jun 2003
Posts: 10

Original Poster
Rep: Reputation: 0
Smile Solved

OK, I figured it out. It was so simple.... OMG.

The options I had were: tar -cvf. The -v (verbos) option would spit out all kinds of data which were useful for me in debugging the script. But, they happen to crash the cron job for some reason. Just remove the -v option and it works just fine. =)


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
shell prompt works, cron job fails btuley Linux - General 1 11-13-2008 12:40 PM
shell script using /etc/cron.hourly to execute cron.php file? rioguia Programming 3 06-11-2008 09:09 AM
Script works when run manually but not in cron job Harlin Linux - Software 7 10-16-2007 07:36 PM
Command works in shell, fails as a cron task angeljusto Linux - Software 3 06-13-2007 08:06 AM
command works in shell, but not in cron bret Linux - Newbie 10 09-04-2006 03:16 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:29 PM.

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