如何打印 PID 以及进程转储

如何打印 PID 以及进程转储

我希望结合以下两个命令:

ps -ef| grep -v grep|grep java | awk ' {print "PID is:" $2}';
PID is:515
PID is:22145
PID is:32703

ps -ef| grep -v grep|grep java
wladmin    515   506  0 Nov03 ?        01:11:39 /web/jdk-18.0.1.1/bin/java -jar remoting.jar -workDir /web/jenkinsprod -jar-cache /web/jenkinsprod/remoting/jarCache
wladmin  22145 22140  0 Nov04 ?        00:54:18 /web/jdk-18.0.1.1/bin/java -Djava.io.tmpdir=/web/jenkins_windows/tmp -Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8 -jar remoting.jar -workDir /web/jenkins_windows -jar-cache /web/jenkins_windows/remoting/jarCache
wladmin  32703 32699  0 Nov03 ?        01:10:34 /web/jdk-18.0.1.1/bin/java -Djava.io.tmpdir=/web/jenkins/tmp -Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8 -jar remoting.jar -workDir /web/jenkins -jar-cache /web/jenkins/remoting/jarCache

我如何组合这两个命令来获得以下内容

期望的输出:

PID is:515
Process Dump is:     wladmin    515   506  0 Nov03 ?        01:11:39 /web/jdk-18.0.1.1/bin/java -jar remoting.jar -workDir /web/jenkinsprod -jar-cache /web/jenkinsprod/remoting/jarCache

PID is:22145
Process Dump is:    wladmin  22145 22140  0 Nov04 ?        00:54:18 /web/jdk-18.0.1.1/bin/java -Djava.io.tmpdir=/web/jenkins_windows/tmp -Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8 -jar remoting.jar -workDir /web/jenkins_windows -jar-cache /web/jenkins_windows/remoting/jarCache

答案1

使用awk

$ awk '/java/ {print "PID is:" $2"\nProcess Dump is:\t" $0}' <(ps -ef | grep -v grep)
PID is:515
Process Dump is:    wladmin    515   506  0 Nov03 ?        01:11:39 /web/jdk-18.0.1.1/bin/java -jar remoting.jar -workDir /web/jenkinsprod -jar-cache /web/jenkinsprod/remoting/jarCache
PID is:22145
Process Dump is:    wladmin  22145 22140  0 Nov04 ?        00:54:18 /web/jdk-18.0.1.1/bin/java -Djava.io.tmpdir=/web/jenkins_windows/tmp -Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8 -jar remoting.jar -workDir /web/jenkins_windows -jar-cache /web/jenkins_windows/remoting/jarCache
PID is:32703
Process Dump is:    wladmin  32703 32699  0 Nov03 ?        01:10:34 /web/jdk-18.0.1.1/bin/java -Djava.io.tmpdir=/web/jenkins/tmp -Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8 -jar remoting.jar -workDir /web/jenkins -jar-cache /web/jenkins/remoting/jarCache

答案2

根据 的版本ps,这可能可以列出 pid:

ps -ef -o pid:1,args | grep '[j]ava' | cut -d' ' -f1

请注意,使用grep '[j]ava'它可以避免再次调用grep.

相关内容