Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When will an infinite loop end?

E.g. if I run

while :; do sleep 1; date +%s; done &

when will this end — on closing the terminal or on shutdown?

share|improve this question
up vote 5 down vote accepted

When you provide & at the end, it goes into the background. Whether it is killed or not depends on how you close the terminal.

If you close the terminal application itself (Ctrl+F4, press the x in the window decoration, or Shift+Ctrl+W for gnome-terminal), it kills the background process.

But if you close the terminal using exit or Ctrl+D, it continues to run, even if you log out (unless it is a GUI application). It will end when you shut down.

(I do not know why there is a difference between the two. I suspect that when you close the application, the operating system closes it and its children.)

The following is different:

nohup while :; do sleep 1; date +%s; done &

The nohup command detaches it from the terminal. However you close the terminal, the command will continue to run until you shut down (logging out is not sufficient) or kill the command manually.

When you use nohup, the output goes to ~/nohup.out unless you manually redirect the output.

P.S. The command will not work with nohup as given, because nohup does not take a compound command. But you could script it and run the script with nohup.

P.P.S. It is easier to see what is going on by running a GUI. Instead of the while… command, I run this:

gcalctool &

Much quicker and easier to see what is happening, as the calculator disappears when it is killed; and you can kill it easily (just close the calculator).

share|improve this answer
@choroba says 'If you supply & a the end, closing the terminal will not close the process' i'm confused. – totti Jan 31 '13 at 13:44
I have tested my answer. I run the loop without nohup, close the terminal, but the process is still there. – choroba Jan 31 '13 at 13:51
totti - You can test it yourself. @choroba - that article is out of date. I wonder if you are using a different version of Ubuntu? It very definitely goes away when I close my terminal! I am using 12.04 64-bit. – Paddy Landau Jan 31 '13 at 17:35
@PaddyLandau: See a screencast. I am using Ubuntu 10.04 LTS. – choroba Jan 31 '13 at 17:49

If you supply & a the end, closing the terminal will not close the process. It can still be killed by a TERM or KILL signal, though. On shutdown, TERM and KILL are sent to all the processes.

share|improve this answer
Is while is a process ? How can i know the pid to kill (ps aux ?) – totti Jan 31 '13 at 13:50
@totti: After starting the background process, bash will tell you on stderr, or you can use the special variable $!. – choroba Jan 31 '13 at 13:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.