Get process info by pid linux

30 Useful ‘ps Command’ Examples for Linux Process Monitoring

ps (processes status) is a native Unix/Linux utility for viewing information concerning a selection of running processes on a system: it reads this information from the virtual files in the /proc filesystem. It is one of the important utilities for system administration specifically under process monitoring, to help you understand whats is going on in a Linux system.

It has numerous options for manipulating its output, however, you’ll find a small number of them practically useful for daily usage.

In this article, we’ll look at 30 useful examples of ps commands for monitoring active running processes on a Linux system.

Note that ps produces output with a heading line, which represents the meaning of each column of information, you can find the meaning of all the labels on the ps man page.

List All Processes in Current Shell

1. If you run the ps command without any arguments, it displays processes for the current shell.

List Current Running Processes

2. Display every active process on a Linux system in generic (Unix/Linux) format.

List Processes in Standard Format

3. Display all processes in BSD format.

List Processes in BSD Format

4. To perform a full-format listing, add the -f or -F flag.

List Processes in Long List Format

Display User Running Processes

5. You can select all processes owned by you (runner of the ps command, root in this case), type:

6. To display a user’s processes by real user ID (RUID) or name, use the -U flag.

$ ps -fU tecmint OR $ ps -fu 1000

List User Processes by ID

7. To select a user’s processes by effective user ID (EUID) or name, use the -u option.

$ ps -fu tecmint OR $ ps -fu 1000

8. The command below enables you to view every process running with root user privileges (real & effective ID) in user format.

Display Root User Running Processes

Display Group Processes

9. If you want to list all processes owned by a certain group (real group ID (RGID) or name), type.

$ ps -fG apache OR $ ps -fG 48

Display Group Processes

10. To list all processes owned by effective group name (or session), type.

Display Processes by PID and PPID

11. You can list processes by PID as follows.

List Processes by PID

12. To select process by PPID, type.

List Process by PPID

13. Make a selection using a PID list.

List Processes by PIDs

Display Processes by TTY

14. To select processes by tty, use the -t flag as follows.

$ ps -t pts/0 $ ps -t pts/1 $ ps -ft tty1

List Processes by TTY

15. A process tree shows how processes on the system are linked to each other; processes whose parents have been killed are adopted by the init (or systemd).

List Process Tree

16. You can also print a process tree for a given process like this.

$ ps -f --forest -C sshd OR $ ps -ef --forest | grep -v grep | grep sshd

List Tree View of Process

17. To print all threads of a process, use the -L flag, this will show the LWP (lightweight process) as well as NLWP (number of the lightweight processes) columns.

Читайте также:  Linux firmware package debian

List Process Threads

Specify Custom Output Format

Using the -o or –format options, ps allows you to build user-defined output formats as shown below.

18. To list all format specifiers, include the L flag.

19. The command below allows you to view the PID, PPID, user name, and command of a process.

List Processes with Names

20. Below is another example of a custom output format showing file system group, nice value, start time, and elapsed time of a process.

$ ps -p 1154 -o pid,ppid,fgroup,ni,lstart,etime

List Process ID Information

Find Process using PID

Display Parent and Child Processes

22. To select a specific process by its name, use the -C flag, this will also display all its child processes.

Find Parent Child Process

23. Find all PIDs of all instances of a process, useful when writing scripts that need to read PIDs from an std output or file.

Find All Process PIDs

24. Check the execution time of a process.

$ ps -eo comm,etime,user | grep httpd

The output below shows the HTTPD service has been running for 1 hour, 48 minutes, and 17 seconds.

Find Process Uptime

Troubleshoot Linux System Performance

If your system isn’t working as it should be, for instance, if it’s unusually slow, you can perform some system troubleshooting as follows.

26. Find top running processes by highest memory and CPU usage in Linux.

$ ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head OR $ ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head

Find Top Running Processes

27. To kill Linux processes/unresponsive applications or any process that is consuming high CPU time.

First, find the PID of the unresponsive process or application.

Then use the kill command to terminate it immediately.

Find and Kill a Process

28. Show security context (specifically for SELinux) like this.

Find SELinux Context

29. You can also display security information in a user-defined format with this command.

$ ps -eo euser,ruser,suser,fuser,f,comm,label

List SELinux Context by Users

Perform Real-time Process Monitoring Using Watch Utility

30. Finally, since ps displays static information, you can employ the watch utility to perform real-time process monitoring with repetitive output, displayed after every second as in the command below (specify a custom ps command to achieve your objective).

$ watch -n 1 'ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head'

Real Time Process Monitoring

Important: ps only shows static information, to view frequently updated output you can use tools such as htop; top, and glances: the last two are in fact Linux system performance monitoring tools.

You might also like to read the following related articles.

That’s all for now. If you have any useful ps command example(s) to share (not forgetting to explain what it does), use the comment form below.

Источник

Essential Examples of the ps Command in Linux

The ps command in Linux is used for getting information about running processes. Here are some useful examples of the complicated and extensive ps command.

What is ps command in Linux?

The ps command in Linux displays running processes on the system. You can get information like process ID (PID) for the processes you or any other user is running on the same Linux system.

The ps command is an extensive tool and has over 80 command options. You can understand its strength and complexity. This is why I will show you some of the most common and useful examples of the ps command in Linux.

Essential usage of ps command in Linux

Ps Command In Linux

If you use the ps command without any options in Linux, it will show the running processes in the current shell:

This is the output. I have sent Gedit command in the background this is why it shows three processes otherwise you will normally see just ps and bash.

 PID TTY TIME CMD 503 pts/0 00:00:00 gedit 2053 pts/0 00:00:00 ps 31585 pts/0 00:00:00 bash
  • PID is the unique process ID of the process
  • TTY is the type of terminal user is logged in to. pts means pseudo terminal
  • TIME gives you how long the process has been running
  • CMD is the command that you run to launch the process
Читайте также:  Линукс разделы файловой системы

Now, this doesn’t really provide any real, useful information. Let’s see some better examples of the ps command:

1. See all your running process

If you want to see all the processes run by you, you can use the ps command with options x like this:

The x option will display all the processes even if they are not associated with current tty (terminal type) or if they don’t have a controlling terminal (like daemons).

The – before option x is optional, but the general Linux convention is to use – before options, so I advise you to keep on following it. It won’t hurt you.

This is what the output looks like. I have truncated the output because it had hundreds of lines:

 PID TTY STAT TIME COMMAND 503 pts/0 Sl 0:00 gedit 2245 ? S 0:00 /usr/bin/ssh-agent -D -a /run/user/1000/keyring/.ssh 3039 ? Ss 0:00 /lib/systemd/systemd --user 3040 ? S 0:00 (sd-pam) 3054 ? SLl 0:01 /usr/bin/gnome-keyring-daemon --daemonize --login 3059 tty2 Ssl+ 0:00 /usr/lib/gdm3/gdm-x-session --run-script env 

The STAT in the above output means Process State Codes. You can find a detailed table in the man page of the ps command.

But you’ll rarely see ps command used with just option x. It is usually accompanied by option u in this manner:

With option u, you’ll have detailed information about each process:

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND abhishek 503 0.0 0.4 681580 37516 pts/0 Sl 18:09 0:00 gedit abhishek 2245 0.0 0.0 11300 1496 ? S 18:37 0:00 /usr/bin/ssh-agent -D -a /run/user/1000/keyring/.ssh abhishek 3039 0.0 0.0 77344 3508 ? Ss 10:37 0:00 /lib/systemd/systemd --user abhishek 3040 0.0 0.0 114632 360 ? S 10:37 0:00 (sd-pam) abhishek 3054 0.0 0.1 517104 11512 ? SLl 10:37 0:01 /usr/bin/gnome-keyring-daemon --daemonize --login

As you can see, now you get the user name, CPU consumption, and memory usage of each process. RSS shows how much memory the process currently has in RAM while VSZ is how much virtual memory the process has in total.

2. See all running processes with ps aux command

You’ll probably see ps -aux or ps aux all the time in Linux tutorials and documentation.

With the added -a option, you can see the running processes by all the users on Linux system.

The command output is the same as ps -ux but now you have processes from other users as well. Thanks to the -u option, you can identify which process belongs to which user.

List all running processes in Linux

3. See all running processes with ps -ef command in Linux

Apart from ps -aux, you can also list all the running processes with -e option. The common practice is to combine it with the option -f to get the full listing of the commands used to run the processes.

Do note that – before e is important; otherwise, it will show a different result. I told you, ps is a messed up, complicated command.

You can also combine the H option to see all the processes in a threaded view with child processes under their parents:

4. See all running processes by a certain user

To get the information about all the processes run by a certain user, you can use the -U option with the user name:

Читайте также:  Arch linux no wifi menu

For example, I can see all the processes running by the root user like this:

ps -U root PID TTY TIME CMD 1 ? 00:00:41 systemd 2 ? 00:00:00 kthreadd 3 ? 00:00:00 rcu_gp 4 ? 00:00:00 rcu_par_gp 8 ? 00:00:00 mm_percpu_wq 9 ? 00:00:03 ksoftirqd/0 10 ? 00:01:22 rcu_sched

5. See all processes run by a group

You can also categorize running processes by group instead of users by providing group name or group id:

You can combine with option f to get the full listing.

6. Get all occurrences and PID of a program

One of the essential use of the ps command is to get the process ID (PID) of a running program. Normally when you are looking to kill a misbehaving program, you search for all the program occurrences, get their PIDs and use the kill command to end the process.

For example, if I have to find all running instances of the apt package manager:

ps -C apt PID TTY TIME CMD 11425 pts/1 00:00:00 apt

You can also use grep command to get a similar result.

ps aux | grep program_name

7. Get process information about a PID

Alright! You have a PID but don’t know which process it belongs to. You can use the ps command to find the process information from its PID in this way:

You can use more than one PID by separating them with a comma:

As I said earlier, ps is a complicated and extensive command. These were some of the most common ps command examples you’ll need most of the time. If you want more, you can always refer to its man page.

If you have some other useful example of the ps command that you use regularly, why not share it with the rest of us in the comment section?

Источник

How to Find Process Name from its PID

If you know the PID of a process, here’s how to get the process name in Linux command line.

If you know the process ID (PID), you can get the process name using the ps command:

The ps command is used for process related operations. In the above command, -p PID provides the process ID and -o comm= asks it to output the command associated with the given PID.

Get process name from process ID

Honestly, it’s difficult to remember the weird ps -p PID -o comm= syntax. And if that’s the case, you may either look into the man page of ps command or use other ways of getting the details.

You can list all running process using the ps command or top command and note the process ID and process name as desired.

List all running processes in Linux

If you know the PID, you can simply use the grep command to filter the output and get the details on that PID:

But as illustrated by the image below, the output gives other details along with the process name.

Use grep and ps command to get process name from PID

That’s not an issue if you are manually reading it. However, if you have to use it in a script, it could be an issue.

This is where the ps -p PID -o comm= is better.

Bonus tip: Since we are talking about process name and PIDs, let me quickly show you the reverse method, i.e., to find PID from process name.

There is a dedicated command called pidof and you can use it like this if you know the exact process name:

So, you just learned how to find process name from its PID in Linux command line and you also learned to get the PID from the process name.

If something isn’t working right, or you just have any comments in general, feel free to leave any of it in the comments below.

Источник

Оцените статью
Adblock
detector