Logo 
Search:

Unix / Linux / Ubuntu Forum

Ask Question   UnAnswered
Home » Forum » Unix / Linux / Ubuntu       RSS Feeds

Using cron to run a script

  Date: Dec 02    Category: Unix / Linux / Ubuntu    Views: 523
  

I'm trying to write a script to use rsync to backup my data regularly.
Everything seems to work on its own, but when I use cron to run it daily
there's a small but critical problem. The script ("choose_bu") runs,
rsync runs, but terminates immediately (well, within 1 second) and no
new or changed files are copied to the backup set. Running choose_bu or
choose_bu -f from the command line both work perfectly well.

So, any idea what could be happening? I know the cron call works, I know
choose_bu works, what's preventing them working together?

Appended below are the script, the crontab listing and the output from
the script.

The choose_bu script:
--------------------

#! /bin/bash

if test X$1 = X-f
then
fullbu=0
message_out='Full'
else
fullbu=1
message_out='Incremental'
fi

echo Started $message_out backup at:
date

#The directory to back up
folder=/home/$USER

#The backup directory/drive
backup=/media/Samsung

if test $fullbu = 0 ; then
rm -fr $backup/oldbu
mv $backup/steve $backup/oldbu
fi

rsync -avzu --exclude-from=$folder/Documents/Techie/bu_exclude $folder
$backup

echo Completed $message_out backup at:
date
*******************************************

Crontab:
--------

steve@Ubuntu-A:~$ crontab -l

0 3 * * 1-6 Documents/Techie/choose_bu > Desktop/output # JOB_ID_2
0 3 * * 7 Documents/Techie/choose_bu -f > Desktop/output # JOB_ID_3
steve@Ubuntu-A:~$
*********************************************

Content of /Desktop/output:
---------------------------

Started Incremental backup at:
Mon May 31 03:00:01 BST 2010
running incremental backup
Completed Incremental backup at:
Mon May 31 03:00:01 BST 2010
*******************************************

Share: 

 

7 Answers Found

 
Answer #1    Answered On: Dec 02    

Remember that cron runs as root and may have a different $PATH than when
you run from the console. Ensure that you use full pathnames when
referencing files. Also check the system log files for any suspicious
goings on. Next would be to insert some debugging statements in the
code to determine where it is failing. Write the debug output to a file
for later examination.

 
Answer #2    Answered On: Dec 02    

Actually cron runs jobs as whoever scheduled the job to run. In any
case, you need to properly set your path in any scripts run from cron.
Assume nothing.

 
Answer #3    Answered On: Dec 02    

The easy way: install Back in Time from the Ubuntu repositories.

 
Answer #4    Answered On: Dec 02    

What I meant to say is that cron does not set the user's $PATH (on my
systems users cannot run cron, only root) consequently a script could
run from a shell and not from cron. Most cron issues are path related.

 
Answer #5    Answered On: Dec 02    

Maybe this is just a typo in the email, but there should be no space on the
first line of your script. i.e., shoud be "#!/bin/bash", unless I am
loosing my mind (I wouldn't put that past me) :)

I don't know too much about cron, but you may want to add something like "
--log-file=/some/directory/rsync.log" to your rsync command to see if any
errors are getting thrown. But that may not help if the issue is that cron
is not able to find rsync. like if it only looks in /usr/bin and rsync is in
/usr/local/bin. But I don't know.

 
Answer #6    Answered On: Dec 02    

It worked! That's the only change I made, took out that errant space and
it ran.

Seems odd, because the script itself was running (it output its
messages) and I would have thought if it couldn't identify the shell
properly it would have failed altogether...?


 
Answer #7    Answered On: Dec 02    

I think with the space, it interpreted that first line as a comment,
and used the default shell syntax to try to interpret the script
(Bourne maybe?). It worked from the terminal, I assume, because you
were executing it from withing the bash shell. But that level of
knowledge is somewhat out of my league. Glad you got it working.

 
Didn't find what you were looking for? Find more on Using cron to run a script Or get search suggestion and latest updates.




Tagged: