|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
I have a script but it hangs when run in some instances. I need to have a way where if it hasn't responded in a certain time then a message is printed. (Using ksh) Cheers
|
|
#2
|
|||
|
|||
|
Hi Bint,
The following method might help. Good luck, Andy CONTENTS OF script1.ksh (a script that could hang): Code:
#!/usr/bin/ksh echo PID=$$ while : do date sleep 5 done exit 0 CONTENTS OF script2.ksh (a script calling the script that could hang): Code:
#!/usr/bin/ksh
#-- Start running other script in the background
script1.ksh >script1.log 2>&1 &
#-- Get the process id of the script we've just started running
sleep 1
CHILDPID=`grep PID= script1.log |awk -F"=" '{print $2}'`
echo CHILDPID=${CHILDPID}
ps -fp ${CHILDPID}
#-- Check to see if it's still running after a certain amount of time
echo
STILL_RUNNING=Y
for X in 1 2 3 4
do
if [[ `ps -fp ${CHILDPID} |grep -c " ${CHILDPID} "` -ne 0 ]]; then
date "+%Y/%m/%d %H:%M:%S Script is still running ..."
else
date "+%Y/%m/%d %H:%M:%S Script has finished."
STILL_RUNNING=N
fi
sleep 3
done
#-- Kill it if it's still running
echo
if [[ ${STILL_RUNNING} == "Y" ]]; then
date "+%Y/%m/%d %H:%M:%S Killing script script1.ksh ..."
kill ${CHILDPID}
fi
exit 0
|
|
#3
|
|||
|
|||
|
Just for information, in the korn shell (possibly others?) you can get the PID of the just-started child process by referencing $! as follows:
Code:
# Start the script in the background my-script.ksh & # Make a note of the process ID child_pid=$! Incidentally, in circumstances where I've needed to watch for long-running processes I've created a timeout script that sleeps for a specific amount of time then does something - send an email, perform a kill, whatever. I then have a control script that runs the (potentially long-running) worker script and the timer script in the background (noting the PIDs as worker_pid and timer_pid respectively), then calls wait $worker_pid. This causes the control script to wait for the worker script to finish. Immediately after the wait I call kill $timer_pid. This means that if the worker script runs for longer than the timeout value I specified when I started it then the timer script will do something. If it finishes before the timeout then the timer is killed and everything is fine. Also, the return code from wait will be the final return code from your worker script so you can make sure that it ran OK. You can pass $worker_pid to the timer if you need it to kill the worker after the timeout period. Thought you might appreciate another approach. Steve |
|
#4
|
|||
|
|||
|
Nice one Steve. I like it.
I didn't know the $! ... I'll have a go using that too. |
|
#5
|
|||
|
|||
|
A refinement I implemented yesterday does away with the separate timer script. You can do something like this instead:
Code:
function timeout {
# The worker has been running too long so kill it
# and exit with a non-zero return code
print "Process has been running too long"
kill $worker_pid
exit 2
}
# Set a trap for ALRM signals
trap timeout ALRM
# Start the worker
run_worker &
worker_pid=$!
# Start a 3 minute timer
sleep 180 && kill -s ALRM $$ &
timer_pid=$!
# Wait until the worker finishes
wait $worker_pid
worker_rc=$?
# Kill the timer
kill $timer_pid
exit $worker_rc
There's a minor problem with this script though: When the timer is killed it leaves an orphaned sleep process. I'm about to post a question about this. |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > If not responding echo... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|