How to stop infinite loop

Im unable to stop the below infinite loop (bash script). Can someone tell me why this isnt responding to signals eg: ctrl+c (SIGINT) or ctrl+z

 c=0
 test_loop() {
     c=$(($c+1))
     echo "count value is : $c "
     sleep 1
     test_loop
 }

Im using: SunOS 5.10

PS: If run this as a script then it is responding to kill signals, but running in command line makes it unresponsive

Try ---

 kill <process-id> 

---------- Post updated at 01:37 AM ---------- Previous update was at 01:35 AM ----------

Get more info...

ps -ef | grep mytestscript.sh
kill <process-id>
 

How is this function called in the script
This code by itself will not run in a loop.
That's just a function definition.

bmk: when I execute the code in command line it isnt responding, but when i put in the script im able to bring it down by pressing ctrl+c. So your suggestion aint working for me.

---------- Post updated at 12:13 PM ---------- Previous update was at 12:12 PM ----------

elixir_sinari: yes off-course we need to call the function.

solaris$ :>  test_loop

Is it possible for you to post what you type in and show how the function has gone "rogue".

the function is a recursive one (calls itself) hence you need a if condition in the function body so that it can be terminated when a certain condition is met(e.g when $c reaches a specific value).

@codemaniac, I think the OP knows that obvious fact. He is just asking for a way to stop the loop.

@Arun_Linux, I don't seem to be having any problem stopping the loop in bash 4.1 on cygwin using SIGINT.

Here's copy paste of my window...

bash-3.00$  c=0
bash-3.00$  test_loop() {
>      c=$(($c+1))
>      echo "count value is : $c "
>      sleep 1
>      test_loop
> }
bash-3.00$ test_loop
count value is : 1 
count value is : 2 
count value is : 3 
count value is : 4 
count value is : 5 
^C
count value is : 6 
^C
count value is : 7 
^C
count value is : 8 
^Z
[1]+  Stopped                 sleep 1
count value is : 9 
count value is : 10 
count value is : 11 
^Z
[2]+  Stopped                 sleep 1
count value is : 12 
count value is : 13 
count value is : 14 
count value is : 15 
count value is : 16 
count value is : 17 

I think this is issue because of you are calling function in function.

Better way use infinite loop in the function itself. That will be killed by ctrl c .

Like this..

 c=0 
 test_loop() {
     while true
     do
     echo "$c"
     let c++
     sleep 1
     done
 }

Please post the output of stty -a .

If you run it on the command line, you are running it directly in the parent shell, so there are no foreground process groups that listen to ctrl-c, except for the "sleep"-command , so each time Ctrl-C is pressed one sleep terminates, but a new sleep gets started by the recursive function loop..

If you run it as a script, then a new shell is started in the foreground in which these commands are run, so then that child shell will listen to CTRL-C and terminate, so then the script stops...

1 Like

elixir_sinari: here's the output

bash-3.00$ stty -a
speed 38400 baud; 
rows = 37; columns = 190; ypixels = 0; xpixels = 0;
csdata ?
eucw 1:0:0:0, scrw 1:0:0:0
intr = ^c; quit = ^\; erase = ^h; kill = ^u;
eof = ^d; eol = <undef>; eol2 = <undef>; swtch = <undef>;
start = ^q; stop = ^s; susp = ^z; dsusp = ^y;
rprnt = ^r; flush = ^o; werase = ^w; lnext = ^v;
-parenb -parodd cs8 -cstopb -hupcl cread -clocal -loblk -crtscts -crtsxoff -parext 
-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl -iuclc 
ixon -ixany -ixoff imaxbel 
isig icanon -xcase echo echoe echok -echonl -noflsh 
-tostop echoctl -echoprt echoke -defecho -flusho -pendin iexten 
opost -olcuc onlcr -ocrnl -onocr -onlret -ofill -ofdel tab3 

Initially, I was inclined towards a view similar to that of Scrutinizer's. But, when I tried this in bash 4.1, it worked (SIGINT stopped the loop). It also worked in ksh93.

But, when I tried it in bash 3.0 just now, it didn't work (just like the OP had mentioned)!!!

Any explanations anyone?

---------- Post updated at 03:00 AM ---------- Previous update was at 02:24 AM ----------

Here's a nice article I found:
Proper handling of SIGINT/SIGQUIT
May help people understand signal handling.

elixir_sinari:
Finally your answer is much convincing.. I read the link, good explanation. But still I wonder why ctrl-c is ignored since i'm not handling any traps to capture the signals.