Running Processes in the Background
This post explains how to run and manage background processes in Linux. It covers starting commands with &, suspending and resuming jobs with Ctrl+Z and bg/fg, and keeping processes alive after logout with nohup.
When you run a command in the Linux terminal, it normally takes over your session until it finishes. That works fine for quick tasks, but what happens when you need to run something that takes a while, like a file transfer, a log monitor, or a long-running script? That is where background processes come in.
This post covers the core techniques for managing foreground and background processes in Linux, so you can keep working without waiting around.
Foreground vs Background Processes
By default, every command you run in the terminal runs in the foreground. Your shell waits for it to finish before giving you a new prompt. A background process runs independently, freeing your terminal so you can continue working.
Think of it like putting a task on hold so you can do something else, while that task keeps running quietly in the background.
Starting a Process in the Background
The simplest way to run a command in the background is to add an ampersand (&) at the end of it.
sleep 60 &Your terminal will respond with something like:
[1] 4732The number in brackets is the job number, and the second number is the process ID (PID). Both are useful for managing the process later.
Suspending and Backgrounding a Running Process
If you already started a command in the foreground and want to move it to the background, you can do it in two steps:
- Press
Ctrl + Zto suspend (pause) the running process. - Type
bgto resume it in the background.
$ ping google.com
PING google.com (142.250.70.78): 56 data bytes
^Z
[1]+ Stopped ping google.com
$ bg
[1]+ ping google.com &The process is now running in the background, and your shell prompt is free again.
Listing Background Jobs
Use the jobs command to see all processes running in the background for your current shell session.
$ jobs
[1]- Running sleep 60 &
[2]+ Running ping google.com &The + sign indicates the most recent job. The - sign indicates the job before that.
Bringing a Process Back to the Foreground
To bring a background job back to the foreground, use the fg command followed by the job number.
fg %1If you only have one background job, just typing fg will bring it forward automatically.
Keeping Processes Running After You Log Out
Background processes started with & are tied to your current shell session. If you close your terminal or log out, they will stop. To prevent this, use nohup.
nohup python3 my_script.py &The nohup command (short for "no hangup") tells the process to keep running even after you disconnect. Output is saved to a file called nohup.out by default.
For more control over long-running processes, tools like screen and tmux are worth learning. They let you create persistent terminal sessions you can detach from and reconnect to later. Those tools deserve their own post, so we will cover them separately.
Killing a Background Process
If you need to stop a background process, you can use kill with the PID:
kill 4732Or you can kill a job by its job number:
kill %1Quick Reference
command &: Run a command in the background from the start.Ctrl + Z: Suspend a running foreground process.bg: Resume a suspended process in the background.fg %N: Bring job number N back to the foreground.jobs: List all background jobs in the current session.nohup command &: Run a command that survives logout.kill %N: Kill background job number N.
What's Next
Now that you know how to run and manage background processes, the next step is learning how to inspect what is actually happening on your system. In the next post, we will look at process monitoring with ps and top, two essential tools for seeing which processes are running, how much CPU and memory they are using, and how to find and control them. These skills pair perfectly with what you just learned here.