Operating System - HP-UX
1823986 Members
4388 Online
109667 Solutions
New Discussion юеВ

How to stop an endless loop with trap?

 
SOLVED
Go to solution
cybermilky
Occasional Contributor

How to stop an endless loop with trap?

How do I stop my while loop by using 'trap'? I don't want my shell script to use ctrl-c.
-------------
#!/bin/sh
trap '???' ? #Problem: How to trap?
while:
do
echo Program is looping
done
-------------
How can I kill it while I'm having an endless loop?
3 REPLIES 3
harry d brown jr
Honored Contributor
Solution

Re: How to stop an endless loop with trap?


Write better scripts, by not having infinite loops! That's the first rule of programming.

live free or die
harry
Live Free or Die
harry d brown jr
Honored Contributor

Re: How to stop an endless loop with trap?


give this link a once over:

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x022718276953d61190040090279cd0f9,00.html

live free or die
harry
Live Free or Die
Wodisch_1
Honored Contributor

Re: How to stop an endless loop with trap?

Hi,

well, why not use signal #1 (SIGHUP) and send that signal from another shell-session to that running script?

trap "echo trapped" 1 # set trap
#your loop here
trap "" 1 # release trap

Now you start it, say into the background:

./your-script & # now $! is the PID of this process
pid=$!

And finally, as soon as you decide to stop it:

kill -1 $pid

HTH,
Wodisch