Linux: Job Control

By Xah Lee. Date: . Last updated: .

This is a tutorial on unix “job control”.

The following are part of bash, not separate utils, so there's no man page for them. They are documented in man bash. [see Linux: Bash Manual in Chapters, Using Emacs for Info]

cmd &Start a program in background. For example, ruby process_log.rb &
Ctrl+cStop the current program associated with your terminal. (sending SIGINT to it)
Ctrl+zSuspend the current program associated with your terminal. (sending SIGTSTP to it)
jobsList background processes
bg %numberRun the suspended command in background.
fg %numberResume a background process to foreground.
disown %numberSeparate a job ID number from jobs table.

unix process signal is a integer send to programs to tell it something. To see a list of all signals, type man kill.

Example of Using Job Control

Here's a common scenario of using job control:

  1. Start xlock with a second hand by xclock -update 1, it'll hog the terminal.
  2. Now, in terminal, press 【Ctrl+z】 to pause the program and returns you the prompt. (when the program is paused, it can't be used. You'll see that the second-hand stopped moving.)
  3. Type jobs to list all jobs and their ID in job table. Here's a sample output: [1]+ Stopped xclock -update 1
  4. Type bg %1 to start the process with job ID 1 in background.
  5. Now, it is as if you started xclock by xclock &.

the “xlock” example above can be any shell command.

Here's some other commands that are very useful. Note, many GUI apps these days will detach itself from terminal, even if you didn't start it with &.

setsid commandRun a program in a new session.
nohup commandRun a command immune to SIGHUP signal , and redirect stdout to a normal file.

Closing Terminal Kills My Program?


To run a server, you can do: nohup myserver > out.txt 2> err.txt < /dev/null &.

This will set stdout to out.txt, and stderr to err.txt, and stdin from /dev/null, and not receive signal SIGHUP signal, and remove this process from jobs table.

Sys Admin

  1. Job Control
  2. ps
  3. top
  4. htop
  5. RAM stat
  6. Users and Groups
  7. File Permission
  8. Opened Files: lsof
  9. shutdown, sleep

If you have a question, put $5 at patreon and message me.