1847069 Members
5605 Online
110261 Solutions
New Discussion

Re: Process ID.

 
SOLVED
Go to solution
Daniel Hart
Occasional Advisor

Process ID.

Is there a system variable that holds the process id of a job that I can use to identify it later when I want to end/kill the process?
I'm running a lot of java processes that look exactly the same when I do a 'ps -ef' and I need to identify each one individually so they can be shutdown in a specific order.
10 REPLIES 10
Steven E. Protter
Exalted Contributor

Re: Process ID.

[5004#] VAR=`ps -ef | grep pmon`
hebron:/root
[5005#] echo $VAR
root 8243 8185 0 11:22:25 pts/tb 0:00 grep pmon oracle 4586 1 0 09:10:10 , 0:00
ora_pmon_repnew oracle 4528 1 0 09:09:44 , 0:00 ora_pmon_jufdev oracle 4554 1 0
09:09:56 , 0:00 ora_pmon_juftest


That is the reverse tick key, in the uppper left, just below the escape key.

This methodology works in shell scripting as well.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
James R. Ferguson
Acclaimed Contributor

Re: Process ID.

Hi Daniel:

If you are launching background processes from the shell, then the 'pid' can be captured from '$!' thusly and used later:

# my.sh &
# my.sh_pid=$!

Regards!

...JRF...
Pete Randall
Outstanding Contributor

Re: Process ID.

You could adopt the technique that HP uses when starting some of the system's demons. They store the PID in a file when they start it, using a technique like Steven mentioned to capture it, then they refer to the file when it's time to kill it.


Pete

Pete
Colin Topliss
Esteemed Contributor

Re: Process ID.

Only the Java process itself will know what its PID is. You used to be able to get this (not sure what the code for this would be though - seem to recall something like getPid). Then you'd need to find a way to pass this to the parent.

If you're after trying to determine the PID of a Java process initiated from a shell script - no method that I can think of because the UNIX OS takes care of PID allocation at the time the process is started (ie already handed off by the parent process).

What sort of order do you need to shut them down in? Descending order? Even trying to do it by the PIDs in general could be problematic if the PIDs roll over.
Ralph Grothe
Honored Contributor

Re: Process ID.

The HP-UX ps version has a nice feature which can ease parsing output from a ps.
You just have to give ps the environment variable UNIX95 (see man ps)

e.g.

SSHD_PID=$(UNIX95= ps -e -o pid= -o ppid= -o comm=|awk '$3~/sshd/&&$2==1{print $1}')

$ echo $SSHD_PID
1216

will give you the PID of the parent sshd process
(n.b. most daemon processes place their PID in a file usually called daemon_name.pid

e.g.

$ cat /var/run/sshd.pid
1216
Madness, thy name is system administration
S.K. Chan
Honored Contributor

Re: Process ID.

There isn't a system variable that holds a PID. You have to store the PID that you wanted into a variable in your own script. You may want to investigate if it is possible to shutdown/kill the PPID instead, that way it'll save you the some time from having to deal with each individual processes.
John Poff
Honored Contributor
Solution

Re: Process ID.

Hi,

The $$ variable will return the PID of the running process. You can save that to a file and kill it from another script.

JP
Daniel Hart
Occasional Advisor

Re: Process ID.

I understand, but if I do a search for 'java' instead of 'pmon' the restriction on the display shows me 1/2 a dozen processes that look identical. Is there anyway of 'grabbing' the process ID at the moment the job is started? - I could then put the id into a file and retreive it later when I wanted to stop that particular java process.
Ralph Grothe
Honored Contributor

Re: Process ID.

Apropos, somtimes I need to list a PID hirarchy tree, e.g. when I want to do an "aggregated kill" like what the Linux killall command does.

Before searching long for such a tool (there must be thousands out there) I hacked a little Perl scriplet (see attachment), which uses that nice feature of HP-UX's ps.

So it can be used like such (lsppid being the name of that script in this case)

# ps -fp "$(/usr/local/sbin/lsppid 25512)"
UID PID PPID C STIME TTY TIME COMMAND
root 1 0 0 Mar 26 ? 0:37 init
root 628 1 0 Mar 26 ? 1:00 /usr/sbin/inetd -l
root 25407 628 0 Mar 30 pts/td 0:00 telnetd -b /etc/issue
oracle 25512 25408 0 Mar 30 pts/td 0:52 sqlplus
oracle 25408 25407 0 Mar 30 pts/td 0:48 -ksh
Madness, thy name is system administration
Daniel Hart
Occasional Advisor

Re: Process ID.

Thanks, everyone. It seems that the $$ variable was the one I was looking for. Unfortunately as I'm trying to spawn a java process from within a script I cannot seem to grab the eventual java process id - BUT I do seem to be able to grab the process id immediately prior to it ( sort of like the parent ID I suppose). This id always appears to be one less than the process I'm after (eg 2344 instead of 2345). It gives me something to work with anyway.
Thanks again for all your time.
Dan.