cronning rsync

tim.conway at tim.conway at
Thu Jun 20 14:51:02 EST 2002

Unless you can be certain that nobody else might run their own rsync, it's 
not quite ready.
I'd suggest more like


#get the content.  will be blank if nonexistent... saves a stat.
 oldpid=`cat $pidfile 2>/dev/null`
#verify that it's numeric
if [ "`expr $oldpid / $oldpid 2>/dev/null`" -eq 1 ]
#see if it represents a running rsync... unlikely to randomly get another 
rsync on the same pid
  if ps -p $oldpid |grep rsync >/dev/null
#and if it's running, that's all we need to know.. maybe next time 
   exit 0
#fire off the rsync in the background
rsync -options source destination &
#save its pid for the next run, in case we're not done when he starts
echo $! > $pidfile
#wait for it to finish (this is kid of like a "fg"
wait $!
#and get rid of the pid file
rm $pidfile
fix the ps for however your system works.

Now, there are still two vulnerabilities here.  If it takes more than 5 
minutes to cat the file, do the expr, do the ps and grep call rsync, and 
echo the rsync pid into the pidfile, you could possibly get a race 
condition.  If it takes that long, though, you've got bigger problems. 
Frankly, you should be safe all the way down to an every-minute run, 
though that would probably be wasteful.
I don't know if the setlock thing is completely immune to racing.  It 
might be better.

Tim Conway
tim.conway at
303.682.4917 office, 3039210301 cell
Philips Semiconductor - Longmont TC
1880 Industrial Circle, Suite D
Longmont, CO 80501
Available via SameTime Connect within Philips, n9hmg on AIM
perl -e 'print pack(nnnnnnnnnnnn, 
".\n" '
"There are some who call me.... Tim?"

matthew.2.burgess at
Sent by: rsync-admin at
06/20/2002 08:03 AM

        To:     rsync at
        cc:     (bcc: Tim Conway/LMT/SC/PHILIPS)
        Subject:        RE: cronning rsync

Thanks for all your swift replies.  I can't believe that I didn't even see
the obvious solution of checking to see if rsync was running before-hand.
I've essentially rolled up my call to rsync in a shell-script as below:

---begin script---


#is a previous rsync process still running?

ps -ef | grep 'rsync' | grep -v 'grep rsync' | grep -v '' >

if [ $? -eq 1 ]; then #rsync isn't running - let's launch it now
 rsync [opts] user at host::/module/* .
  echo "Rsync is still running...please wait and try again later"

---end script---

Thanks for all of your suggestions,

Matt Burgess

British Telecommunications plc
Registered office: 81 Newgate Street London EC1A 7AJ
Registered in England no. 1800000
This electronic message contains information from British 
plc which may be privileged or confidential. The information is intended 
be for the use of the individual(s) or entity named above. If you are not
the intended recipient be aware that any disclosure, copying, distribution
or use of the contents of this information is prohibited. If you have
received this electronic message in error, please notify us by telephone 
email (to the numbers or address above) immediately.

-----Original Message-----
From: Adrian Ho [mailto:aho-sw-rsync at]
Sent: Thursday, June 20, 2002 14:19
To: rsync at
Subject: Re: cronning rsync

On Thu, Jun 20, 2002 at 12:51:14PM +0100, matthew.2.burgess at wrote:
> We're thinking about putting rsync to use in our production environment.
> What we want to do is have a cron job running on a client that 
> files on the host every five minutes.  We believe that some of the files
> will take longer than five minutes to complete.

Then you'll want to ensure that only one rsync instance is active
at a time.  This is best done with an external locking mechanism.
My personal favorite is setlock, part of Dan Bernstein's daemontools
package <>, and your setlock'd cron job
might look like this:

0-55/5 * * * * setlock -n /tmp/.rsync.lock rsync <blah blah blah>

Your OS may have similar functionality already available, and other
3rd-party packages may also have rolled their own equivalents (I know
both procmail and maildrop did) -- "man -k lock" to see for yourself.

- Adrian

To unsubscribe or change options:
Before posting, read:

To unsubscribe or change options:
Before posting, read:

More information about the rsync mailing list