Skip to content

Background job management

Background job management refers to the suite of techniques used in shell environments to control the execution state of processes, allowing tasks to run non-interactively or to be paused and resumed later.^[600-developer-linux-centos7-command.md]

Core Commands

The & operator is used at the end of a command line to execute the process in the background immediately.^[600-developer-linux-centos7-command.md] To manage these processes, the jobs command lists all background tasks associated with the current terminal, while jobs -l provides the list along with their corresponding Process IDs (PIDs).^[600-developer-linux-centos7-command.md]

Process State Control

It is possible to move processes between the foreground and background or suspend their execution entirely. Pressing Ctrl + Z suspends a currently running foreground task and places it in the background in a stopped state.^[600-developer-linux-centos7-command.md]

  • bg %num: Resumes execution of a suspended background task (identified by its job number).^[600-developer-linux-centos7-command.md]
  • fg %num: Brings a background task to the foreground to interact with it directly.^[600-developer-linux-centos7-command.md]

Persistence

By default, background processes may terminate when the user logs out or the session ends. To ensure a program continues running after the user logs out, output must be redirected, and the execution must be decoupled from the session.^[600-developer-linux-centos7-command.md]

The standard method to achieve this is using nohup (no hang up) combined with output redirection and the background operator:

nohup /path/my_program > my.out 2> my.err &
^[600-developer-linux-centos7-command.md]

Sources

  • 600-developer-linux-centos7-command.md
  • [[Linux Administration]]
  • [[Process Management]]
  • [[Shell Scripting]]
  • [[SSH]]