shell-script killing a process after some time

hi!

i want to call a program in a shell-script.
normally i check its returncode
if [ $? -eq ...
but this program simple hangs, if an error occurs.
so i want to call the program and wait
either for its normal end or kill it after some seconds.

how to achieve this nifty in a shell script?

what I tried:

--cut--
#!/bin/bash

{ echo "simulating hanging program"; sleep 600 ; echo "finished" ; } &
PID=$!
echo ${PID}
CMD="
echo "killing process ${PID}"
kill -s SIGHUP ${PID}
kill -9 ${PID}
"
echo -e "${CMD}"|at now + 2 minutes
--cut--

but when I kill the $PID, the sleep-command is still running ...

thanks
michi
LVL 1
wittyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

daravuthCommented:
Hello, this works on my machine (solaris + korn shell)

--------------------
#!/bin/ksh -f
RESULT=`(./childutil;echo $?) &`
sleep 20
if [ $RESULT -ne 0 ] ; then
        'check_to_kill_the_hanging_process()
fi

--------------------

This give 20 seconds for childutl to run and return a valid return value. Should this time exceed or childutil  return the wrong return value, you will get inside the IF code. Otherwise your script continues normally. If childutil is supposed to ouptput anything, you can redirect the output to a file childutil >> /dev/null 2>>/dev/null

good luck
0
daravuthCommented:
oups did not see you added sample of your work. This change things.
0
jimbbCommented:
Hello, with many Bourne shells you can trap the CHLD signal (received when a child process exits) and act accordingly.

I say "with many" because it works with /bin/sh on my OpenBSD (pdksh) and Solaris systems, but apparently does not work correctly with Bash, and I'm not sure why.  So if you want to use this on a Linux box, you'd probably have to use ksh or ash as your interpreter.

Anyway, here's an example:

#!/bin/sh

getchild () {
echo 'Got CHLD!'
}

trap getchild 20

kill -s CHLD $$


I know that's not a real "answer" to your question as I am also looking into this issue for some of my own purposes; but if you can see where I'm going with that you should be able to incorporate it into your script.  For example, if you want the process to run for no more than 300 seconds, you can background it (with '&') and then run a sleep 300.  In your trap function, set a variable like GOTCHLD=1.  After your sleep is done, check whether that variable has been set.  If not, then kill the child process.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

wittyAuthor Commented:
thanks I found a solution for myself!

thanks for any support!
michi
0
jimbbCommented:
Mind mentioning what it is?
0
CleanupPingCommented:
witty:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
paullamhkgCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is to:

Be PAQ'd/Points No Refunded

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Paul
EE Cleanup Volunteer
0
RomModCommented:
Finalized per recommendation.

RomMod
Community Support Moderator
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.