I have stopped jobs?
You hit Ctrl-D (^D) to close a terminal window, a messages pops up, “There are stopped jobs.”, you hit ^D again, gulp some espresso, the terminal fades away.
What did that message mean? Why did it stop the window from closing the first time?
The Simple Answer
A Job (process) can run in the foreground (fg) or in the background (bg), which if there are, you get told this to give you a chance to end those background jobs gracefully.
You can run a job in the background by adding “&” to the command. You can send the current running app to the background with Ctrl-Z (^Z).
Use ‘jobs’ to list all jobs, and ‘fg’ to switch jobs: either by its job number, or the last job you worked on (if no job number is given).
Some Easy Examples
Try these out with me, it’s a great way to learn! (you can use pico instead of vi)
# edit our shopping
$ vi shopping
- buy ground espresso
~
# send vi to the background (Press Ctrl-Z)
[1]+ Stopped vi shopping
# I need to look at the ingredients for sorbet
$ vi recipes
# our recipe says we need peaches, great. We ^Z this one to the background too
[2]+ Stopped vi recipes
# we now have 2 stopped jobs
$ jobs
[1]- Stopped vi shopping
[2]+ Stopped vi recipes
# switch back to shopping (job 1)
$ fg 1
# complete our shopping list
- buy ground espresso
- buy peaches
~
We have two vi’s running, and switch between them, in the same terminal. This seems arb, since you could just open another terminal window. But this method of managing jobs was a great way to multitask back when X Windows wasn’t always available.
You can see how this is handy in a ssh session: Busy editing your firewall config, you have to check the mac address of your second network card. No need to discard your half-edited file. No problem!
So when you ‘exit’ a session, it tells you that “there are stopped jobs”, just in case you forgot to save your shopping list. If you ignore the message and ‘exit’ a second time, the shell will kill those jobs for you.
For more on this, try ‘man jobs’ and see the entries for jobs, fg and bg
*Pro Tip:* ^Z in a ssh session will background the remote process, while ~^Z (Tilde-Ctrl-Z) will background the ssh session itself. Tilde (~) is the ssh escape character: keystrokes following it apply to the local session.

May 17th, 2011
Wez
Posted in 
