Skip to main content

Working with Job Control

1. Running Background Jobs

Command Description Example
bg Resume a stopped job in the background bg %1
jobs List all jobs in the current session jobs -l
kill Send a signal to a job (default is TERM) kill %1
nohup Run a command immune to hangups nohup long-running-command &
wait Wait for a job to complete before continuing wait %2

Example: Starting a Background Job

sleep 300 &
jobs

Example: Resuming a Stopped Job in Background

sleep 300
# Press Ctrl+Z to suspend
bg %1

2. Suspending Foreground Jobs

Command Description Usage
Ctrl+Z Suspends the currently running foreground job Pressing Ctrl+Z sends a SIGTSTP signal to the foreground process, pausing it.
jobs Lists all suspended and running jobs jobs shows the status of all background and suspended jobs in the current shell session.
bg Resumes a suspended job in the background bg %1 resumes job number 1 in the background.
fg Resumes a suspended job in the foreground fg %1 brings job number 1 back to the foreground.

Example: Suspending and Resuming a Job

$ sleep 300
^Z
[1]+  Stopped        sleep 300
$ jobs
[1]+  Stopped        sleep 300
$ bg %1
[1]+ sleep 300 &
$ fg %1
sleep 300
^C
[1]+  Interrupted    sleep 300

3. Listing Jobs

Command Description Notes
jobs List currently running and stopped jobs Default output includes job number, status, and command
jobs -l List jobs with additional details Shows process IDs (PIDs) and job state information
jobs -r List only running jobs Filters out stopped or suspended jobs
jobs -s List only stopped jobs Filters out running jobs
jobs -n List jobs that have changed status since last notification Useful for tracking job state transitions

Example: Using jobs command

$ sleep 100 &
[1] 12345
$ jobs
[1]+ Running sleep 100
$ sleep 200 &
[2] 12346
$ jobs -l
[1]+ 12345 Running sleep 100
[2]- 12346 Running sleep 200
Note: Job numbers are assigned sequentially and can change when jobs are added or removed.

4. Resuming Jobs in Foreground

Command Description Usage
fg Brings the most recent background job to the foreground fg
fg %1 Brings job number 1 to the foreground fg %1
fg job_name Brings a named job to the foreground fg my_job

Example: Resuming a Background Job

sleep 100 &
[1] 12345
fg %1
Note: The fg command resumes the job in the foreground, allowing it to interact with the terminal directly.

5. Resuming Jobs in Background

Command Description Usage
bg Resumes suspended jobs in the background bg %1
jobs Lists current jobs jobs -l

Example: Resuming a suspended job in background

sleep 100
# Press Ctrl+Z to suspend the job
bg %1

6. Using nohup for Persistent Processes

The nohup command allows you to run processes that will continue running even after you log out of the shell. It stands for "no hang up" and is particularly useful for long-running tasks.

Command Description Example
nohup Run a command immune to hangups nohup ./long_running_script.sh &

Example: Running a Script with nohup

nohup python3 /home/user/data_processing.py &</code>
</pre>
</div>
</div>
<p>By default, <code>nohup</code> redirects the standard output and standard error to a file named <code>nohup.out</code> in the current directory. You can customize this behavior using redirection.</p>
<div class="example">
<h2 id="example-redirecting-output">Example: Redirecting Output<a href="#example-redirecting-output" class="toc__headingLink" aria-label="Link to this section"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<div class="codeBlock" data-language="bash">
<pre><code>nohup ./my_script.sh > output.log 2>&1 &</code>
</pre>
</div>
</div>
<p>When you run a command with <code>nohup</code>, it will continue running even if the terminal session ends. The <code>&</code> at the end places the process in the background.</p>
<div class="note">
<strong>Note:</strong> The <code>nohup</code> command does not prevent a process from being killed by the system if it runs out of memory or is otherwise terminated.
</div>
<h2 id="7-disowning-jobs">7. Disowning Jobs<a href="#7-disowning-jobs" class="toc__headingLink" aria-label="Link to this section"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<table>
<tbody>
<tr>
<th>Command</th>
<th>Description</th>
<th>Usage</th>
</tr>
<tr>
<td class="property">disown</td>
<td>Remove jobs from the shell's job table</td>
<td><code>disown <job_spec></code></td>
</tr>
<tr>
<td class="property">disown -h</td>
<td>Disown jobs and prevent them from being signaled on shell exit</td>
<td><code>disown -h <job_spec></code></td>
</tr>
<tr>
<td class="property">disown -a</td>
<td>Disown all jobs</td>
<td><code>disown -a</code></td>
</tr>
</tbody>
</table>
<div class="example">
<h2 id="example-disown-a-background-job">Example: Disown a background job<a href="#example-disown-a-background-job" class="toc__headingLink" aria-label="Link to this section"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<div class="codeBlock" data-language="bash">
<pre><code>sleep 100 &
jobs
[1]+ Running sleep 100 &
disown %1
jobs
# No output, job is disowned
Note: Disowned jobs will not receive SIGHUP when the shell exits, but they will still be subject to other signals.

8. Using screen for Terminal Multiplexing

screen is a terminal multiplexer that allows you to manage multiple terminal sessions within a single window. It's particularly useful for running long-running processes or working with multiple shells simultaneously.

Command Description Usage Example
screen Starts a new screen session screen
screen -S <session_name> Creates a named screen session screen -S mysession
screen -ls List all active screen sessions screen -ls
screen -r <session_name> Reattach to a named screen session screen -r mysession
screen -d -r <session_name> Detaches and reattaches to a session screen -d -r mysession

Example: Starting and Managing a Screen Session

screen -S webserver
# This creates a new screen session named "webserver"

# Inside the screen session, run:
npm start

# Detach from the session (Ctrl+A, then D)
# Reattach later:
screen -r webserver
Note: By default, screen uses Ctrl+A as the escape key. You can change this by modifying ~/.screenrc.

Common Screen Key Bindings

  • Ctrl+A, D – Detach from session
  • Ctrl+A, K – Kill current window
  • Ctrl+A, C – Create new window
  • Ctrl+A, N – Switch to next window
  • Ctrl+A, P – Switch to previous window
  • Ctrl+A, ? – Show help

9. Managing screen Sessions

Example: Starting a screen session

screen -S mysession

Example: Attaching to a screen session

screen -r mysession
Command Description Flags
screen -S <session_name> Starts a new screen session Creates a named session
screen -r <session_name> Reattaches to an existing session Can also use session ID
screen -ls List all active screen sessions Shows session IDs and names
screen -d -r <session_name> Detaches and reattaches to a session Useful for reconnecting to detached sessions
Note: Use screen -r to reattach to a session, and screen -d to detach from it.
Warning: If a session is already attached, screen -r will fail unless you use -d -r.

Common screen Session Management Commands

  • screen -S session_name: Create a new session
  • screen -ls: List all sessions
  • screen -r session_name: Reattach to a session
  • screen -d -r session_name: Detach and reattach to a session

10. Using tmux for Terminal Multiplexing

tmux is a terminal multiplexer that allows you to manage multiple terminal sessions within a single window. It's particularly useful for long-running processes, remote server management, and organizing your terminal workflow.

Command Description Example
tmux new-session Starts a new tmux session tmux new-session -s mysession
tmux attach Reattach to an existing session tmux attach -t mysession
tmux list-sessions List all active tmux sessions tmux list-sessions
tmux kill-session Terminate a session tmux kill-session -t mysession

Example: Creating and Managing a tmux Session

tmux new-session -s "dev"
tmux split-window -h
tmux send-keys "ls -la" Enter
Note: By default, tmux uses Ctrl-b as the prefix key. You can change this in your ~/.tmux.conf file.
Key Combination Action Description
Ctrl-b d Detach session Leave the current session running in the background
Ctrl-b s List sessions Show all active sessions
Ctrl-b " Split window horizontally Create a new pane in the current window
Ctrl-b | Split window vertically Create a new pane in the current window
Ctrl-b o Navigate panes Cycle through panes in the current window
Ctrl-b x Close pane Close the current pane

Common tmux Use Cases

  • Running long processes without keeping the terminal open
  • Organizing multiple terminal tasks in one window
  • Working on remote servers with persistent sessions

11. Managing tmux Sessions

Command Description Usage
tmux new-session Create a new tmux session tmux new-session -s mysession
tmux attach Attach to an existing session tmux attach -t mysession
tmux list-sessions List all active tmux sessions tmux list-sessions
tmux kill-session Terminate a session tmux kill-session -t mysession
tmux detach Detach from current session (in client) tmux detach

Example: Creating and Attaching to a Session

tmux new-session -s webdev
tmux attach -t webdev

Example: Listing and Killing Sessions

tmux list-sessions
tmux kill-session -t webdev

12. Using tmux Panes and Windows

Example: Creating a new tmux window

tmux new-window -n "myapp"
Command Description Usage
tmux new-window Create a new window tmux new-window -n "WindowName"
tmux select-window Select a window by index or name tmux select-window -t 1
tmux split-window Split current pane into two panes tmux split-window -h
tmux attach Attach to a session tmux attach -t <session_name>
Note: Use tmux list-windows to see all windows in a session.

Example: Splitting panes horizontally

tmux split-window -h

Example: Navigating between panes

tmux select-pane -L  # Select left pane
tmux select-pane -R  # Select right pane

Key tmux Window and Pane Commands

  • new-window -n "name": Create a new window with a name
  • split-window -h: Split pane horizontally
  • split-window -v: Split pane vertically
  • select-pane -L/R/U/D: Navigate panes
  • list-windows: List all windows in session

Was this article helpful?