LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-18-2012, 01:01 PM   #1
rajkiran183
LQ Newbie
 
Registered: Oct 2012
Posts: 2

Rep: Reputation: Disabled
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.
 
Old 10-18-2012, 01:33 PM   #2
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,500

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546
Quote:
Originally Posted by rajkiran183 View Post
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.
 
Old 10-19-2012, 08:53 AM   #3
rajkiran183
LQ Newbie
 
Registered: Oct 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
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.
 
Old 10-19-2012, 09:33 AM   #4
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,500

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546
Quote:
Originally Posted by rajkiran183 View Post
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.
 
1 members found this post helpful.
Old 10-19-2012, 11:13 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,166
Blog Entries: 54

Rep: Reputation: 2807Reputation: 2807Reputation: 2807Reputation: 2807Reputation: 2807Reputation: 2807Reputation: 2807Reputation: 2807Reputation: 2807Reputation: 2807Reputation: 2807
..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.
 
1 members found this post helpful.
Old 10-19-2012, 11:28 AM   #6
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,500

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546
See? Told you there were better ways of doing it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Check if a script is already running by saving its PID. Rupadhya Linux - Newbie 2 10-06-2012 08:45 AM
Check if process is running, if not, execute another script Erco21 Linux - Newbie 5 07-12-2011 09:12 AM
[SOLVED] bash script - check process is running, if not start zee# Linux - Newbie 8 02-07-2010 05:50 PM
script to check if process is dead or running rspurlock *BSD 6 04-12-2004 11:32 PM
[script] check for a running process mikshaw Linux - Software 2 01-13-2004 08:33 PM


All times are GMT -5. The time now is 02:37 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration