我使用过的Linux命令之ps - 显示当前正在运行的进程列表

我使用过的Linux命令之ps - 显示当前正在运行的进程列表

本文链接:http://codingstandards.iteye.com/blog/841996   (转载请注明出处)

用途说明

ps命令是Process Status的缩写(ps is the shortage for Process Status.)。ps命令用来列出系统中当前运行的那些进程(The command should be used to display the currently running processes on Unix/Linux systems.)。ps命令列出的是当前那些进程的快照(report a snapshot of the current processes),就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信息,就可以使用top命令(ps displays information about a selection of the active processes. If you want a repetitive update of the selection and the displayed information, use top(1) instead.)。

 

常用参数

ps命令参数可以支持不同的风格,而且又多,实在很难一一列出,下面只列出常用的方式。列位看官也可以补充一下您的常用方式。

 

格式:ps

只列出当前终端有关的进程。

 

格式:ps -ef

格式:ps aux     <== BSD syntax

显示所有的进程(To see every process on the system using standard syntax)。通常和grep/egrep等命令配合使用来查找相关的进程。


格式:COLUMNS=1024 ps h -C java -f

显示java进程的完整信息。(h表明不显示头部信息,-C指定命令名称,-f列出全部信息)

 

使用示例

示例一 查看当前shell中的进程

[root@web ~]# ps
  PID TTY          TIME CMD
 5488 pts/12   00:00:00 bash
29720 pts/12   00:00:00 ps
[root@web ~]# ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0  5488 26086  0  75   0 -  1753 wait   pts/12   00:00:00 bash
4 R     0 29727  5488  0  76   0 -  1320 -      pts/12   00:00:00 ps
[root@web ~]#

 

示例二 ps与grep配合查找进程

通常查找进程的目的是确认程序是否正在运行、或者准备把它kill掉。

[root@web imx_server]# ps -ef|grep update.sh  
root     10683 17963  0 13:52 pts/7    00:00:00 /bin/sh ./update.sh
root     26342  5488  0 16:25 pts/12   00:00:00 grep update.sh
[root@web imx_server]# ps -ef|grep update.sh|grep -v grep
root     10683 17963  0 13:52 pts/7    00:00:00 /bin/sh ./update.sh

[root@web imx_server]# kill 10683
[root@web imx_server]#

 

示例三 查看java进程的完整信息

使用ant启动的java进程命令行都相当的长。

[root@web imx_server]# COLUMNS=1024 ps h -C java -f
root      1572     1  0 Nov15 ?        Ssl   29:34 /usr/java/jdk1.6.0_20/jre/bin/java -classpath /opt/apache/apache-ant-1.8.1/lib/ant-launcher.jar -Dant.home=/opt/apache/apache-ant-1.8.1 -Dant.library.dir=/opt/apache/apache-ant-1.8.1/lib org.apache.tools.ant.launch.Launcher -cp  start
root      1586  1572  0 Nov15 ?        Sl    50:01 /usr/java/jdk1.6.0_20/jre/bin/java -Xmx1024m -classpath /opt/web/web_server/web.server.jar:/opt/web/web_server:/opt/web/web_server/lib/apache-mime4j-0.6.jar:/opt/web/web_server/lib/commons-beanutils-1.8.3.jar:/opt/web/web_server/lib/commons-codec-1.3.jar:/opt/web/web_server/lib/commons-logging-1.1.1.jar:/opt/web/web_server/lib/freemarker.jar:/opt/web/web_server/lib/httpclient-4.0.1.jar:/opt/web/web_server/lib/httpcore-4.0.1.jar:/opt/web/web_server/lib/httpmime-4.0.1.jar:/opt/web/web_server/lib/hyjc.jar:/opt/web/web_server/lib/jdom-1.1.1.jar:/opt/web/web_server/lib/json.jar:/opt/web/web_server/lib/junit-4.8.1.jar:/opt/web/web_server/lib/log4j-1.2.16.jar:/opt/web/web_server/lib/mysql-connector-java-5.1.13-bin.jar:/opt/web/web_server/lib/poi-3.6-ZHY-20100522.jar:/opt/web/web_server/lib/proxool-0.9.1.jar:/opt/web/web_server/lib/proxool-cglib.jar:/opt/web/web_server/lib/servlet-api.jar:/opt/web/web_server/lib/sms.client.jar web.server.Main
root      4702  4698  0 Aug11 ?        Sl    27:43 /usr/java/jdk1.6.0_20/bin/java -classpath /root/work190/mhr_user/derby/lib/derby.jar:/root/work190/mhr_user/derby/lib/derbynet.jar:/root/work190/mhr_user/derby/lib/derbytools.jar:/root/work190/mhr_user/derby/lib/derbyclient.jar org.apache.derby.drda.NetworkServerControl start
root      7233     1  0 Nov02 ?        Ssl   33:07 /usr/java/jdk1.6.0_20/jre/bin/java -classpath /opt/apache/apache-ant-1.8.1/lib/ant-launcher.jar -Dant.home=/opt/apache/apache-ant-1.8.1 -Dant.library.dir=/opt/apache/apache-ant-1.8.1/lib org.apache.tools.ant.launch.Launcher -cp  start
root      7262  7233  0 Nov02 ?        Sl    57:55 /usr/java/jdk1.6.0_20/jre/bin/java -Xmx1024m -classpath /opt/imx/iss_server/iss.server.jar:/opt/imx/iss_server:/opt/imx/iss_server/lib/commons-beanutils-1.8.3.jar:/opt/imx/iss_server/lib/commons-logging-1.1.1.jar:/opt/imx/iss_server/lib/hyjc.jar:/opt/imx/iss_server/lib/ida.api.jar:/opt/imx/iss_server/lib/jdom-1.1.1.jar:/opt/imx/iss_server/lib/json.jar:/opt/imx/iss_server/lib/junit-4.8.1.jar:/opt/imx/iss_server/lib/log4j-1.2.16.jar:/opt/imx/iss_server/lib/mysql-connector-java-5.1.13-bin.jar:/opt/imx/iss_server/lib/poi-3.6-ZHY-20100113.jar:/opt/imx/iss_server/lib/proxool-0.9.1.jar:/opt/imx/iss_server/lib/proxool-cglib.jar:/opt/imx/iss_server/lib/servlet-api.jar:/opt/imx/iss_server/lib/sms.client.jar iss.server.Main
root      8488     1  0 Oct14 ?        Ssl  102:53 /usr/java/jdk1.6.0_20/jre/bin/java -classpath /opt/apache/apache-ant-1.8.1/lib/ant-launcher.jar -Dant.home=/opt/apache/apache-ant-1.8.1 -Dant.library.dir=/opt/apache/apache-ant-1.8.1/lib org.apache.tools.ant.launch.Launcher -cp  start
root      8502  8488  0 Oct14 ?        Sl   259:35 /usr/java/jdk1.6.0_20/jre/bin/java -Xmx1024m -classpath /root/work190/mhr_server/mhr.server.jar:/root/work190/mhr_server:/root/work190/mhr_server/lib/activation.jar:/root/work190/mhr_server/lib/admin.client.jar:/root/work190/mhr_server/lib/ant.jar:/root/work190/mhr_server/lib/commons-beanutils.jar:/root/work190/mhr_server/lib/commons-email-1.1.jar:/root/work190/mhr_server/lib/commons-logging-1.1.1.jar:/root/work190/mhr_server/lib/dsmp.client.jar:/root/work190/mhr_server/lib/ems.client.jar:/root/work190/mhr_server/lib/filters.jar:/root/work190/mhr_server/lib/htmlparser.jar:/root/work190/mhr_server/lib/hyjc.jar:/root/work190/mhr_server/lib/jcharset.jar:/root/work190/mhr_server/lib/jcommon-1.0.13.jar:/root/work190/mhr_server/lib/jdom-1.1.1.jar:/root/work190/mhr_server/lib/jfreechart-1.0.10.jar:/root/work190/mhr_server/lib/json-lib-2.2.2-jdk15.jar:/root/work190/mhr_server/lib/json.jar:/root/work190/mhr_server/lib/junit-4.1.jar:/root/work190/mhr_server/lib/log4j-1
root     15239     1  0 Oct18 ?        Ssl  115:03 /usr/java/jdk1.6.0_20/jre/bin/java -classpath /opt/apache/apache-ant-1.8.1/lib/ant-launcher.jar -Dant.home=/opt/apache/apache-ant-1.8.1 -Dant.library.dir=/opt/apache/apache-ant-1.8.1/lib org.apache.tools.ant.launch.Launcher -cp  start
root     18066     1  1 Dec03 ?        Sl   176:47 /usr/java/jdk1.6.0_20/bin/java -Djava.util.logging.config.file=/opt/apache/apache-tomcat-6.0.26/conf/logging.properties -Xms512m -Xmx1024m -XX:MaxNewSize=256m -XX:MaxPermSize=256m -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/opt/apache/apache-tomcat-6.0.26/endorsed -classpath /opt/apache/apache-tomcat-6.0.26/bin/bootstrap.jar -Dcatalina.base=/opt/apache/apache-tomcat-6.0.26 -Dcatalina.home=/opt/apache/apache-tomcat-6.0.26 -Djava.io.tmpdir=/opt/apache/apache-tomcat-6.0.26/temp org.apache.catalina.startup.Bootstrap start
root     26555 26479  0 Dec07 ?        Sl     6:36 java -classpath .:../../lib/jdom-1.1.jar:../../lib/log4j-1.2.15.jar:../../lib/hyjc.jar:../../lib/commons-email-1.1.jar:../../lib/mail.jar:../../lib/activation.jar ems.server.Main
root     27855     1  0 10:01 ?        Sl     0:42 /usr/java/jdk1.6.0_20/jre/bin/java -classpath /opt/apache/apache-ant-1.8.1/lib/ant-launcher.jar -Dant.home=/opt/apache/apache-ant-1.8.1 -Dant.library.dir=/opt/apache/apache-ant-1.8.1/lib org.apache.tools.ant.launch.Launcher -cp  start
root     27872 27855  0 10:01 ?        Sl     3:28 /usr/java/jdk1.6.0_20/jre/bin/java -Xmx1024m -classpath /opt/imx/imx_server/imx.server.jar:/opt/imx/imx_server/ima.server.jar:/opt/imx/imx_server/ida.server.jar:/opt/imx/imx_server:/opt/imx/imx_server/lib/commons-beanutils-1.8.3.jar:/opt/imx/imx_server/lib/commons-logging-1.1.1.jar:/opt/imx/imx_server/lib/ems.client.jar:/opt/imx/imx_server/lib/hyjc.jar:/opt/imx/imx_server/lib/jdom-1.1.1.jar:/opt/imx/imx_server/lib/json.jar:/opt/imx/imx_server/lib/junit-4.8.1.jar:/opt/imx/imx_server/lib/log4j-1.2.16.jar:/opt/imx/imx_server/lib/mysql-connector-java-5.1.12-bin.jar:/opt/imx/imx_server/lib/poi-3.6-ZHY-20100113.jar:/opt/imx/imx_server/lib/proxool-0.9.1.jar:/opt/imx/imx_server/lib/proxool-cglib.jar:/opt/imx/imx_server/lib/servlet-api.jar:/opt/imx/imx_server/lib/sms.client.jar imx.server.Main
[root@web imx_server]#

 

示例四 一个显示java进程的脚本

脚本sj.sh。使用方式:不跟参数显示所有java进程,跟上特征参数则显示包含此特征的java进程,特征参数可以是类名、目录名等。

#!/bin/sh

listpids()
{
        #ps -ef|grep java|grep -v grep
        COLUMNS=1024 ps h -C java -f
}

showpids()
{
        while read u p pp t1 t2 tty cpu cmd;
        do
                ls -l /proc/$p/cwd
                echo $p $cwd $cmd
                echo
        done
}

showpidof()
{
        while read u p pp t1 t2 tty cpu cmd;
        do
                if ls -l /proc/$p/cwd | grep -q $1; then
                        echo $p
                elif echo $cmd | grep -q $1; then
                        echo $p
                fi
        done
}

if [ "$1" ]; then
        listpids | showpidof $1 | xargs
else
        listpids | showpids
fi

 

[root@web imx_server]# sj.sh
lrwxrwxrwx 1 root root 0 12-11 11:09 /proc/1572/cwd -> /opt/web/web_server
1572 29:36 /usr/java/jdk1.6.0_20/jre/bin/java -classpath /opt/apache/apache-ant-1.8.1/lib/ant-launcher.jar -Dant.home=/opt/apache/apache-ant-1.8.1 -Dant.library.dir=/opt/apache/apache-ant-1.8.1/lib org.apache.tools.ant.launch.Launcher -cp start

lrwxrwxrwx 1 root root 0 12-13 10:36 /proc/1586/cwd -> /opt/web/web_server
1586 50:03 /usr/java/jdk1.6.0_20/jre/bin/java -Xmx1024m -classpath /opt/web/web_server/web.server.jar:/opt/web/web_server:/opt/web/web_server/lib/apache-mime4j-0.6.jar:/opt/web/web_server/lib/commons-beanutils-1.8.3.jar:/opt/web/web_server/lib/commons-codec-1.3.jar:/opt/web/web_server/lib/commons-logging-1.1.1.jar:/opt/web/web_server/lib/freemarker.jar:/opt/web/web_server/lib/httpclient-4.0.1.jar:/opt/web/web_server/lib/httpcore-4.0.1.jar:/opt/web/web_server/lib/httpmime-4.0.1.jar:/opt/web/web_server/lib/hyjc.jar:/opt/web/web_server/lib/jdom-1.1.1.jar:/opt/web/web_server/lib/json.jar:/opt/web/web_server/lib/junit-4.8.1.jar:/opt/web/web_server/lib/log4j-1.2.16.jar:/opt/web/web_server/lib/mysql-connector-java-5.1.13-bin.jar:/opt/web/web_server/lib/poi-3.6-ZHY-20100522.jar:/opt/web/web_server/lib/proxool-0.9.1.jar:/opt/web/web_server/lib/proxool-cglib.jar:/opt/web/web_server/lib/servlet-api.jar:/opt/web/web_server/lib/sms.client.jar web.server.Main

lrwxrwxrwx 1 root root 0 12-14 13:31 /proc/4702/cwd -> /root/work190/mhr_user0
4702 27:44 /usr/java/jdk1.6.0_20/bin/java -classpath /root/work190/mhr_user/derby/lib/derby.jar:/root/work190/mhr_user/derby/lib/derbynet.jar:/root/work190/mhr_user/derby/lib/derbytools.jar:/root/work190/mhr_user/derby/lib/derbyclient.jar org.apache.derby.drda.NetworkServerControl start

lrwxrwxrwx 1 root root 0 12-13 10:36 /proc/7233/cwd -> /opt/imx/iss_server
7233 33:10 /usr/java/jdk1.6.0_20/jre/bin/java -classpath /opt/apache/apache-ant-1.8.1/lib/ant-launcher.jar -Dant.home=/opt/apache/apache-ant-1.8.1 -Dant.library.dir=/opt/apache/apache-ant-1.8.1/lib org.apache.tools.ant.launch.Launcher -cp start

lrwxrwxrwx 1 root root 0 12-13 10:36 /proc/7262/cwd -> /opt/imx/iss_server
7262 57:57 /usr/java/jdk1.6.0_20/jre/bin/java -Xmx1024m -classpath /opt/imx/iss_server/iss.server.jar:/opt/imx/iss_server:/opt/imx/iss_server/lib/commons-beanutils-1.8.3.jar:/opt/imx/iss_server/lib/commons-logging-1.1.1.jar:/opt/imx/iss_server/lib/hyjc.jar:/opt/imx/iss_server/lib/ida.api.jar:/opt/imx/iss_server/lib/jdom-1.1.1.jar:/opt/imx/iss_server/lib/json.jar:/opt/imx/iss_server/lib/junit-4.8.1.jar:/opt/imx/iss_server/lib/log4j-1.2.16.jar:/opt/imx/iss_server/lib/mysql-connector-java-5.1.13-bin.jar:/opt/imx/iss_server/lib/poi-3.6-ZHY-20100113.jar:/opt/imx/iss_server/lib/proxool-0.9.1.jar:/opt/imx/iss_server/lib/proxool-cglib.jar:/opt/imx/iss_server/lib/servlet-api.jar:/opt/imx/iss_server/lib/sms.client.jar iss.server.Main

lrwxrwxrwx 1 root root 0 12-13 10:36 /proc/8488/cwd -> /root/work190/mhr_server
8488 102:56 /usr/java/jdk1.6.0_20/jre/bin/java -classpath /opt/apache/apache-ant-1.8.1/lib/ant-launcher.jar -Dant.home=/opt/apache/apache-ant-1.8.1 -Dant.library.dir=/opt/apache/apache-ant-1.8.1/lib org.apache.tools.ant.launch.Launcher -cp start

lrwxrwxrwx 1 root root 0 12-13 10:36 /proc/8502/cwd -> /root/work190/mhr_server
8502 259:40 /usr/java/jdk1.6.0_20/jre/bin/java -Xmx1024m -classpath /root/work190/mhr_server/mhr.server.jar:/root/work190/mhr_server:/root/work190/mhr_server/lib/activation.jar:/root/work190/mhr_server/lib/admin.client.jar:/root/work190/mhr_server/lib/ant.jar:/root/work190/mhr_server/lib/commons-beanutils.jar:/root/work190/mhr_server/lib/commons-email-1.1.jar:/root/work190/mhr_server/lib/commons-logging-1.1.1.jar:/root/work190/mhr_server/lib/dsmp.client.jar:/root/work190/mhr_server/lib/ems.client.jar:/root/work190/mhr_server/lib/filters.jar:/root/work190/mhr_server/lib/htmlparser.jar:/root/work190/mhr_server/lib/hyjc.jar:/root/work190/mhr_server/lib/jcharset.jar:/root/work190/mhr_server/lib/jcommon-1.0.13.jar:/root/work190/mhr_server/lib/jdom-1.1.1.jar:/root/work190/mhr_server/lib/jfreechart-1.0.10.jar:/root/work190/mhr_server/lib/json-lib-2.2.2-jdk15.jar:/root/work190/mhr_server/lib/json.jar:/root/work190/mhr_server/lib/junit-4.1.jar:/root/work190/mhr_server/lib/log4j-1

lrwxrwxrwx 1 root root 0 12-12 09:09 /proc/15239/cwd -> /root/work190/exam_server
15239 115:05 /usr/java/jdk1.6.0_20/jre/bin/java -classpath /opt/apache/apache-ant-1.8.1/lib/ant-launcher.jar -Dant.home=/opt/apache/apache-ant-1.8.1 -Dant.library.dir=/opt/apache/apache-ant-1.8.1/lib org.apache.tools.ant.launch.Launcher -cp start

lrwxrwxrwx 1 root root 0 12-13 10:36 /proc/18066/cwd -> /
18066 177:59 /usr/java/jdk1.6.0_20/bin/java -Djava.util.logging.config.file=/opt/apache/apache-tomcat-6.0.26/conf/logging.properties -Xms512m -Xmx1024m -XX:MaxNewSize=256m -XX:MaxPermSize=256m -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/opt/apache/apache-tomcat-6.0.26/endorsed -classpath /opt/apache/apache-tomcat-6.0.26/bin/bootstrap.jar -Dcatalina.base=/opt/apache/apache-tomcat-6.0.26 -Dcatalina.home=/opt/apache/apache-tomcat-6.0.26 -Djava.io.tmpdir=/opt/apache/apache-tomcat-6.0.26/temp org.apache.catalina.startup.Bootstrap start

lrwxrwxrwx 1 root root 0 12-12 18:25 /proc/26555/cwd -> /opt/omc08/src/ems_proxy
26555 6:37 java -classpath .:../../lib/jdom-1.1.jar:../../lib/log4j-1.2.15.jar:../../lib/hyjc.jar:../../lib/commons-email-1.1.jar:../../lib/mail.jar:../../lib/activation.jar ems.server.Main

lrwxrwxrwx 1 root root 0 12-14 10:02 /proc/27855/cwd -> /opt/imx/imx_server
27855 0:45 /usr/java/jdk1.6.0_20/jre/bin/java -classpath /opt/apache/apache-ant-1.8.1/lib/ant-launcher.jar -Dant.home=/opt/apache/apache-ant-1.8.1 -Dant.library.dir=/opt/apache/apache-ant-1.8.1/lib org.apache.tools.ant.launch.Launcher -cp start

lrwxrwxrwx 1 root root 0 12-14 10:02 /proc/27872/cwd -> /opt/imx/imx_server
27872 3:57 /usr/java/jdk1.6.0_20/jre/bin/java -Xmx1024m -classpath /opt/imx/imx_server/imx.server.jar:/opt/imx/imx_server/ima.server.jar:/opt/imx/imx_server/ida.server.jar:/opt/imx/imx_server:/opt/imx/imx_server/lib/commons-beanutils-1.8.3.jar:/opt/imx/imx_server/lib/commons-logging-1.1.1.jar:/opt/imx/imx_server/lib/ems.client.jar:/opt/imx/imx_server/lib/hyjc.jar:/opt/imx/imx_server/lib/jdom-1.1.1.jar:/opt/imx/imx_server/lib/json.jar:/opt/imx/imx_server/lib/junit-4.8.1.jar:/opt/imx/imx_server/lib/log4j-1.2.16.jar:/opt/imx/imx_server/lib/mysql-connector-java-5.1.12-bin.jar:/opt/imx/imx_server/lib/poi-3.6-ZHY-20100113.jar:/opt/imx/imx_server/lib/proxool-0.9.1.jar:/opt/imx/imx_server/lib/proxool-cglib.jar:/opt/imx/imx_server/lib/servlet-api.jar:/opt/imx/imx_server/lib/sms.client.jar imx.server.Main

[root@web imx_server]# sj.sh imx_server
27855 27872
[root@web imx_server]#

 

问题思考

相关资料

【1】symfony 中文    linux ps命令    
【2】保留心中的一片天    Linux下ps命令详解    
【3】linux.ie     :: New Users :: Beginners Linux Guide :: Using the ps command.     
【4】建站学     Linux下ps查找并用kill命令终止进程     
【5】飘渺的风   Linux ps 详解    
【6】时光漂流瓶    linux中ps命令的用法说明     
【7】鸟哥的Linux私房菜    ps :將某個時間點的程序運作情況擷取下來

 

返回 我使用过的Linux命令系列总目录

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值