Find the answer to your Linux question:
Results 1 to 6 of 6
Hi, I tried to get process ID using pidof. It didn't give any error but a blank output at console Code: $ pidof -s instance1 $ But when I use ...
  1. #1
    Just Joined!
    Join Date
    Jan 2011
    Posts
    9

    [SOLVED] How to get process ID using pidof

    Hi,

    I tried to get process ID using pidof. It didn't give any error but a blank output at console
    Code:
    $ pidof -s instance1
    
    $

    But when I use ps -ef, I get the process ID
    Code:
    $ ps -ef | grep instance1
    root      4174 21661  0 06:52 pts/1    00:00:00 grep instance1
    provgw   30220 30219 28 06:46 pts/1    00:01:44 /usr/java/jdk1.6.0_18/bin/java -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=/opt/provgw/tomcat/instance1/conf/logging.properties -Dinstance_id=instance1 -Dglobal.conf=/etc/provgw -Daxis.xml.reuseParsers=true -Xbatch -dsa -Xrs -Xss128k -XX:+UseParallelGC -XX:ParallelGCThreads=20 -XX:+AggressiveOpts -Xms10097514k -Xmx10097514k -Xmn3029255k -XX:MaxPermSize=256m -XX:+CMSClassUnloadingEnabled -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n -Djava.endorsed.dirs=/opt/provgw/tomcat/endorsed -classpath :/opt/provgw/tomcat/bin/bootstrap.jar:/opt/provgw/tomcat/bin/onends-tomcat.jar -Dcatalina.base=/opt/provgw/tomcat/instance1 -Dcatalina.home=/opt/provgw/tomcat -Djava.io.tmpdir=/opt/provgw/tomcat/instance1/temp org.apache.catalina.startup.Bootstrap start
    Actually i'm writing a shell script where I need to get the process ID. So I thought if i can get the process ID directly using "pidof" it'd be more easier. Can someone please help?

  2. #2
    Linux Enthusiast Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    717
    What should this shell script do with that pid?

    hint to why pidof doesn't work: your programs name isn't instance1

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Posts
    9
    Hi,

    I need the process ID to kill the process at some point in the script.

    And I'm sure it's instance1, because immediately after "pidof" command I tried ps -ef as shown in my previous post and i'm able to retrieve the process ID.

    I could have used ps -ef but it's listing unwanted things also along with the process ID. I need only process ID, otherwise I have to write some other logic to extract process ID from this output. If there is any way to get the process ID alone, it's enough for me.

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    The problem here is, I suspect, that there is actually no process called "instance1". The process name is actually "java", and one of its arguments contains "instance1".

    Try pgrep instead, which should work:
    Code:
    pgrep instance1
    pgrep matches against the entire commandline, and not only the name of the process.
    DISTRO=Arch
    Registered Linux User #388732

  5. #5
    Linux Enthusiast Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    717
    even if nearly all roads will get you to rome, i must disagree Cabhan.

    you shouldn't kill jre's (java runtime environments) that tomcat brought up. use the tomcat manager to stop/restart/remove them or restart the tomcat service in case where tomcat is not responding anymore. tomcat could become unstable / unresponsive which could bear bigger problems than just the need of undeploying a unresponsive app.

    my point is: there are administrative tools that come along with tomcat and you should definitly use those over sig_kill!

    look here. btw this was also the reason why i asked why the heck you would need the pid once i read "tomcat" within your reference quotes.

  6. #6
    Just Joined!
    Join Date
    Jan 2011
    Posts
    9
    I know I should not kill tomcat instances just like that. I use start, stop and restart options for this purpose. But in my system i used to face some problems when i try to restart tomcat. instance1 never stops at all. Thats the point when i need to kill it and start it again.

    Anyway I got it
    ps -ef | grep "instance_id=instance$1"|grep -v "grep instance_id=instance$1"|tr -s ' '|cut -d ' ' -f 2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...