247

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 is some real-world terminal output:

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.
>>> 

ctrl+z

[1]+  Stopped                 python
example_user@example_server:~$ exit
logout
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)
1
  • 1
    ctrl+shift+\ will also make python exit. Commented Apr 6, 2014 at 9:56

2 Answers 2

324

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 that job is not attached to the current terminal, it cannot produce output and is not receiving 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
user@mysystem:~$ 

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
python

At this point you are back in the python interpreter and may exit by using control-D.

Conversely, you may kill the command with either it's jobspec or PID. For instance:

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
user@mysystem:~$ 

To use the jobspec, precede the number with the percent (%) key:

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

If you issue an exit command with stopped jobs, the warning you saw will be given. The jobs will be left running for safety. That's to make sure you are aware you are attempting to kill jobs you might have forgotten you stopped. The second time you use the exit command the jobs are terminated and the shell exits. This may cause problems for some programs that aren't intended to be killed in this fashion.

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

Also note that 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 doing that.

Note that you can create background process that will 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.

3
  • 5
    I would quibble with your second sentence. It should say, “Because the job is stopped, it cannot take any action – file I/O, network I/O, terminal I/O, or any other processing.” Commented Aug 15, 2014 at 16:23
  • 2
    This helps me a lot.
    – shellbye
    Commented Aug 1, 2015 at 2:26
  • Excellent post great help to any one! Commented Dec 24, 2017 at 22:15
25

Q: What is a 'stopped job', or what does this mean?

Stopped job means process which received stop signal (SIGSTOP/SIGTSTP) from keyboard (suspend character Ctrl-Z), command (e.g. kill -STOP [PID]) or another process (e.g. kernel when system is lack on resources) and it is in paused state, so it tells system to stop/suspend a process so it won't do any execution/processing.

Active shell jobs can be listed by: jobs.

Q: Can a stopped process be resumed?

The stopped process will only resume its execution if it is sent the SIGCONT signal. This can be achieved by either by fg (or fg ID) which will move job to the foreground making it the current job, bg to continue it in the background, or by sending SIGCONT signal (e.g. kill -CONT [PID]).

Q: Does the first exit kill the stopped jobs?

First time when you type exit/logout or press Ctrl-D, the shell prints a warning message about current active jobs that are associated with your terminal, therefore it won't kill you those without your permission by confirming the action second time. If the checkjobs option is enabled (shopt -s checkjobs), it can also lists the jobs with their statuses.

Q: Is there a way to exit the shell the first time? (without entering exit twice)

You can press Ctrl+D twice or hold it for longer, this will exit shell quiet quickly killing the current stopped/running shel jobs.

Alternatively disown them (disown) to leave them or kill them manually: kill $(jobs -p).

1
  • Note that SIGTTIN can also stop a process.
    – Alex D
    Commented Dec 23, 2020 at 6:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .