다음 두 명령을 결합하고 싶습니다.
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
.