This guide covers techniques for diagnosing and managing processes in Linux, including commands like ps, top, nice, and lsof.
In this guide, you will learn how Linux handles processes and discover various techniques to diagnose and manage them effectively. Every command you execute creates a process that runs until it completes its task or is manually terminated. For instance, running a command like “ls” creates a short-lived process that displays the directory contents and then exits, while services like the SSH daemon run continuously in the background.Below is an example that lists all files—including hidden ones—using the ls command:
The ps command is a fundamental utility for inspecting active processes. It has several option syntaxes, reflecting both Unix and BSD styles. For example, ps -a uses Unix-style options, while ps -A (uppercase A) follows BSD syntax, leading to different outputs.Running ps without any options only displays processes associated with the current terminal session. To see all processes running on the system, combine the options a, x, and u:
Copy
Ask AI
jeremy@kodekloud:~$ ps aux
In this command:
The ax options ensure that processes from all controlling terminals are listed.
The u option presents a user-oriented format, adding columns for memory and CPU usage as well as the process owner. This is why ps aux is widely used for obtaining a complete snapshot of system processes.
Review the typical columns shown in the ps aux output:
Column
Description
%CPU
Percentage of one CPU core’s capacity used by the process. A value of 150 may indicate one full core plus 50% of a second core.
%MEM
Percentage of the system’s total memory used by the process.
START
The time or date when the process was initiated.
TIME
Total CPU time consumed by the process. Processes with long lifespans often spend the majority of their time sleeping.
COMMAND
The command that initiated the process, along with its parameters.
Processes displayed within square brackets (e.g., [kthreadd]) are kernel processes operating in privileged areas and usually do not require user interaction. Only processes outside these brackets represent user-space applications.
While ps gives you a static snapshot, the top command is ideal for real-time process monitoring. It continuously updates its display and reorders the processes by CPU usage, making it easier to identify resource-intensive processes.Use the arrow keys to navigate the list, and press “Q” to exit. Below is an example output of the top command:
Copy
Ask AI
top - 18:13:36 up 3:05, 2 users, load average: 0.05, 0.04, 0.00Tasks: 215 total, 1 running, 214 sleeping, 0 stopped, 0 zombie%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni, 99.3 id, 0.0 wa, 0.0 hi, 0.7 si, 0.0 stMiB Mem : 7939.4 total, 7303.2 free, 546.3 used, 325.0 buff/cacheMiB Swap: 4096.0 total, 4096.0 free, 0.0 used. 7393.1 avail MemPID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND11110 jeremy 20 0 11912 5632 3456 R 14.3 0.1 00:00.05 top1526 jeremy 20 0 15124 7092 5120 S 7.1 0.1 00:15.84 sshd 1 root 20 0 22560 13412 9316 S 0.0 0.2 00:23.06 systemd 2 root 20 0 0 0 0 S 0.0 0.0 00:00.05 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 00:00.00 pool_workqueue_release 4 root 0 -20 0 0 0 S 0.0 0.0 00:00.00 kworker/R-rcu_g 5 root 0 -20 0 0 0 S 0.0 0.0 00:00.00 kworker/R-rcu_p
You can filter process listings by specific criteria such as PID or user. For example, to view details for process 1:
Copy
Ask AI
jeremy@kodekloud:~$ ps 1 PID TTY STAT TIME COMMAND 1 ? Ss 0:23 /sbin/initjeremy@kodekloud:~$
For detailed user-oriented information about process 1, run:
Copy
Ask AI
jeremy@kodekloud:~$ ps u 1USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDroot 1 0.2 0.1 22560 13412 ? Ss 15:07 0:23 /sbin/initjeremy@kodekloud:~$
To list processes started by a specific user (e.g., jeremy), use the uppercase -U option:
Copy
Ask AI
jeremy@kodekloud:~$ ps u -U jeremyUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDjeremy 1398 0.0 0.1 20324 11392 ? Ss 15:33 0:00 /usr/lib/systemd/systemd --userjeremy 1401 0.0 0.0 21148 3520 ? S 15:33 0:00 (sd-pam)jeremy 1412 0.0 0.0 8656 5632 tty1 S+ 15:33 0:00 -bashjeremy 1526 0.1 0.0 15124 7092 ? Ss 15:37 0:16 sshd: jeremy@pts/0jeremy 1527 0.0 0.0 10300 6400 pts/0 S 15:37 0:00 -bashjeremy 11326 0.0 0.0 10884 4480 pts/0 R+ 18:15 0:00 ps u -U jeremy
Additionally, you can search for processes by name using the pgrep command with the -a option to display the complete command line:
Copy
Ask AI
jeremy@kodekloud:~$ pgrep -a syslog1084 /usr/sbin/rsyslogd -n -iNONEjeremy@kodekloud:~$
Adjusting Process Priority Using Niceness and renice
Linux processes have a “niceness” value that determines their scheduling priority. This value ranges from -20 (highest priority) to 19 (lowest priority). When launching a process, you can assign a niceness value. For example, launching a bash shell with a niceness value of 11:
Copy
Ask AI
nice -n 11 bash
Note that the default ps aux output does not include niceness values. To view these, use the BSD long format with ps l, which displays the NI column:
Copy
Ask AI
jeremy@kodekloud:~$ nice -n 11 bashjeremy@kodekloud:~$ ps lF UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND4 1000 1412 1227 20 0 8656 5632 do_sel S+ tty1 0:00 -bash0 1000 1527 1526 20 0 10300 6400 do_wai Ss pts/0 0:00 -bash0 1000 11657 1527 31 11 8652 5504 do_wai SN pts/0 0:00 bash0 1000 11702 11657 31 11 10916 4224 - RN+ pts/0 0:00 ps l
Child processes inherit the niceness value from their parent process. To modify the niceness of an already running process, use the renice command. For example, to change the nice value of a process with PID 12238 from 0 to 7:
Copy
Ask AI
jeremy@kodekloud:~$ renice 7 1223812238 (process ID) old priority 0, new priority 7jeremy@kodekloud:~$
Only superusers can lower the nice value (i.e., increase priority) of a process. Regular users can only increase the niceness value.
Linux signals prompt processes to perform actions such as stopping, pausing, or terminating. Although some signals can be handled gracefully by applications, signals like SIGSTOP and SIGKILL are non-catchable. SIGSTOP pauses a process until it receives a SIGCONT, while SIGKILL immediately terminates it.List all available signals using:
For example, to send a SIGHUP signal (which typically instructs a process to reload its configuration) to the SSH daemon with PID 1457, use:
Copy
Ask AI
jeremy@kodekloud:~$ kill -s SIGHUP 1457
Since SSHD runs as root, a normal user must prepend sudo:
Copy
Ask AI
jeremy@kodekloud:~$ sudo kill -s SIGHUP 1457
When no signal is specified, the kill command sends SIGTERM by default for a graceful shutdown. To forcefully terminate a process, you can use SIGKILL in any of these ways:
Copy
Ask AI
kill -s SIGKILL PIDkill -KILL PIDkill -9 PID
The pkill command lets you send signals based on process names. For example, to send SIGKILL to all processes with “bash” in their name, first verify the target processes:
Copy
Ask AI
jeremy@kodekloud:~$ pgrep -a bash1412 -bash1527 -bash12066 bash
If the list is correct, use:
Copy
Ask AI
jeremy@kodekloud:~$ pkill -KILL bash
Be very careful when terminating processes with signals such as SIGKILL. Stopping critical processes may lead to system instability or disconnect you from your session.
Long-running tasks can be executed in the background, allowing you to use your terminal for other commands. For example, to run a sleep command for 180 seconds:
Copy
Ask AI
jeremy@kodekloud:~$ sleep 180
If you need to interrupt a process, press CTRL-C. In applications like Vim, you can suspend the process temporarily by pressing CTRL-Z:
Copy
Ask AI
jeremy@kodekloud:~$ vim /etc/hostname[1]+ Stopped vim /etc/hostnamejeremy@kodekloud:~$
To bring a suspended process back to the foreground, execute:
Copy
Ask AI
jeremy@kodekloud:~$ fgvim /etc/hostname
For processes started in the background using an ampersand (&):
This guide has demonstrated multiple techniques for diagnosing and managing processes in Linux. You learned to inspect processes with ps and top, adjust process priorities using nice and renice, send signals with kill and pkill, manage background and foreground tasks, and inspect open files with lsof. Mastering these tools is essential for effective system administration and troubleshooting on Linux.For more in-depth knowledge, explore additional resources such as: