I have a need to timeout a program from a shell script. My logic
goes somewhat like this:
run this program
if it hasn't exited in x minutes, then kill it, and run this program
Is there a way to do this from a shell script, or do I have to
do it in a C program?
Thanks,
Matt
(OK, I'm a relative newbie, so keep the flames gentle...)
How about:
#! /bin/sh
process1 &
sleep $howlong
kill %1 > /dev/null
If this doesn't work, I'd like to know why...
I think the #! /bin/sh mechanism will give the process it's own job
queue, but I could be wrong...
Mike
+-Mike Shaver--...@ccs.carleton.ca---wingman@pinetree.org-+
| Saying it can't be done is no substitute for doing it. |
| Mortal Kombateer (v3.1 - :>), Hacker and Commodore Apologist |
+---------Go ahead, flame me... it's freezing up here!---------+
>(OK, I'm a relative newbie, so keep the flames gentle...)
>How about:
>#! /bin/sh
>process1 &
>sleep $howlong
>kill %1 > /dev/null
Yes, one cuold think of that. But:
o I don't know if all /bin/sh understand %1. So you better use $!.
o This script is extremly insecure. What if process1 died because of
some reason and there is another process running with this pid
(especially a problem when root runs this script...)
A little better would be the script below. It does a little more than
simply timeout xlock but also kills other processes. But we don't use
it any more because also this script is far from beeing perfect and
one might find a lot of objections here, too.
(It runs under AIX, for other OS you might have to change ps format
and grep string. This is of course the first objection aginst this
script.)
======================================================================
#!/bin/ksh
# xlock cover with timeout
# (C) Michael & Fred (@hal6000.thp.Uni-Duisburg.DE)
MIN=120;;
(( TIO = MIN * 60 ))
echo "Xlock will timeout in $MIN minutes." >&2
echo "All interactive processes will then be killed." >&2
[ "$1" = "now" ] || sleep 10
export PID=$$
(
NAME=$(whoami)
sleep $TIO
if ps -ef|grep -v grep|grep "$NAME.* $PID .*/usr/bin/xlock" >/dev/null; then
# Kill all remaining clients... /Fred
xlsclients -l | grep Window | cut -c8-16 | xargs -i xkill -id {} > /dev/null
kill -KILL $PID >/dev/null 2>&1
fi
)&
exec /usr/bin/xlock
======================================================================
Michael
--
Michael Staats, Theoretical Physics, Uni-GH Duisburg
email: mic...@hal6000.thp.Uni-Duisburg.DE
<a href="http://WWW.thp.Uni-Duisburg.DE/">Click</a> me!
<a href="http://WWW.thp.Uni-Duisburg.DE/cuaix/cuaix.html">A c.u.aix archive</a>