Stopping a background process in Linux requires using the kill command with its job number. If you want to learn how to kill background process in linux effectively, this guide covers everything from basic commands to advanced signals. You will find step-by-step instructions, practical examples, and common troubleshooting tips.
Background processes are tasks that run independently in the terminal. They let you continue working while a command executes. But sometimes you need to stop them—maybe they hang, consume too many resources, or you simply made a mistake. Knowing how to kill background process in linux is essential for any user.
This article assumes you have basic terminal experience. You should know how to open a terminal and run simple commands. Let’s start with the fundamentals.
What Is A Background Process In Linux
A background process runs without blocking your terminal. When you start a command with an ampersand (&) at the end, it runs in the background. For example:
sleep 300 &
This starts a 5-minute sleep in the background. You can still type other commands. The shell assigns a job number (like [1]) and a process ID (PID).
Background processes are useful for long-running tasks like file downloads or data processing. But they can become problematic if they misbehave. That is when you need to kill them.
How To Kill Background Process In Linux
The primary tool for killing background processes is the kill command. It sends a signal to a process, telling it to stop. The most common signal is SIGTERM (signal 15), which asks the process to terminate gracefully.
If a process ignores SIGTERM, you can use SIGKILL (signal 9). This forces the process to stop immediately. However, it may leave data in an inconsistent state.
Finding The Process ID Or Job Number
Before you kill a process, you need to identify it. There are two main ways:
- Use
jobsto list background jobs in the current terminal session. - Use
psortopto find the PID of any running process.
For background jobs started in your current terminal, jobs is simplest:
$ sleep 300 &
[1] 12345
$ jobs
[1]+ Running sleep 300 &
The number in brackets (like [1]) is the job number. The number after (12345) is the PID.
Using Kill With Job Number
To kill a background job by its job number, use:
kill %1
The percent sign (%) tells the shell you are refering to a job number. Replace 1 with the actual job number. This sends SIGTERM by default.
If the job does not stop, use:
kill -9 %1
This sends SIGKILL. Be careful—this does not give the process a chance to clean up.
Using Kill With Process ID
You can also kill a process by its PID. First find the PID with ps:
ps aux | grep sleep
This shows all processes named “sleep”. The second column is the PID. Then run:
kill 12345
Or for a forced kill:
kill -9 12345
Using Pkill And Killall
For convenience, you can use pkill or killall. These kill processes by name:
pkill sleep
This kills all processes named “sleep”. killall works similarly but is more specific:
killall sleep
Be careful with these commands—they can kill multiple processes at once.
Common Signals And Their Meanings
Signals are how Linux communicates with processes. Here are the most useful ones:
- SIGTERM (15): Requests graceful termination. The process can clean up resources.
- SIGKILL (9): Forces immediate termination. The process cannot ignore this.
- SIGINT (2): Interrupts the process, like pressing Ctrl+C.
- SIGHUP (1): Hang up signal, often used to reload configuration.
- SIGSTOP (19): Pauses the process. It can be resumed later.
- SIGCONT (18): Resumes a paused process.
Most of the time, you will use SIGTERM or SIGKILL. Always try SIGTERM first.
Stopping Background Processes With Fg And Ctrl+C
If you have a background job that you want to stop interactively, you can bring it to the foreground first:
fg %1
This brings job number 1 to the foreground. Then press Ctrl+C to send SIGINT. This is often safer than using kill -9.
Alternatively, you can use bg to resume a stopped background job. But for killing, fg then Ctrl+C is clean.
Killing Multiple Background Processes
Sometimes you need to kill several processes at once. Here are a few methods:
- Use
pkillwith a pattern:pkill -f "python script.py" - Use
killallwith the process name:killall python - Use a loop with
kill:for pid in $(pgrep -f "pattern"); do kill $pid; done
Be careful with pattern matching—you might kill unintended processes.
Handling Stuck Or Zombie Processes
Sometimes a process becomes unresponsive. It may show as “defunct” or “zombie” in ps output. Zombie processes are already dead but remain in the process table because the parent has not read their exit status.
You cannot kill a zombie with kill. Instead, you must kill its parent process. Use ps -eo pid,ppid,stat,cmd to find the parent PID. Then kill the parent:
kill -9 1234
If the parent is init (PID 1), you may need to reboot. Zombie processes are rare but annoying.
Using Top And Htop To Manage Processes
For a visual overview, use top or htop. These tools show running processes in real time. In top, press ‘k’ and enter the PID to kill a process. In htop, navigate to the process and press F9 to send a signal.
These tools are helpful when you do not know the exact PID or name. They also show resource usage, so you can identify problematic processes.
Automating Process Killing With Scripts
If you regularly kill the same processes, write a script. For example, create a file called kill_sleep.sh:
#!/bin/bash
pkill sleep
echo "All sleep processes killed."
Make it executable with chmod +x kill_sleep.sh. Then run it whenever needed.
You can also use cron to kill processes automatically. But be cautious—automated killing can cause data loss.
Preventing Background Processes From Running Away
To avoid needing to kill background processes, set limits. Use ulimit to restrict CPU time or memory. For example:
ulimit -t 30
This limits CPU time to 30 seconds per process. If a process exceeds this, it receives SIGXCPU.
You can also use nice to lower a process’s priority. This prevents it from hogging resources.
Common Mistakes And How To Avoid Them
Here are frequent errors when killing background processes:
- Killing the wrong process: Always double-check the PID or job number.
- Using SIGKILL too soon: Try SIGTERM first to allow clean shutdown.
- Forgetting the percent sign:
kill 1kills PID 1, not job 1. Usekill %1. - Killing system processes: Avoid killing processes owned by root unless you are sure.
- Not checking for child processes: Killing a parent may leave orphans.
Always verify with ps or jobs after killing.
Real-World Example: Killing A Stuck Download
Suppose you start a large download in the background:
wget https://example.com/bigfile.zip &
After a while, it seems stuck. First, check the job:
jobs
If it shows [1]+ Running, try:
kill %1
Check again. If it still runs, use:
kill -9 %1
Then verify it is gone:
ps aux | grep wget
No output means success.
When To Use Kill -9 Versus Other Signals
SIGKILL (kill -9) should be a last resort. It does not allow the process to close files, release memory, or save state. This can lead to corrupted data or resource leaks.
Use SIGTERM first. If the process does not respond after a few seconds, then escalate to SIGKILL. Some processes, like daemons, may need SIGTERM followed by a delay.
For interactive programs, Ctrl+C (SIGINT) is often better because it allows the program to handle the interrupt gracefully.
Understanding Process Groups And Sessions
Background jobs belong to process groups. When you kill a job, you kill the entire group. This is why kill %1 stops all processes in that job.
If you start multiple commands in a pipeline, they share the same process group. Killing the group stops all of them.
This is useful for complex tasks. For example, if you run tar -cf archive.tar large_folder/ | gzip &, killing the job stops both tar and gzip.
Using The Disown Command
Sometimes you want to remove a background job from the shell’s job table without killing it. Use disown:
disown %1
This detaches the process. It continues running but is no longer managed by the shell. You can then close the terminal without killing it.
To kill a disowned process, you must use its PID. You cannot use job numbers after disowning.
Killing Processes By User
If you need to kill all processes owned by a specific user, use:
pkill -u username
This kills every process owned by that user. Use with extreme caution—it logs them out and may cause data loss.
For a safer approach, list the processes first:
ps -u username
Then kill individually.
Frequently Asked Questions
What is the difference between kill and pkill?
kill requires a PID or job number. pkill matches process names or patterns. Use kill for specific processes, pkill for groups.
Can I kill a background process without knowing its PID?
Yes, use jobs to find the job number, then kill %number. Or use pkill with the process name.
What happens if I kill a background process that is writing a file?
The file may be incomplete or corrupted. Always try to stop the process gracefully first.
How do I kill a background process started by another user?
You need root privileges. Use sudo kill PID or sudo pkill -u username.
Why does my background process not die after kill -9?
It might be a zombie process. Check with ps aux | grep Z. Kill its parent process instead.
Summary
Knowing how to kill background process in linux is a fundamental skill. You learned to use kill with job numbers and PIDs, plus pkill and killall for convenience. Always start with SIGTERM and escalate to SIGKILL only when needed.
Practice these commands in a safe environment. Create test processes with sleep and experiment. Over time, you will become confident in managing background tasks.
Remember to check for zombie processes and avoid killing system processes. With these techniques, you can keep your system running smoothly.