Tracking process via ps command | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Tracking process via ps command

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-14-2013
learnbash learnbash is offline
Banned
 
Join Date: Mar 2008
Last Activity: 28 July 2014, 4:29 PM EDT
Posts: 364
Thanks: 31
Thanked 3 Times in 3 Posts
Tracking process via ps command

Hello folks,

I am tracking a process httpd only. But when i am grepping it, it is returning me multiple process of httpd, second it is showing another process of monitorix-httpd. Below commands i have tried.


Code:
Current output

# ps ax |grep http
  929 ?        Ss     0:00 monitorix-httpd listening on 8080
 9551 ?        Ss     0:00 /usr/sbin/httpd -k start
 9552 ?        S      0:00 /usr/sbin/httpd -k start
 9554 ?        S      0:00 /usr/sbin/httpd -k start
 9555 ?        S      0:00 /usr/sbin/httpd -k start
 9556 ?        S      0:00 /usr/sbin/httpd -k start
 9557 ?        S      0:00 /usr/sbin/httpd -k start
 9558 ?        S      0:00 /usr/sbin/httpd -k start
 9559 ?        S      0:00 /usr/sbin/httpd -k start
 9560 ?        S      0:00 /usr/sbin/httpd -k start
 9561 ?        S      0:00 /usr/sbin/httpd -k start



Expected output: one process that confirms that httpd is running, not all httpd child process and neither monitorx process.


Code:
expected output


httpd


Last edited by learnbash; 09-14-2013 at 04:40 AM.. Reason: correction of process name
Sponsored Links
    #2  
Old 09-14-2013
ahamed101's Avatar
ahamed101 ahamed101 is offline Forum Advisor  
root is god!!!
 
Join Date: Sep 2008
Last Activity: 15 May 2014, 2:54 PM EDT
Location: San Jose, CA
Posts: 1,906
Thanks: 54
Thanked 485 Times in 478 Posts
Why not use pgrep ?


Code:
pgrep -nf /usr/sbin/httpd

This will return you the pid of the latest httpd process.

--ahamed
The Following User Says Thank You to ahamed101 For This Useful Post:
learnbash (09-14-2013)
Sponsored Links
    #3  
Old 09-14-2013
learnbash learnbash is offline
Banned
 
Join Date: Mar 2008
Last Activity: 28 July 2014, 4:29 PM EDT
Posts: 364
Thanks: 31
Thanked 3 Times in 3 Posts
If i don't want to mention the path. Below command is fine?


Code:
# pgrep  -n ^httpd$
9561

# echo $?
0

cross-checking

# pgrep  -n ^httpd1$


# echo $?
1

    #4  
Old 09-14-2013
ahamed101's Avatar
ahamed101 ahamed101 is offline Forum Advisor  
root is god!!!
 
Join Date: Sep 2008
Last Activity: 15 May 2014, 2:54 PM EDT
Location: San Jose, CA
Posts: 1,906
Thanks: 54
Thanked 485 Times in 478 Posts
Yes, that should do.
And btw you can check the output of pgrep directly rather than checking the exit status.

--ahamed
The Following User Says Thank You to ahamed101 For This Useful Post:
learnbash (09-14-2013)
Sponsored Links
    #5  
Old 09-14-2013
learnbash learnbash is offline
Banned
 
Join Date: Mar 2008
Last Activity: 28 July 2014, 4:29 PM EDT
Posts: 364
Thanks: 31
Thanked 3 Times in 3 Posts
Thanks it is clear now.
Sponsored Links
    #6  
Old 09-14-2013
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 17 September 2014, 4:59 AM EDT
Location: Simplicity
Posts: 1,890
Thanks: 120
Thanked 553 Times in 501 Posts

Code:
pgrep '^httpd$'

is the same as

Code:
pgrep -x httpd

Instead of -n (newest) I would take -o (oldest), or pick the one that is spawned by init (PID 1)

Code:
pgrep -P 1 -x httpd

NB in Solaris zones the latter does not work because init is not 1 (and even got a different name).
The Following User Says Thank You to MadeInGermany For This Useful Post:
learnbash (09-14-2013)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Tracking Process to a particular thmnetwork Red Hat 1 07-06-2011 08:17 PM
Tracking down the problem mattmanuel UNIX for Advanced & Expert Users 3 11-16-2007 08:51 AM
Tracking user panknil UNIX for Advanced & Expert Users 5 06-25-2007 11:40 AM
Command Tracking mahatma UNIX for Advanced & Expert Users 5 04-04-2006 08:30 AM
process command tonoche UNIX for Dummies Questions & Answers 2 09-28-2004 10:31 PM



All times are GMT -4. The time now is 01:32 PM.