Processes are the running programs on your Unix system, and managing them is key to understanding how your system operates. Unix provides various commands to monitor, control, and optimize processes.
In this blog, we’ll explore how to view processes, terminate them, and manage their priority using clear examples.
What Is a Process?
A process is an instance of a running program. Unix assigns each process a unique Process ID (PID) to identify it. Processes can be:
- Foreground: Interacting directly with the user.
- Background: Running silently in the background.
1. Viewing Processes
Use the ps
Command
The ps
command lists processes running on the system.
Example:
$ ps
This shows processes running in your current terminal session.
See All Processes
Use the -e
option to display all processes on the system:
$ ps -e
Add the -f
option for detailed output (full format):
$ ps -ef
Use the top
Command
The top
command provides a real-time view of running processes, showing their CPU and memory usage.
Example:
$ top
Press q
to exit the top
view.
Use the htop
Command
The htop
command (if installed) offers an interactive, colorful view of processes, making it easier to manage them.
Example:
$ htop
2. Killing Processes
If a process is unresponsive or consuming too many resources, you can terminate it.
Kill a Process by PID
Use the kill
command followed by the process’s PID:
$ kill 1234
Forcefully Kill a Process
If a process doesn’t terminate with kill
, use the -9
option:
$ kill -9 1234
Kill by Name with pkill
The pkill
command lets you kill processes by name:
$ pkill firefox
Kill Multiple Processes
You can combine commands like ps
and grep
with xargs
to kill multiple processes.
Example:
$ ps -ef | grep "python" | awk '{print $2}' | xargs kill
This kills all Python processes.
3. Background and Foreground Processes
Run a Process in the Background
Add &
at the end of a command to run it in the background:
$ sleep 100 &
View Background Jobs
Use the jobs
command to list background processes:
$ jobs
Bring a Process to the Foreground
Use the fg
command followed by the job number:
$ fg %1
Pause a Process
Press Ctrl + Z
to pause a running process and move it to the background.
4. Managing Process Priority
View Process Priority
The top
command shows process priorities in the PR and NI columns:
- PR: Process priority.
- NI: Niceness value (ranges from -20 to +19, with lower values being higher priority).
Change Process Priority: renice
The renice
command adjusts the priority of a running process.
Example:
- Increase priority:
$ sudo renice -5 -p 1234
- Decrease priority:
$ sudo renice 10 -p 1234
Practice Time!
- List all processes on your system:
$ ps -ef
- Kill a process named “chrome”:
$ pkill chrome
- Run a process in the background and then bring it to the foreground:
$ sleep 100 &
$ jobs
$ fg %1
Summary
- Use
ps
,top
, orhtop
to monitor processes. - Use
kill
andpkill
to terminate processes by PID or name. - Manage process priority with
renice
. - Use
jobs
andfg
to control background and foreground processes.