1

I wish to combine both the below commands:

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

How can i combine both the commands to get the below

Desired output:

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

2 Answers 2

2

Using 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
1

Depending on which version of ps, this might work to list pids:

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

Note the use of grep '[j]ava' which obviates the need for a second invocation of grep.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .