Operating System - HP-UX
1824485 Members
3416 Online
109671 Solutions
New Discussion юеВ

how to get process ID by using process name or arguments

 
madhavabk
Regular Advisor

how to get process ID by using process name or arguments

Hi All,

I am using grep command with ps -ef command to get process id of my required matching process names. But since grep is also listed in this output list i am not able to get exact list, in other words i am not able to avoid grep commands entry in output.

what is the best way to get process ID of required know process names?

Thanks in advance.

Madhava
11 REPLIES 11
Peter Godron
Honored Contributor

Re: how to get process ID by using process name or arguments

Hi,
how about filtering out the grep ?
ps -ef | grep -v grep
Pete Randall
Outstanding Contributor

Re: how to get process ID by using process name or arguments

Along the same lines, we use a script in /usr/local/bin:

##########################################################################
# psg - Process Search
# Substitute for "ps -ef|grep" command string.
##########################################################################
case $# in
0) echo "Error: Argument Expected";
exit;
;;
1) ps -ef | grep $1 | grep -v "grep"
;;
*) echo "Error: Only 1 Argument Allowed";
exit;
;;
esac


Pete

Pete
Peter Godron
Honored Contributor

Re: how to get process ID by using process name or arguments

Hi again,
thinking about what you asked, it seems you may be looking at some sort of process tree ?

If so, please see earlier thread:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1001424
James R. Ferguson
Acclaimed Contributor

Re: how to get process ID by using process name or arguments

Hi:

Using 'grep' to match a process name is fraught with problems! Do this, instead:

# UNIX95= ps -C cron -o pid=

This will find a process whose basename is 'cron' (exactly!) and return one line representing its pid.

There is a space after 'UNIX95=' but NO semicolon. This arms the UNIX95 (XPG4) *only* for the duration of the commandline. The equal sign after the '-o pid' suppresses the header line written by 'ps'. See the manpages for 'ps' for more options and information.

Regards!

...JRF...
spex
Honored Contributor

Re: how to get process ID by using process name or arguments

Hi Madhava,

The "right" way to filter ps output is to use the '-C ' XPG4 option rather than grep:

# UNIX95=1 ps -C myproc -o pid=

PCS
Bill Hassell
Honored Contributor

Re: how to get process ID by using process name or arguments

As pointed out, grep is a very poor tool for ps information. ps has all the tools you need but you do have to spend time with the man page. The UNIX95 variable enables special features but it must be on the same line as ps so it does not affect other commands or libraries.

Note that the -C option is an EXACT match for the process name, no matter how the process was started (/fullpath/procname ./procname, etc) which eliminates mistakes like grep'ing for sh to find the sh shell (and finds instead, ksh, bash, csh, unhashdaemon plus a bunch of user logins and commandline arguments with "sh" somewhere in the string.

Note also that your script should always test for 3 possibilities: no match, one match and more than one match. If the program is never supposed to be run multiple times, your script can report the error. Also useful with the UNIX95 variable is the ability to customize exactly which columns you want and to remove the header line:

MYPROC=myprogram
MYPID=$(UNIX95= ps -C $MYPROC -o pid=)
COUNT=$(echo "$MYPID")
if [ $COUNT -eq 0 ] # not running
then
echo "$MYPROC not found"
elif [ $COUNT -eq 1 ] # run one copy
then
echo "$MYPROC is PID $MYPID"
else
echo "$MYPROC running more than once: $MYPID"
fi


Bill Hassell, sysadmin
Peter Nikitka
Honored Contributor

Re: how to get process ID by using process name or arguments

Hi,

there are some glitches in Bill's solution (sorry) - it is not enough to do just that last 'echo':
>>
MYPROC=myprogram
MYPID=$(UNIX95= ps -C $MYPROC -o pid=)
COUNT=$(echo "$MYPID")
<<

Better feed all information into an array and deal with this, e.g.:
MYPROC=myprogram
set -A MYPIDs=$(UNIX95= ps -C $MYPROC -o pid=)
case ${#MYPIDs} in
0) print $myprogram is NOT running ;;
1) print $myprogram is running with pid=$MYPIDs ;;
*) print ${#MYPIDs} instances of $myprogram are running with pids ${MYPIDs[*]} ;;
esac


mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
spex
Honored Contributor

Re: how to get process ID by using process name or arguments

Hi,

Another way to get the number of processes with the same name running is to pipe the output from 'ps' to 'wc':

PROC=myprogram
COUNT=$(UNIX95=1 ps -C ${PROC} -o pid= | wc -w)

PCS
Bill Hassell
Honored Contributor

Re: how to get process ID by using process name or arguments

Thanks Peter. The line in my example:

COUNT=$(echo "$MYPID")

should be:

COUNT=$(echo "$MYPID" | wc -w)

but Peter and spex simplified it even further.


Bill Hassell, sysadmin
Peter Godron
Honored Contributor

Re: how to get process ID by using process name or arguments

Hi,
can you please update this thread.
IS this problem resoved, if so please read http://forums1.itrc.hp.com/service/forums/helptips.do?#28 , otherwise please add your comments.
madhavabk
Regular Advisor

Re: how to get process ID by using process name or arguments

Thanks a lot for your valuable discussion and ways shown to me.

Thanks,
madhava