Linux and Unix ps command tutorial with examples

Tutorial on using ps, a UNIX and Linux command for reporting information on running processes. Examples of searching by user, group, executable name and killing processes.

Estimated reading time: 5 minutes

Table of contents

Terminal showing ps man page

What is the ps command in UNIX?

The ps command reports information on current running processes, outputting to standard output. It is frequently used to find process identifier numbers. It supports searching for processes by user, group, process id or executable name. Related commands include pgrep that supports searching for processes and pkill that can kill processes based on a search.

How to show processes for the current shell

To show the processes for the current running shell run ps. If nothing else is running this will return information on the shell process being run and the ps command that is being run.

ps
PID  TTY          TIME CMD
5763 pts/3    00:00:00 zsh
8534 pts/3    00:00:00 ps

The result contains four columns of information.

  • PID - the number of the process
  • TTY - the name of the console that the user is logged into
  • TIME- the amount of CPU in minutes and seconds that the process has been running
  • CMD - the name of the command that launched the process

To demonstrate that other processes will show by just running ps a task can be put into the background before running the command.

sleep 10 &
ps
PID   TTY          TIME CMD
5763  pts/3    00:00:00 zsh
10254 pts/3    00:00:00 sleep
10258 pts/3    00:00:00 ps

How to list all processes

To list all processes on a system use the -e option.

ps -e
PID TTY          TIME CMD
  1 ?        00:00:01 systemd
  2 ?        00:00:00 kthreadd
  3 ?        00:00:00 ksoftirqd/0
....  

This option can be combined with the -f and -F options to provide more information on processes. The -f option offers full-format listing.

ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 19:58 ?        00:00:01 /sbin/init
root         2     0  0 19:58 ?        00:00:00 [kthreadd]
root         3     2  0 19:58 ?        00:00:00 [ksoftirqd/0]
...

The -F provides extra full format information.

ps -F 
UID        PID  PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
root         1     0  0 13250  6460   1 19:58 ?        00:00:01 /sbin/init
root         2     0  0     0     0   1 19:58 ?        00:00:00 [kthreadd]
root         3     2  0     0     0   0 19:58 ?        00:00:00 [ksoftirqd/0]
...

Another commonly used syntax to achieve seeing every process on the system using BSD syntax is ps aux.

ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  53120  6368 ?        Ss   Sep19   0:01 /sbin/init
root         2  0.0  0.0      0     0 ?        S    Sep19   0:00 [kthreadd]
...

How to list all processes for a user

To list all processes by user use the -u option. This supports the user ID or name.

ps -u george
 PID TTY          TIME CMD
1053 ?        00:00:00 systemd
1062 ?        00:00:00 (sd-pam)
1074 tty1     00:00:00 zsh
...

How to list all processes for a group

To list all processes by group use the -g option. This supports the group ID or name.

ps -g users
 PID TTY          TIME CMD
 997 ?        00:00:00 login
1053 ?        00:00:00 systemd
1062 ?        00:00:00 (sd-pam)
...

How to list all processes by process number

To list all processes by process number use the -p option. This selects the processes whose numbers match the list provided to the -p option.

ps -p 12608 3995
  PID TTY      STAT   TIME COMMAND
 3995 ?        Ss     0:00 st
12608 pts/2    S+     0:04 vim content/post/unix-ps.md

How to list all processes by executable name

To list all processes by executable name use the -C option. This selects the processes whose executables match the list of executables given to the -Coption.

ps -C tmux
 PID TTY          TIME CMD
5733 pts/0    00:00:00 tmux
5735 ?        00:00:06 tmux

How to show a process hierarchy or tree

To show a process hierarchy or tree use the -H option. This outputs a process tree.

ps -eH
  PID TTY          TIME CMD
 5735 ?        00:00:07   tmux
 5736 pts/2    00:00:00     zsh
12608 pts/2    00:00:08       vim
 5763 pts/3    00:00:00     zsh
17185 pts/3    00:00:00       ps

This may also be displayed in ASCII format by using the --forest option.

ps -e --forest
 PID TTY          TIME CMD
 5735 ?        00:00:07 tmux
 5736 pts/2    00:00:00  \_ zsh
12608 pts/2    00:00:08  |   \_ vim
 5763 pts/3    00:00:00  \_ zsh
16952 pts/3    00:00:00      \_ ps

How to just get the process id

A common task is to find the process id of a running process. Like many things in UNIX this can be achieved in a number of ways. In the examples above the ps command can look for processes by user, group or executable name. The ps can be also be piped to grep to search for arbitrary items.

ps -ef | grep vim
george   12608  5736  0 21:00 pts/2    00:00:11 vim content/post/unix-ps.md
george   18324  5763  0 21:32 pts/3    00:00:00 grep vim

On many systems the pgrep command also exists that supports a number of ways to search for a process id. This is very useful if you are just interested in process id rather than other information. To search for all processes for an executable the pgrep command can be used.

pgrep tmux
5733
5735

To search by user pass the -u option.

pgrep -u george
1053
1062
1074
...

To search by group pass the -G option.

pgrep -G users
997
1053
1062
...

How to search for an kill a process

To search for an kill a process the ps command can first be used to find the process id before using the kill command to terminate the process. In the following example ps is piped to grep and awk.

sleep 100 &
[1] 21664
kill $(ps -e | grep 'sleep' | awk '{print $1}')
[1]  + terminated  sleep 100

On some systems the pkill command is also available to accomplish this.

sleep 100 &
pkill sleep
[1]  + terminated  sleep 100

Further reading

Have an update or suggestion for this article? You can edit it here and send me a pull request.

Tags

Recent Posts

  • Using HashiCorp Vault with LDAP
    How to use HashiCorp Vault to setup an LDAP backed secret store with read-only access for users in groups and read-write access for specific users

     

  • Linux and Unix xargs command tutorial with examples
    Tutorial on using xargs, a UNIX and Linux command for building and executing command lines from standard input. Examples of cutting by character, byte position, cutting based on delimiter and how to modify the output delimiter.

     

  • Copy a file in Go
    How to copy a file in Go. The ioutil package does not offer a shorthand way of copying a file. Instead the os package should be used.

CCF大数据与计算智能大赛-面向电信行业存量用户的智能套餐个性化匹配模型联通赛-复赛第二名-【多分类,embedding】.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值