您的位置:首页 > 运维架构 > Shell

【SHELL】使用ps如何准确地打印出某个用户的进程

2015-11-03 11:05 633 查看
请看如下两种方式:

<pre name="code" class="html"><pre name="code" class="html">[info@localhost ~]$ ps -U 500 -f h
info      3964  3962  0 09:47 ?        S      0:00 sshd: info@pts/2
info      3965  3964  0 09:47 pts/2    Ss+    0:00 -bash
info      5468  3964  0 10:09 ?        Ss     0:00 /usr/libexec/openssh/sftp-server
info      5944     1  0  2011 ?        Ssl    0:00 /usr/libexec/bonobo-activation-server --ac-activate --ior-output-fd=16
info      6377  6375  0 10:21 ?        S      0:00 sshd: info@pts/3
info      6378  6377  0 10:21 pts/3    Ss+    0:00 -bash
info      7068  7066  0 10:32 ?        S      0:00 sshd: info@pts/4
info      7069  7068  0 10:32 pts/4    Ss     0:00 -bash
info      7740  7069  0 10:43 pts/4    R+     0:00 ps -U 500 -f h
info     19376 19375  0  2012 pts/1    S      0:00 -bash
root     19427 19376  0  2012 pts/1    S      0:00 su - root
info     26974     1  0  2011 ?        Ss     0:00 /home/info/IBM/HTTPServer/bin/httpd -f /home/info/IBM/HTTPServer/conf/admin.conf
info     26976 26974  0  2011 ?        Sl     0:00 /home/info/IBM/HTTPServer/bin/httpd -f /home/info/IBM/HTTPServer/conf/admin.conf
info     29616 26974  0 Sep15 ?        Sl     0:00 /home/info/IBM/HTTPServer/bin/httpd -f /home/info/IBM/HTTPServer/conf/admin.conf
[info@localhost ~]$ ps -U 500 -f h |wc -l
15
[info@localhost ~]$



[info@localhost ~]$ ps -ef|grep info
root      3962 14846  0 09:47 ?        00:00:00 sshd: info [priv]
info      3964  3962  0 09:47 ?        00:00:00 sshd: info@pts/2
info      3965  3964  0 09:47 pts/2    00:00:00 -bash
info      5468  3964  0 10:09 ?        00:00:00 /usr/libexec/openssh/sftp-server
info      5944     1  0  2011 ?        00:00:00 /usr/libexec/bonobo-activation-server --ac-activate --ior-output-fd=16
root      6375 14846  0 10:21 ?        00:00:00 sshd: info [priv]
info      6377  6375  0 10:21 ?        00:00:00 sshd: info@pts/3
info      6378  6377  0 10:21 pts/3    00:00:00 -bash
root      7066 14846  0 10:32 ?        00:00:00 sshd: info [priv]
info      7068  7066  0 10:32 ?        00:00:00 sshd: info@pts/4
info      7069  7068  0 10:32 pts/4    00:00:00 -bash
info      7891  7069  0 10:45 pts/4    00:00:00 ps -ef
info      7892  7069  0 10:45 pts/4    00:00:00 grep info
info      7893  7069  0 10:45 pts/4    00:00:00 more
root     19375 19348  0  2012 pts/1    00:00:00 su - info
info     19376 19375  0  2012 pts/1    00:00:00 -bash
info     26974     1  0  2011 ?        00:00:00 /home/info/IBM/HTTPServer/bin/httpd -f /home/info/IBM/HTTPServer/conf/admin.conf
info     26976 26974  0  2011 ?        00:00:00 /home/info/IBM/HTTPServer/bin/httpd -f /home/info/IBM/HTTPServer/conf/admin.conf
info     29616 26974  0 Sep15 ?        00:00:00 /home/info/IBM/HTTPServer/bin/httpd -f /home/info/IBM/HTTPServer/conf/admin.conf
[info@localhost ~]$ ps -ef|grep info|wc -l
19
[info@localhost ~]$


很明显,第一种方式比第二种方式较好。但是第一种方式的19427号进程为什么会被打印出来,目前还不知道。。。。

原因是ps -U 是RUID,正确应该是指定EUID,也就是-u参数。

[info@localhost ~]$ ps -u 500 -f h
info      3964  3962  0 09:47 ?        S      0:00 sshd: info@pts/2
info      3965  3964  0 09:47 pts/2    Ss+    0:00 -bash
info      5468  3964  0 10:09 ?        Ss     0:00 /usr/libexec/openssh/sftp-server
info      5944     1  0  2011 ?        Ssl    0:00 /usr/libexec/bonobo-activation-server --ac-activate --ior-output-fd=16
info      6377  6375  0 10:21 ?        S      0:00 sshd: info@pts/3
info      6378  6377  0 10:21 pts/3    Ss+    0:00 -bash
info      7068  7066  0 10:32 ?        S      0:00 sshd: info@pts/4
info      7069  7068  0 10:32 pts/4    Ss     0:00 -bash
info      7740  7069  0 10:43 pts/4    R+     0:00 ps -U 500 -f h
info     19376 19375  0  2012 pts/1    S      0:00 -bash
root     19427 19376  0  2012 pts/1    S      0:00 su - root
info     26974     1  0  2011 ?        Ss     0:00 /home/info/IBM/HTTPServer/bin/httpd -f /home/info/IBM/HTTPServer/conf/admin.conf
info     26976 26974  0  2011 ?        Sl     0:00 /home/info/IBM/HTTPServer/bin/httpd -f /home/info/IBM/HTTPServer/conf/admin.conf
info     29616 26974  0 Sep15 ?        Sl     0:00 /home/info/IBM/HTTPServer/bin/httpd -f /home/info/IBM/HTTPServer/conf/admin.conf
[info@localhost ~]$ ps -u 500 -f h |wc -l
14
[info@localhost ~]$
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: