I get the message There are stopped jobs. when I try to exit a bash shell sometimes. Here is a reproducible scenario in python 2.x.

  • ctrl + c is handled by the interpreter as an exception.
  • ctrl + z 'stops' the process.
  • ctrl + d exits python for reals.

Here's the real-world output to the terminal

example_user@example_server:~$ python
Python 2.7.3 (default, Sep 26 2013, 20:03:06)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.


[1]+  Stopped                 python
example_user@example_server:~$ exit
There are stopped jobs.

Bash did not exit, I must exit again to exit the bash shell.

  • Q: What is a 'stopped job', or what does this mean?
  • Q: Can a stopped process be resumed?
  • Q: Does the first exit kill the stopped jobs?
  • Q: Is there a way to exit the shell the first time? (without entering exit twice)
Best Answer

A stopped job is one that has been temporarily put into the background and is no longer running, but is still using resources (i.e. system memory). Because this job is not attached to the current terminal it cannot produce output and receives no input from the user

You can see jobs you have running using the jobs builtin command in bash, probably other shells as well. Example.

user@mysystem:~$ jobs
[1] + Stopped                python

You can resume a stopped job by using the fg (foreground) bash built-in command. If you have multiple commands that have been stopped you must specify which one to resume by passing jobspec number on the command line with fg . If only one program is stopped, you may use fg alone.

user@mysystem:~$ fg 1

At this point you're back in the python interpreter and you can leave the process by executing control-d

Conversely, you may kill the command with either it's jobspec or PID. Take example

user@mysystem:~$ ps
  PID TTY          TIME CMD
16174 pts/3    00:00:00 bash
17781 pts/3    00:00:00 python
18276 pts/3    00:00:00 ps
user@mysystem:~$ kill 17781
[1]+  Killed                  python

The jobspec uses the percentage key and precedes the number

user@mysystem:~$ kill %1
[1]+  Terminated              python

When you do an exit command on stopped jobs the warning you saw will be shown The jobs will continue if they are taken seriously for safety You have to make sure you're aware you're trying to kill jobs that you forgot you've stopped The second time you use the exit command the jobs are terminated and the shell is shutdown This might cause problems for some programs that are not intended to be killed in this way

In bash it seems you can use the logout command which will kill stopped processes and exit. This may cause unwelcome results

Some programs may not exit when terminated in this way and your system could end up with a lot of orphaned processes using up resources if you make a habit of it

Note that you can create background processes that stop if they require user input

user@mysystem:~$ python &
[1] 19028
user@mysystem:~$ jobs
[1]+  Stopped                 python

You can resume and kill these jobs in the same way you did jobs that you stopped with the Ctrl-z interrupt.