0
0
Linux-cliHow-ToBeginner · 3 min read

How to Run a Process in Background in Linux Easily

To run a process in the background in Linux, add & at the end of the command. For example, sleep 30 & runs the sleep command in the background, letting you continue using the terminal.
📐

Syntax

To run a process in the background, append & to the command. This tells the shell to start the process and immediately return control to you.

Example syntax:

  • command & - Runs command in the background.
  • nohup command & - Runs command immune to hangups, useful for long-running tasks.
bash
command &
💻

Example

This example runs the sleep 10 command in the background. You can keep using the terminal while it waits silently.

bash
sleep 10 &

# Output shows the job number and process ID, then returns prompt immediately.
Output
[1] 12345 user@host:~$
⚠️

Common Pitfalls

One common mistake is forgetting the &, which runs the process in the foreground and blocks the terminal.

Another issue is that background processes may stop if the terminal closes. Use nohup or disown to keep them running.

bash
sleep 10
# This blocks the terminal until sleep finishes

nohup sleep 10 &
# Runs sleep immune to hangups, output goes to nohup.out

# To detach a running job:
# 1. Press Ctrl+Z to pause
# 2. Run 'bg' to resume in background
# 3. Run 'disown' to detach from terminal
📊

Quick Reference

CommandDescription
command &Run command in background
nohup command &Run command immune to hangups
jobsList background jobs
fg %job_numberBring job to foreground
bg %job_numberResume paused job in background
disown %job_numberRemove job from shell's job list

Key Takeaways

Add & at the end of a command to run it in the background.
Use nohup to keep processes running after terminal closes.
Use jobs, fg, bg, and disown to manage background processes.
Forgetting & runs the process in foreground and blocks the terminal.
Detach running jobs with disown to prevent termination on logout.