LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script to check PID from file and check whether process is running or not (http://www.linuxquestions.org/questions/linux-newbie-8/script-to-check-pid-from-file-and-check-whether-process-is-running-or-not-4175432926/)

rajkiran183 10-18-2012 02:01 PM

Script to check PID from file and check whether process is running or not
 
I have a requirement where we need to check Interface.pid file (this file contains PID of process running.
We have to check every 15 mins whether that process is active or not.
If the process is active we should get process already running information also we should get ps -ef | grep PID output
else we need to execute stop script then start script of interface.

Appreciate your early response.

TB0ne 10-18-2012 02:33 PM

Quote:

Originally Posted by rajkiran183 (Post 4809307)
I have a requirement where we need to check Interface.pid file (this file contains PID of process running.
We have to check every 15 mins whether that process is active or not.
If the process is active we should get process already running information also we should get ps -ef | grep PID output
else we need to execute stop script then start script of interface.

Appreciate your early response.

Ok...post what you've written and tried so far, and we can help.

If you're looking for hints, I'd suggest putting your script into cron so it runs every 15 minutes. There are MANY bash scripting examples of doing just this that you can easily find with a quick Google search. Have you looked there, or tried anything on your own? Just putting "linux bash script check if pid is running" brings back over 4 MILLION hits...the first hit has an example and a tutorial.

rajkiran183 10-19-2012 09:53 AM

Quote:

Originally Posted by TB0ne (Post 4809332)
Ok...post what you've written and tried so far, and we can help.

If you're looking for hints, I'd suggest putting your script into cron so it runs every 15 minutes. There are MANY bash scripting examples of doing just this that you can easily find with a quick Google search. Have you looked there, or tried anything on your own? Just putting "linux bash script check if pid is running" brings back over 4 MILLION hits...the first hit has an example and a tutorial.

Hi,
Thanks for your response.

So far i have written script (also scheduled in cron every 15 mins).

export MYLOCKFILE=/usr/myapp/interface.pid
pid=$(cat $MYLOCKFILE)

if [ -f $MYLOCKFILE ]
then
cat $MYLOCKFILE
ps -ef | grep $pid
else
/usr/myapp/stopmyapp.sh
/usr/myapp/startmyapp.sh
fi

This script work fine in normal scenaio.

Suppose there is hard shutdown of server then interface.pid file will have process id entry.
But actually that process will not be running as it would have been terminated in hard shutdown.

As of now only we can see output in screen but script itself is not understanding whether PID is active or not.

So we want to check whatever pid is there in interface.pid file if this process is actually running it should show output as process already running.
else it should go execute stopmyapp.sh and startmyapp.sh.

TB0ne 10-19-2012 10:33 AM

Quote:

Originally Posted by rajkiran183 (Post 4809947)
Hi,
Thanks for your response.

So far i have written script (also scheduled in cron every 15 mins).
Code:

export MYLOCKFILE=/usr/myapp/interface.pid
pid=$(cat $MYLOCKFILE)

if [ -f $MYLOCKFILE ]
then
        cat $MYLOCKFILE
        ps -ef | grep $pid
else
        /usr/myapp/stopmyapp.sh
        /usr/myapp/startmyapp.sh
fi

This script work fine in normal scenaio.

Suppose there is hard shutdown of server then interface.pid file will have process id entry. But actually that process will not be running as it would have been terminated in hard shutdown. As of now only we can see output in screen but script itself is not understanding whether PID is active or not.

So we want to check whatever pid is there in interface.pid file if this process is actually running it should show output as process already running.
else it should go execute stopmyapp.sh and startmyapp.sh.

Easily done, since you've got what you need already. One way would be to read the PID from the lockfile, and check to see if it's running.
Code:

PID=$(cat $MYLOCKFILE)
$PID=`ps -ef | grep $pidfromfile`
if [ -z "$PID" ] ;  ### Checks if $PID is empty....
  then
    rm $MYLOCKFILE  ### $PID was empty...remove the old lockfile....
    /usr/myapp/startmyapp.sh  #### ...and start the app.  Assuming the lockfile gets created in that script
fi

Untested totally, and there are probably better ways to do it too.

unSpawn 10-19-2012 12:13 PM

..in addition to what TB0ne wrote:
Code:

# The file should exist and not be empty (check matching process or restart):
[ -s "$MYLOCKFILE" ]

# One shouldn't use protected variables:
MYPID=$(cat $MYLOCKFILE)

# Pgrep is easier than 'ps|grep':
REALPID=$(pgrep myapp)

# No running process (start it and fill PID file):
[ -z ${REALPID} ]

# Saved PID matches current process: exit check (else refill PID file):
[ $REALPID -eq $MYPID ]

...and like he said depending on the distribution ready made functions for PID handling may already exist.

TB0ne 10-19-2012 12:28 PM

See? Told you there were better ways of doing it. :)


All times are GMT -5. The time now is 05:49 AM.