4 Ways to View Processes in Linux


The PIDs of processes running on the system can be found using the 
pidof, pgrep, ps, and pstree commands.
Method 1: Use the pidof command
pidof is used to find the process ID of the running program. It prints these ids on standard output. To demonstrate, we will find out the process ID of Apache2 on a Debian 9 (stretch) system.
# pidof apache2
3754 2594 2365 2364 2363 2362 2361
From the output above, you may have trouble identifying the process ID as it shows all PIDs (both parent and child) by process name. So, we need to find out the parent PID (PPID), which is what we are looking for. It might be the first number. In this case, it's 3754, and it's sorted in descending order.
Method 2: Use the pgrep command
pgrep to traverse the currently running processes, and list the process IDs that meet the selection criteria to the standard output.
# pgrep apache2
2361
2362
2363
2364
2365
2594
3754
This is also similar to the output above, but it sorts the results from small to large, which makes it clear that the parent PID is last. In this case it is 3754.
Note: If you have process IDs for multiple processes, then using pidof and pgrep to identify the parent process ID may not go well.
Method 3: Using the pstree command
pstree displays running processes as a tree. The root of the tree is some pid, or init if the pid parameter is omitted. If a username is specified in the pstree command, all processes owned by the corresponding user are displayed.
pstree will put identical branches in square brackets and prefix them with a repeat count to visually merge them together.
# pstree -p | grep "apache2"
|- apache2(3754) -|-apache2(2361)
| |-apache2(2362)
| |-apache2(2363)
| |-apache2(2364)
| |-apache2(2365)
| `-apache2(2594)
To get the parent process individually, use the following format.
# pstree -p | grep "apache2" | head -1
|- apache2(3754) -|-apache2(2361) The
pstree command is very simple as it isolates the parent and child processes respectively, but this is the same when using pidof and pgrep commands Not easy to do.
Method 4: Use the ps command
ps to display selection information of active processes. It displays the process ID (pid=PID), the terminal associated with the process (tname=TTY), the accumulated CPU time in [DD-]hh:mm:ss format (time=TIME), and the execution name (ucmd=CMD ). The output is unsorted by default.
# ps aux | grep "apache2"
www-data 2361 0.0 0.4 302652 9732 ?S 06:25 0:00 /usr/sbin/apache2 -k start
www-data 2362 0.0 0.4 302652 9732 ?S 06:25 0:00 /usr/sbin/apache2 -k start
www-data 2363 0.0 0.4 302652 9732 ?S 06:25 0:00 /usr/sbin/apache2 -k start
www-data 2364 0.0 0.4 302652 9732 ?S 06:25 0:00 /usr/sbin/apache2 -k start
www-data 2365 0.0 0.4 302652 8400 ?S 06:25 0:00 /usr/sbin/apache2 -k start
www-data 2594 0.0 0.4 302652 8400 ?S 06:55 0:00 /usr/sbin/apache2 -k start
root 3754 0.0 1.4 302580 29324 ? Ss Dec11 0:23 /usr/sbin/apache2 -k start
root 5648 0.0 0.0 12784 940 pts/0 S+ 21:32 0:00 grep apache2
From the above output, we can The start date easily identifies the parent process ID (PPID). In this example, apache2 was started on Dec 11, it is the parent process, the others are child processes. The PID of apache2 is 3754.

Extended reading:
Everyone knows PID, what exactly is PID? Why do you want the PID? What are you going to do with the PID? Do you have the same question in your head? If so, you're in the right place for these details.
We query the PID primarily to kill an unresponsive program, similar to the Windows Task Manager. The Linux GUI also provides the same functionality, but the CLI is an efficient way to perform the kill operation.
What is a process ID?
PID stands for process identification and is used in most operating system kernels such as Linux, Unix, macOS, and Windows. It is a unique identification number that is automatically assigned to each process when it is created in the operating system. A process is a running program instance.
All process IDs except the init process change each time because init is always the first process on the system and is the parent of all other processes. Its PID is 1.
The default maximum value for PID is 32768. This can be verified by running cat /proc/sys/kernel/pid_max on your system. On a 32-bit system, 32768 is the maximum value, but we can set it to any value within the maximum of 222 (about 4 million) on a 64-bit system. You might ask, why do we need so many PIDs? Because we can't reuse the PID right away, that's why. Also to prevent possible errors.

Post a Comment

Previous Post Next Post