Script to check PID from file and check whether process is running or not
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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).
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.
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.
# 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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.