Linux provides various commands to manage and monitor processes efficiently. This guide covers the basic commands for running, viewing, and managing processes in Linux, making it easier to understand for beginners.
Background Command:
- Run a command in the background so you can continue using the terminal for other tasks:
The
ping 8.8.8.8 &&at the end sends the command to the background, freeing up the terminal.
Foreground Command:
- Run a command in the foreground:
This keeps the terminal occupied until the command completes.
ping 8.8.8.8
Move a Background Job to the Foreground:
- Bring a background job to the foreground:
This moves the most recent background job to the foreground.
fg
List Jobs Running in the Background:
- Check which jobs are running in the background:
This shows a list of jobs that are currently running in the background.
jobs
Redirect Command Output to the Background:
- Send a command’s output to the background:
The
seq 10000000000000 >/dev/null & ping 8.8.8.8 >/dev/null &
>/dev/nullpart sends the output to a special location that discards it, and the&runs it in the background.
View Current Shell's Running Processes:
- See the processes running in your current shell:
ps
List Processes in Long Format:
- Get a detailed list of processes:
ps -l
View All Running Processes (Including CPU and Memory Usage):
- See a list of all running processes with CPU and memory usage:
ps -aux
List All Processes:
- List all processes running on the system:
ps -e
Display Processes in a Tree Structure:
- Show processes in a hierarchical tree format:
ps -axjf
Filter Processes by User (e.g., root):
- See processes owned by a specific user, like
root:ps -U root -u root u
Show Processes for a Specific User:
- Display processes owned by any specific user (e.g., 'name'):
ps -U user -u user u
View Real-Time System Processes:
- Monitor active system processes:
This command shows real-time process activity on your system.
top
Install htop for Better Monitoring:
htopprovides a more user-friendly interface to monitor processes:- For Debian/Ubuntu:
sudo apt install htop
- For RHEL/CentOS:
sudo yum install htop
- For Debian/Ubuntu:
Display Processes in a Tree Structure with htop:
- See processes displayed as a tree:
htop -t
Get More Detailed Information with htop:
htopshows more detailed process information compared totop.
Monitor Processes for a Specific User:
- View processes for a particular user (e.g., 'name'):
htop -t -u name
Monitor Processes by Process ID (e.g., 2780):
- View processes using a specific process ID:
htop -t -u 2780
Terminate a Process by ID:
- Kill a process using its process ID (PID):
kill 2780
Forcefully Kill a Process:
- Forcefully terminate a process if it's not responding:
kill -9 1891
Kill All Instances of a Process (e.g., seq):
- Terminate all processes of a certain type (e.g.,
seq):- First, install the necessary tool:
sudo yum install psmisc
- Then kill all processes with the name
seq:killall seq
- First, install the necessary tool:
Kill Processes by Name Using Regular Expressions:
- Terminate all processes matching a specific name (e.g.,
firefox):killall -r firefox
Kill All Processes for a Specific User:
- Terminate all processes belonging to a specific user:
killall -u user
Terminate All ping Processes Using pkill:
- Use
pkillto stop allpingprocesses:pkill ping
Kill All Processes for a Specific User (e.g., armour):
- Terminate all processes for a given user:
pkill -u user
Kill Only the ping Process for a Specific User:
- Terminate only the
pingprocess for a particular user (e.g.,armour):pkill -u user -x ping
This guide covers the essential commands for managing processes in Linux, from running tasks in the background to terminating processes when necessary. By understanding these basic commands, you can efficiently monitor and control system processes.