Linux list running processes user

Linux: How to show a list of running processes with PID, user, group, command line, and parent/child hierarchy?

Also, the parent/child hierarchy of the processes should be displayed.

2 Answers 2

The magic combination is ps axfo pid,euser,egroup,args

Here is an output example on Ubuntu 16.04:

$ ps axfo pid,euser,egroup,args PID EUSER EGROUP COMMAND 2 root root [kthreadd] 3 root root \_ [ksoftirqd/0] 4 root root \_ [kworker/0:0] 5 root root \_ [kworker/0:0H] 6 root root \_ [kworker/u4:0] 7 root root \_ [rcu_sched] 8 root root \_ [rcu_bh] 9 root root \_ [migration/0] 10 root root \_ [watchdog/0] 11 root root \_ [watchdog/1] 12 root root \_ [migration/1] 13 root root \_ [ksoftirqd/1] 14 root root \_ [kworker/1:0] 15 root root \_ [kworker/1:0H] 16 root root \_ [kdevtmpfs] 17 root root \_ [netns] 18 root root \_ [perf] 19 root root \_ [khungtaskd] 20 root root \_ [writeback] 21 root root \_ [ksmd] 22 root root \_ [khugepaged] 23 root root \_ [crypto] 24 root root \_ [kintegrityd] 25 root root \_ [bioset] 26 root root \_ [kblockd] 27 root root \_ [ata_sff] 28 root root \_ [md] 29 root root \_ [devfreq_wq] 30 root root \_ [kworker/u4:1] 31 root root \_ [kworker/1:1] 32 root root \_ [kworker/0:1] 34 root root \_ [kswapd0] 35 root root \_ [vmstat] 36 root root \_ [fsnotify_mark] 37 root root \_ [ecryptfs-kthrea] 53 root root \_ [kthrotld] 54 root root \_ [acpi_thermal_pm] 55 root root \_ [bioset] 56 root root \_ [bioset] 57 root root \_ [bioset] 58 root root \_ [bioset] 59 root root \_ [bioset] 60 root root \_ [bioset] 61 root root \_ [bioset] 62 root root \_ [bioset] 63 root root \_ [bioset] 64 root root \_ [bioset] 65 root root \_ [bioset] 66 root root \_ [bioset] 67 root root \_ [bioset] 68 root root \_ [bioset] 69 root root \_ [bioset] 70 root root \_ [bioset] 71 root root \_ [bioset] 72 root root \_ [bioset] 73 root root \_ [bioset] 74 root root \_ [bioset] 75 root root \_ [bioset] 76 root root \_ [bioset] 77 root root \_ [bioset] 78 root root \_ [bioset] 79 root root \_ [scsi_eh_0] 80 root root \_ [scsi_tmf_0] 81 root root \_ [scsi_eh_1] 82 root root \_ [scsi_tmf_1] 83 root root \_ [kworker/u4:2] 87 root root \_ [ipv6_addrconf] 88 root root \_ [kworker/1:2] 89 root root \_ [kworker/u4:3] 102 root root \_ [deferwq] 103 root root \_ [charger_manager] 221 root root \_ [kpsmoused] 242 root root \_ [kworker/0:2] 506 root root \_ [mpt_poll_0] 509 root root \_ [mpt/0] 513 root root \_ [scsi_eh_2] 514 root root \_ [scsi_tmf_2] 515 root root \_ [bioset] 517 root root \_ [bioset] 662 root root \_ [raid5wq] 695 root root \_ [bioset] 736 root root \_ [jbd2/sda1-8] 737 root root \_ [ext4-rsv-conver] 802 root root \_ [iscsi_eh] 805 root root \_ [ib_addr] 806 root root \_ [ib_mcast] 807 root root \_ [ib_nl_sa_wq] 808 root root \_ [ib_cm] 809 root root \_ [iw_cm_wq] 810 root root \_ [rdma_cm] 824 root root \_ [kauditd] 1198 root root \_ [iprt-VBoxWQueue] 1778 root root \_ [kworker/1:1H] 1800 root root \_ [kworker/0:1H] 1854 root root \_ [kworker/1:3] 2524 root root \_ [kworker/0:3] 1 root root /sbin/init 794 root root /lib/systemd/systemd-journald 848 root root /sbin/lvmetad -f 872 root root /lib/systemd/systemd-udevd 1815 systemd+ systemd+ /lib/systemd/systemd-timesyncd 1836 root root /usr/sbin/cron -f 1838 daemon daemon /usr/sbin/atd -f 1840 root root /lib/systemd/systemd-logind 1851 root root /usr/sbin/acpid 1853 syslog syslog /usr/sbin/rsyslogd -n 1860 root root /usr/bin/lxcfs /var/lib/lxcfs/ 1865 root root /usr/lib/accountsservice/accounts-daemon 1870 root root /usr/lib/snapd/snapd 1875 message+ message+ /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation 1888 root root /sbin/mdadm --monitor --pid-file /run/mdadm/monitor.pid --daemonise --scan --syslog 1890 root root /usr/lib/policykit-1/polkitd --no-debug 1995 root root /sbin/dhclient -1 -v -pf /run/dhclient.enp0s3.pid -lf /var/lib/dhcp/dhclient.enp0s3.leases -I -df /var/lib/dhcp/dhclient6.enp0s3.lease 2184 root root /sbin/iscsid 2185 root root /sbin/iscsid 2288 root root /usr/sbin/irqbalance --pid=/var/run/irqbalance.pid 2294 root root /usr/sbin/sshd -D 2566 root root \_ sshd: ubuntu [priv] 2602 ubuntu ubuntu \_ sshd: ubuntu@pts/0 2603 ubuntu ubuntu \_ -bash 2618 ubuntu ubuntu \_ ps axfo pid,euser,egroup,args 2301 root root /sbin/agetty --keep-baud 115200 38400 9600 ttyS0 vt220 2305 root root /sbin/agetty --noclear tty1 linux 2568 ubuntu ubuntu /lib/systemd/systemd --user 2570 ubuntu ubuntu \_ (sd-pam) 

What I usually do is — I create an alias and drop it in a convenient place such as /etc/profile.d/aliases.sh with the following content:

alias psx='ps axfo pid,euser,egroup,args' 

And then I only need to run psx to get the nice, informative output shown above.

Читайте также:  Хакерские утилиты кали линукс

Источник

5 practical examples to list running processes in Linux

How to list processes by user and name in Linux? How to check if process is running by pid ? How to check process status? which command is used to kill a process?

In this tutorial we will cover all these questions and explore different commands and tools to list and manage processes in Linux and Unix. ps command is the best tool to list down all the running processes across the server. There are a wide range of arguments which can be used with ps to list processes based in our requirement.

List all the running processes

Method-1: Using “px aux”

To list every process on the system using BSD syntax:

ax Lift the BSD-style "only yourself" restriction, which is imposed upon the set of all processes when some BSD-style (without "-") options are used or when the ps personality setting is BSD-like. u Display user-oriented format.

This will give you a long list of output with more details on individual process such as memory and cpu usage, status, user owner of the process and more. Following is a snippet from my terminal:

5 practical examples to list running processes in Linux

Method-2: Using “ps -ef”

The next method will list all the running process using standard syntax:

 -e Select all processes -f Do full-format listing.

This gives lesser information compared to ps aux :

5 practical examples to list running processes in Linux

Method-3: Using “ps -ely”

We can use some more arguments with ps to list the running processes in Linux:

-e Select all processes. -l Long format -y Do not show flags; show rss in place of addr

This command will give us additional detail compared to ps -ef such as priority and nice value of individual process.

Читайте также:  Kali linux поиск человека

5 practical examples to list running processes in Linux

List processes by user

To list all the processes based on user owner we can use following syntax:

-U userlist Select by real user ID (RUID) or name. It selects the processes whose real user name or ID is in the userlist list. The real user ID identifies the user who created the process, -u userlist Select by effective user ID (EUID) or name. This selects the processes whose effective user name or ID is in userlist. u Display user-oriented format.

To list the process started by user root:

Sample output from my terminal:

5 practical examples to list running processes in Linux

To list the process started by normal user deepak :

~]# ps -U deepak -u deepak u USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND deepak 5575 0.0 0.0 280884 8508 ? S Oct20 0:05 python /opt/deepak/jboss/bin/dualprim_watch.py -a 20 -m 500 -U nds -L 500 -t 10 deepak 8995 0.0 0.0 113420 1628 ? S Oct20 0:00 /bin/sh /opt/deepak/jboss/bin/standalone.sh -b 0.0.0.0 -c standalone-full-adm.xml deepak 9208 2.9 3.6 23510668 4848336 ? Sl Oct20 50:04 /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.252.b09-2.el7_8.x86_64/jre/bin/java -D[Standal

List the process tree

Method-1: Using «ps axjf» or «ps -ef —forest»

We can also use ps command to list the running process in the tree format to understand the parent and child processes.

5 practical examples to list running processes in Linux

Method-2: Using pstree

Although you have a better alternative to above command if you wish to see the structure of all the running process using pstree which is part of psmisc rpm in RHEL/CentOS distribution. This command is used to display the parent-child relationship in hierarchical format.

-p Show PIDs. PIDs are shown as decimal numbers in parentheses after each process name

5 practical examples to list running processes in Linux

To list the process tree of process started by individual user, you can use

For example to show the process tree of user deepak :

You can check the man page of pstree for more list of supported options.

List thread count for individual process

We can use -L argument to list the number of threads along with individual process. It will add a new column in the output possibly with LWP and NLWP

Sample output from this command:

5 practical examples to list running processes in Linux

List process with user defined format

By default ps will show a certain default list of columns. You can manipulate and print your own set of columns to get the required details of a process by using following syntax:

Here, you can replace the ARGUMENTS with supported list of values from man page of ps

Example-1: Show only PID and command

To show only the list of PID and their respective commands:

 ~]# ps -eo pid,comm PID COMMAND 1 systemd 2 kthreadd 3 rcu_gp 4 rcu_par_gp 6 kworker/0:0H-kblockd .

Example-2: Show memory and cpu details of each process

There are different arguments which you can use to print the memory and cpu related information of individual process, here I have consolidated a few:

 ~]# ps -eo pid,%mem,%cpu,rss,rsz,vsz,comm PID %MEM %CPU RSS RSZ VSZ COMMAND . 902 0.2 0.0 14420 14420 424800 sssd 904 0.1 0.0 5220 5220 82744 avahi-daemon 905 0.4 0.0 22152 22152 1626068 polkitd 912 0.2 0.0 14640 14640 462728 ModemManager 914 0.0 0.0 2012 2012 18872 lsmd 916 0.0 0.0 2136 2136 17408 mcelog .

Get process ID of a process

Now assuming you have a running process for which you want to get the PID so we can again use ps in this format:

Читайте также:  Смена даты создания файла linux

Here we need to replace PROCESS with the name of the process or command for which we want to perform the lookup of PID. For example to get the PID of rsyslogd process:

Similarly to get the list of PID for sshd daemon

 ~]# ps -C sshd -o pid= 999 1705 1899 2009 2011 2319 2321 2342 2344 13874 13879

Get process name using the PID

Now if the situation is reversed, i.e. you have the PID and you wish to get the process or command of the mapping PID then you can use following format:

Here, replace PID with the pid value of the process for which you have to perform lookup. Following are some examples where we get the process name using the PID value.

 ~]# ps -q 2009 -o comm= sshd ~]# ps -q 1 -o comm= systemd

List stopped processes

You can stop a running or hung process using ctrl+z short key. When you press this key combination, the ongoing process on the terminal will be forcefully stopped.

For example, here I had an SFTP session which was stuck so I pressed ctrl+z to stop the process forcefully which immediately stops the process and returns to console.

~]# sftp -o Port=2222 deepak@server.ext.example.com deepak@server.ext.example.com's password: Connected to deepak@server.ext.example.com. sftp> exit Interrupt ^Z [3]+ Stopped sftp -o Port=2222 deepak@server.ext.example.com

To list all the processes which are in stopped state use jobs command

~]# jobs [1] Stopped sftp -o Port=2222 deepak@server.ext.example.com [2]- Stopped sftp -o Port=2222 deepak@server.ext.example.com [3]+ Stopped sftp -o Port=2222 deepak@server.ext.example.com

So currently in my server, I have 3 stopped processes. To kill a stopped process we use

where JOB ID is the ID number you see with «Stopped» under square brackets.

So for example to kill the process with job ID 3 we will use:

~]# kill %3 [3]+ Stopped sftp -o Port=2222 deepak@server.ext.example.com

Next if I check the current stopped processes then I see that the process with JOB ID 3 is marked as Exit which means it is in the verge of getting killed (almost dead)

~]# jobs [1] Stopped sftp -o Port=2222 deepak@server.ext.example.com [2]- Stopped sftp -o Port=2222 deepak@server.ext.example.com [3]+ Exit 1 sftp -o Port=2222 deepak@server.ext.example.com

We check the status again in few seconds and our process with JOB ID 3 is not there in the list any more and was killed successfully

]# jobs [1]- Stopped sftp -o Port=2222 deepak@server.ext.example.com [2]+ Stopped sftp -o Port=2222 deepak@server.ext.example.com

Conclusion

In this tutorial we learned about listing and managing Linux processes using ps command. We also have other tools such as top , htop which can list the system processes but I find ps more suitable in most scenarios. If you requirement is to watch the runtime status of process i.e. to monitor a process and it’s status then top would be your best alternative as it continuously monitors the status of process and shows you latest stat for memory, cpu usage and other related values.

Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Источник

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