Terminating an infinite loop

I have a command that I want to have run again automatically each time it terminates, so I ran something like this:

while [ 1 ]; do COMMAND; done;

but if I can’t stop the loop with Ctrl-c as that just kills COMMAND and not the entire loop.

How would I achieve something similar but which I can stop without having to close the terminal?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can stop and put your job in background while it’s running using ctrl+z. Then you can kill your job with:

$ kill %1

Where [1] is your job number.

Method 2

Check the exit status of the command. If the command was terminated by a signal the exit code will be 128 + the signal number. From the GNU online documentation for bash:

For the shell’s purposes, a command which exits with a zero exit status has succeeded. A non-zero exit status indicates failure. This seemingly counter-intuitive scheme is used so there is one well-defined way to indicate success and a variety of ways to indicate various failure modes. When a command terminates on a fatal signal whose number is N, Bash uses the value 128+N as the exit status.

POSIX also specifies that the value of a command that terminated by a signal is greater than 128, but does not seem to specify its exact value like GNU does:

The exit status of a command that terminated because it received a signal shall be reported as greater than 128.

For example if you interrupt a command with control-C the exit code will be 130, because SIGINT is signal 2 on Unix systems. So:

while [ 1 ]; do COMMAND; test $? -gt 128 && break; done

Method 3

I would say it might be best to put your infinite loop in a script and handle signals there. Here’s a basic starting point. I’m sure you’ll want to modify it to suit. The script uses trap to catch ctrlc (or SIGTERM), kills off the command (I’ve used sleep here as a test) and exits.

cleanup ()
{
kill -s SIGTERM $!
exit 0
}

trap cleanup SIGINT SIGTERM

while [ 1 ]
do
    sleep 60 &
    wait $!
done

Method 4

I generally just hold down Ctrl-C. Sooner or later it’ll register between COMMAND‘s and thus terminate the while loop. Maybe there is a better way.

Method 5

If you run bash with -e it will exit on any error conditions:

#!/bin/bash -e
false # returns 1
echo This won't be printed

Method 6

Why not simply,

while [ 1 ]; do COMMAND || break; done;

Or when used in a script,

#!/bin/bash
while [ 1 ]; do
  # ctrl+c terminates COMMAND and exits the while loop
  # (assuming COMMAND responds to ctrl+c)
  COMMAND || break
done;

Method 7

  1. You can always kill a process using its PID, there’s no need to close your terminal
  2. If you want to run something in an infinite loop like a daemon then you’d best put it in the background
  3. while : will create an infinite loop and saves you writing the [ 1 ]
    while :; do COMMAND; done &

This will print the PID. If you exit your prompt using ctrl+d then the background job won’t quit, and you can later kill the job from anywhere using kill PID

If you lose track of your PID, you can use pstree -pa $USER or pgrep -fl '.*PROCESS.*' to help you find it

Method 8

I prefer another solution:

touch .runcmd; while [ -f ".runcmd" ]; do COMMAND; sleep 1; done

In order to kill the loop, just do:

rm .runcmd && kill `pidof COMMAND`

Method 9

What works reasonably well for me is:

while sleep 1; do COMMAND; done

This works because sleep 1 sticks around for a while and if it gets ctrl+c it return with non zero and the loop will terminate.

Method 10

Use trap

exit_()
{
    exit
}

while true
do
    echo 'running..'
    trap exit_ int
done

Method 11

I like this one-liner for this kind of problem:

while true ; do if ! $COMMAND ; then break ; fi ; done

As long as $COMMAND terminates successfully, it will be run again. If it fails or is terminated by signal, the loop will end.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x