[CentOS] Timeout for a script

Filipe Brandenburger filbranden at gmail.com
Wed Nov 12 14:19:26 UTC 2008


Hi,

On Wed, Nov 12, 2008 at 08:39, Jussi Hirvi <greenspot at greenspot.fi> wrote:
> Thanks - but I couldn't make that work as expected. It seems to kill
> *something*, but after that, the rsync part still continues in the
> background...

If what you want to kill is the rsync process, do the opposite, run
rsync in background, sleep for some time, test if it is still running
and then kill it.

> Here's my last test:
>
> log='/root/log/rsync2'
> timeoutseconds=1
> pid=$$
> (sleep $timeoutseconds;
>    echo `date '+%c'` " $0 INTERRUPTED" >>$log;
>    kill -9 $pid) &
> /usr/bin/rsync -avzu --delete /root /home/palvelimet/bckserver1
> echo `date '+%c'` " $0 valmis" >>$log

Use something like:


#! /bin/bash
timeout=60
/usr/bin/rsync -avzu --delete /root /home/palvelimet/bckserver1 &
rsync_pid=$?
sleep "$timeout"
# test if process $rsync_pid is still a child of this process:
ppid_rsync=`ps -o ppid= "$rsync_pid"`
# remove any spaces
ppid_rsync=`echo $ppid_rsync`
# compare the parent of $rsync_pid with this process, if it matches,
kill $rsync_pid
test x"$ppid_rsync" = x"$$" && kill "$rsync_pid"


It's certainly more convoluted than it should be... but it should work.

The C solution is probably the right thing to do, if you have time to
dig into it and find out how it works.

HTH,
Filipe



More information about the CentOS mailing list