ps | grep app 命令不显示grep app本身进程的几种方式
使用ps命令查询进程,常常我们不想打印出"ps | grep app"这个当前进程,比如如下:
- [root@localhost root]# ps -ef | grep java
- root 20070 20049 0 Nov19 ? 00:24:33 /usr/java/jdk1.6.0_07/bin/java
- -Dprogram.name=run.sh -server -Xms512m -Xmx1024m -Xss128k -XX:+UseParallelGC
- -XX:+UseParallelOldGC -XX:PermSize=256m -XX:MaxPermSize=512m
- -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000
- -Djava.net.preferIPv4St
- root 5523 5203 0 10:49 pts/0 00:00:00 grep java
[[email protected] root]# ps -ef | grep java root 20070 20049 0 Nov19 ? 00:24:33 /usr/java/jdk1.6.0_07/bin/java -Dprogram.name=run.sh -server -Xms512m -Xmx1024m -Xss128k -XX:+UseParallelGC -XX:+UseParallelOldGC -XX:PermSize=256m -XX:MaxPermSize=512m -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 -Djava.net.preferIPv4St root 5523 5203 0 10:49 pts/0 00:00:00 grep java
显示java进程的同时还会把“grep java”这个进程打印出来“root 5523 5203 0 10:49 pts/0 00:00:00 grep java”,为了不打印此行,有以下几种方式,大家看着哪个好使吧:
1.ps aux | grep java | grep -v "grep"
2.ps aux|grep \[j]ava 或ps aux|grep [j]ava
如果需要打印出进程号,可以在后面再用awk打印,如下:
ps aux|grep [j]ava | awk ‘{print $2}‘
ps -ef|grep java|awk ‘{if($0~/run.sh/) print $2}‘
ps aux|grep [j]ava | awk ‘{if($0~/run.sh/) print $2}‘
ps aux | grep java | grep -v "grep" | awk ‘{print $2}‘
ps aux | grep java | grep -v "grep" | awk ‘{print $2}‘
ps -ef |grep oracle| awk ‘{print $1,$7}‘
等等写法
如果要kill当前的进程,可以写成,如下杀掉java进程:
ps aux|grep [j]ava | awk ‘{print $2}‘ | xargs kill -9
时间: 2024-10-06 00:16:38