Find the answer to your Linux question:
Results 1 to 9 of 9
This is a sample script I have: #!/bin/bash pidFile=/tmp/.NDSP.pid if !(test -f $pidFile) then #find the process id from the process name ps -ef | grep NDSP | grep -v ...
  1. #1
    STJ
    STJ is offline
    Just Joined!
    Join Date
    Jul 2007
    Posts
    5

    Bash script help

    This is a sample script I have:
    #!/bin/bash

    pidFile=/tmp/.NDSP.pid
    if !(test -f $pidFile)
    then
    #find the process id from the process name
    ps -ef | grep NDSP | grep -v grep
    if (test $? -eq 0)
    then
    echo "Process already running"
    else
    echo "Process not running. O.k. to Start server"
    fi
    fi
    When I run the ps -ef | grep NDSP | grep -v grep; echo $? from the cmd line, it returns a 0 if the process is running and 1 otherwise.
    However, when i execute the script as: bash -x ./StartNDSP, it shows the following even when the process is not running:
    + ps -ef
    + grep NDSP
    + grep -v grep
    + grep -v bash
    + ps -ef
    + grep NDSP
    + grep -v grep
    vrrapadm 22012 4690 0 21:21 pts/1 00:00:00 bash -x ./StartNDSP
    + test 0 -eq 0
    + echo 'Process already running'
    Process already running
    + exit 1
    ## I can get around this by adding: a grep -v bash to the ps -ef | grep NDSP | grep -v grep | grep -v bash. But is that the right way or how do I get around the shell spawned by the script

  2. #2
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,047
    Code:
    pidFile=/tmp/.NDSP.pid
    if !(test -f $pidFile)
    then
    #find the process id from the process name
    ps -ef | grep NDSP | grep -v grep
    if (test $? -eq 0)
    then
    echo "Process already running"
    else
    echo "Process not running. O.k. to Start server"
    fi
    fi
    You'll want to use square whatchacallid, these [ ] instead of ( )

    Perhaps rewriting the rest a bit might help

    Code:
    pidFile=/tmp/.NDSP.pid
    if [ ! -f $pidFile ] ; then # Look for the existence of the NDSP-process
        PROCESS=`ps -ef | grep NDSP | grep -v grep`
        if [ $PROCESS ] ; then 
            echo "Process already running"
        else
            echo "Process not running. O.k. to Start server"
        fi
    fi
    Still, the script doesn't contain instructions on what to do when $pidFile exists. I'll leave that up to you

    PS: I haven't tested, so it's academic as far as I'm concerned.
    Can't tell an OS by it's GUI

  3. #3
    STJ
    STJ is offline
    Just Joined!
    Join Date
    Jul 2007
    Posts
    5
    Thanks for your response. I had intentionally left out the "if file exists" part. That works. This was only for the file does not exist part.
    The OS is Linux (Red Hat 3.4.4-2).
    I tried your suggestion and this is what I got when I executed the script as
    bash -x ./StartNDSP (i.e when the pid file did not exist and the process was running)
    bash -x ./StartNDSP
    ++ ps -ef
    ++ grep NDPS
    ++ grep -v grep
    + PROCESS='vrrapadm 22613 4690 0 09:54 pts/1 00:00:00 bash -x ./StartNDSP
    vrrapadm 22614 22613 0 09:54 pts/1 00:00:00 bash -x ./StartNDSP'
    + '[' vrrapadm 22613 4690 0 09:54 pts/1 00:00:00 bash -x ./StartNDSP vrrapadm 22614 22613 0 09:54 pts/1 00:00:00 bash -x ./StartNDSP ']'
    ./StartNDSP: line 17: [: too many arguments
    + echo 'Process not running. O.k. to Start server'
    Process not running. O.k. to Start server

    ##As you see, the debug says that the process is not running and it's o.k .to start, though the process is running.
    I think it may be the bash shell's that are the culprit.

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Indeed.

    Do you have the 'pidof' utility available? This determines the PID of a given program. Check its man page for the full docs, but this will simplify your issue incredibly.
    DISTRO=Arch
    Registered Linux User #388732

  5. #5
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,047
    This cures the script. Told ya I didn't test before posting
    Code:
    PROCESS=`ps -ef | grep -v grep | grep -c NDPS`
    Can't tell an OS by it's GUI

  6. #6
    STJ
    STJ is offline
    Just Joined!
    Join Date
    Jul 2007
    Posts
    5
    Thanks but the probelm encountered after trying "PROCESS=`ps -ef | grep -v grep | grep -c NDPS`" is that when the file is not there and the process is not up, the script still reports that the "Process is already running". It still reports the PROCESS count as a positive number.

    bash -x ./StartNDSP
    + pidFile=/tmp/.NDSP.pid
    + '[' '!' -f /tmp/.NDSP.pid ']'
    ++ ps -ef
    ++ grep -v grep
    ++ grep -c NDSP
    + PROCESS=2
    + '[' 0 -eq 0 ']'
    + echo 'Process already running'
    Process already running
    + exit 1

  7. #7
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Hi,

    Possibly you have a typo in your script, can you post the script between code brackets?

    Regards

  8. #8
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,047
    Quote Originally Posted by STJ
    Thanks but the probelm encountered after trying "PROCESS=`ps -ef | grep -v grep | grep -c NDPS`" is that when the file is not there and the process is not up, the script still reports that the "Process is already running". It still reports the PROCESS count as a positive number.
    Yeah, well, it still finds itself running. How to fix that depends on how you call the entire piece of script. It may or may not do this when run as a whole.

    Anyway, this works:
    #!/bin/bash
    Code:
    # Generic File/Process finder
    # Version    0.01.1 Beta
    # Date        20-07-2007
    #
    
    pidFile=~/tst/$1 #Adjust path as appropriate
    if [ ! -f $pidFile ] ; then # Look for the existence of the $1-process
        echo Physical file not found on drive.... checking processes
        PROCESS=`ps -ef | grep -v grep | grep -v $0 | grep -c $1`
        if [ $PROCESS = 0 ] ; then 
            echo "Process not running. O.k. to Start server"
        else
            echo "Process already running"
        fi
    else
    echo Found
    fi
    Can't tell an OS by it's GUI

  9. #9
    STJ
    STJ is offline
    Just Joined!
    Join Date
    Jul 2007
    Posts
    5
    Yes, the grep -v $0 addition works. Thx, Freston

Posting Permissions

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