Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

shell-script killing a process after some time

Posted on 2003-02-24
8
Medium Priority
?
794 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
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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
I have written articles previously comparing SARDU and YUMI.  I also included a couple of lines about Easy2boot (easy2boot.com).  I have now been using, and enjoying easy2boot as my sole multiboot utility for some years and realize that it deserves …
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.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

581 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