- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- how to get process ID by using process name or arg...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-12-2006 12:21 AM
тАО10-12-2006 12:21 AM
how to get process ID by using process name or arguments
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-12-2006 12:25 AM
тАО10-12-2006 12:25 AM
Re: how to get process ID by using process name or arguments
how about filtering out the grep ?
ps -ef | grep -v grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-12-2006 12:29 AM
тАО10-12-2006 12:29 AM
Re: how to get process ID by using process name or arguments
##########################################################################
# 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-12-2006 12:35 AM
тАО10-12-2006 12:35 AM
Re: how to get process ID by using process name or arguments
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-12-2006 12:43 AM
тАО10-12-2006 12:43 AM
Re: how to get process ID by using process name or arguments
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-12-2006 12:46 AM
тАО10-12-2006 12:46 AM
Re: how to get process ID by using process name or arguments
The "right" way to filter ps output is to use the '-C
# UNIX95=1 ps -C myproc -o pid=
PCS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-12-2006 03:06 AM
тАО10-12-2006 03:06 AM
Re: how to get process ID by using process name or arguments
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-13-2006 05:00 AM
тАО10-13-2006 05:00 AM
Re: how to get process ID by using process name or arguments
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-13-2006 05:38 AM
тАО10-13-2006 05:38 AM
Re: how to get process ID by using process name or arguments
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-13-2006 03:43 PM
тАО10-13-2006 03:43 PM
Re: how to get process ID by using process name or arguments
COUNT=$(echo "$MYPID")
should be:
COUNT=$(echo "$MYPID" | wc -w)
but Peter and spex simplified it even further.
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-15-2006 08:31 PM
тАО10-15-2006 08:31 PM
Re: how to get process ID by using process name or arguments
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-15-2006 11:06 PM
тАО10-15-2006 11:06 PM
Re: how to get process ID by using process name or arguments
Thanks,
madhava