?
Solved

shell-script killing a process after some time

Posted on 2003-02-24
8
Medium Priority
?
790 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:witty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 1

Expert Comment

by:daravuth
ID: 8011825
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
 
LVL 1

Expert Comment

by:daravuth
ID: 8011847
oups did not see you added sample of your work. This change things.
0
 
LVL 2

Expert Comment

by:jimbb
ID: 8037091
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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 1

Author Comment

by:witty
ID: 8041814
thanks I found a solution for myself!

thanks for any support!
michi
0
 
LVL 2

Expert Comment

by:jimbb
ID: 8043236
Mind mentioning what it is?
0
 

Expert Comment

by:CleanupPing
ID: 9087882
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
 
LVL 12

Expert Comment

by:paullamhkg
ID: 9259810
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
 

Accepted Solution

by:
RomMod earned 0 total points
ID: 9325437
Finalized per recommendation.

RomMod
Community Support Moderator
0

Featured Post

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses
Course of the Month8 days, 12 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question