How to add conditional check of whether a process is running before doing rest of script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to add conditional check of whether a process is running before doing rest of script?
# 1  
Old 09-16-2013
How to add conditional check of whether a process is running before doing rest of script?

Hi,

I'm looking at doing a cron job for a script that could take a very short time to complete or a very long time to complete based on variable activity on a server. I don't want to do a weird schedule, but I don't want to be so aggressive that the process attempts to start before another instance has started.

How do I put in a check to see if a process (in this case, "overviewer.py") is still running, and if so, end the script without starting another instance of overviewer.py?

Thank you!
# 2  
Old 09-16-2013
This is what PID files are generally used for.

When you start the first process, save its PID into a file in /var/run/processname.pid to check later.

Next time you want to start another, check

Code:
if [ -f /var/run/processname.pid ] && read PID < /var/run/processname.pid && ps "$PID" >/dev/null 2>/dev/null
then
        echo "Already running as PID $PID"
        exit 1
fi

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 09-16-2013
OK... So I've never done anything like that before...

Here's the script I have that I want to modify.
Code:
#!/bin/bash

echo Overviewer Starting... >>/home/mcmaps/scripts/overviewer.log
date >>/home/mcmaps/scripts/overviewer.log
echo ------------------------- >>/home/mcmaps/scripts/overviewer.log
overviewer.py --verbose --quiet --processes 1 --config=/home/mcmaps/servers/bukkitpvt/overviewer.conf >>/home/mcmaps/scripts/overviewer.log
echo ------------------------- >>/home/mcmaps/scripts/overviewer.log
echo Overviewer Complete >>/home/mcmaps/scripts/overviewer.log
echo ============================================ >>/home/mcmaps/scripts/overviewer.log

An example of the log it creates:
Code:
============================================
Overviewer Starting...
Mon Sep 16 13:57:52 PDT 2013
-------------------------
overviewer.py:290  12313 2013-09-16 13:57:52 INFO     Welcome to Minecraft Overviewer!
observer.py:105    12313 2013-09-16 13:59:37 INFO     Rendered 0 of 0.  100% complete
observer.py:97     12313 2013-09-16 13:59:37 INFO     Rendered 0 of 0.  100% complete
-------------------------
Overviewer Complete
============================================
Overviewer Starting...
Mon Sep 16 14:10:45 PDT 2013
-------------------------
overviewer.py:290  13681 2013-09-16 14:10:45 INFO     Welcome to Minecraft Overviewer!
observer.py:105    13681 2013-09-16 14:12:31 INFO     Rendered 0 of 1808.  0% complete
observer.py:105    13681 2013-09-16 14:12:33 INFO     Rendered 11 of 1808.  0% complete
observer.py:105    13681 2013-09-16 14:12:35 INFO     Rendered 22 of 1808.  1% complete
observer.py:105    13681 2013-09-16 14:12:36 INFO     Rendered 33 of 1808.  1% complete
observer.py:105    13681 2013-09-16 14:12:37 INFO     Rendered 44 of 1808.  2% complete
observer.py:105    13681 2013-09-16 14:12:38 INFO     Rendered 55 of 1808.  3% complete
observer.py:105    13681 2013-09-16 14:12:39 INFO     Rendered 66 of 1808.  3% complete
observer.py:105    13681 2013-09-16 14:12:40 INFO     Rendered 77 of 1808.  4% complete
observer.py:105    13681 2013-09-16 14:12:42 INFO     Rendered 88 of 1808.  4% complete
observer.py:105    13681 2013-09-16 14:12:43 INFO     Rendered 99 of 1808.  5% complete
observer.py:105    13681 2013-09-16 14:12:48 INFO     Rendered 150 of 1808.  8% complete
observer.py:105    13681 2013-09-16 14:12:57 INFO     Rendered 201 of 1808.  11% complete
observer.py:105    13681 2013-09-16 14:13:08 INFO     Rendered 252 of 1808.  13% complete
observer.py:105    13681 2013-09-16 14:13:17 INFO     Rendered 303 of 1808.  16% complete
observer.py:105    13681 2013-09-16 14:13:27 INFO     Rendered 354 of 1808.  19% complete
observer.py:105    13681 2013-09-16 14:13:40 INFO     Rendered 405 of 1808.  22% complete
observer.py:105    13681 2013-09-16 14:13:49 INFO     Rendered 456 of 1808.  25% complete
observer.py:105    13681 2013-09-16 14:14:07 INFO     Rendered 557 of 1808.  30% complete
observer.py:105    13681 2013-09-16 14:14:26 INFO     Rendered 658 of 1808.  36% complete
observer.py:105    13681 2013-09-16 14:14:45 INFO     Rendered 759 of 1808.  41% complete
observer.py:105    13681 2013-09-16 14:15:07 INFO     Rendered 860 of 1808.  47% complete
observer.py:105    13681 2013-09-16 14:15:24 INFO     Rendered 961 of 1808.  53% complete
observer.py:105    13681 2013-09-16 14:15:43 INFO     Rendered 1062 of 1808.  58% complete
observer.py:105    13681 2013-09-16 14:16:05 INFO     Rendered 1163 of 1808.  64% complete
observer.py:105    13681 2013-09-16 14:16:24 INFO     Rendered 1264 of 1808.  69% complete
observer.py:105    13681 2013-09-16 14:16:43 INFO     Rendered 1365 of 1808.  75% complete
observer.py:105    13681 2013-09-16 14:17:04 INFO     Rendered 1466 of 1808.  81% complete
observer.py:105    13681 2013-09-16 14:17:33 INFO     Rendered 1567 of 1808.  86% complete
observer.py:105    13681 2013-09-16 14:17:59 INFO     Rendered 1668 of 1808.  92% complete
observer.py:105    13681 2013-09-16 14:18:21 INFO     Rendered 1769 of 1808.  97% complete
observer.py:97     13681 2013-09-16 14:18:28 INFO     Rendered 1808 of 1808.  100% complete
-------------------------
Overviewer Complete
============================================

What I see if I run it again and then view "top" in another terminal session.
Code:
  PID USER      PR  NI  VIRT  RES  SHR S  %CPU %MEM    TIME+  COMMAND
15228 mcmaps    20   0  115m  33m 5080 R 100.1  0.9   0:06.94 overviewer.py

But the PID changes each time I run the script. So how would I know what to store (PID wise) in "/var/run/processname.pid" and how would I know what to look for?

Totally lost. Sorry. :-(
# 4  
Old 09-16-2013
That's why you save it in a PID file, so you know what to check.

Code:
#!/bin/bash

if [ -f /tmp/overviewer.pid ] && read PID < /tmp/overviewer.pid && ps "$PID" >/dev/null 2>/dev/null
then
        echo "Overviewer already running as PID $PID" >&2
        exit 1
fi

# Automatically delete PID file on exit
trap "rm /tmp/overviewer.pid" EXIT

# Automatically append stdout to this file
exec 1>>/home/mcmaps/scripts/overviewer.log

echo Overviewer Starting...
date
echo -------------------------

# Run this in the background with &
overviewer.py --verbose --quiet --processes 1 --config=/home/mcmaps/servers/bukkitpvt/overviewer.conf &

echo "$!" > /tmp/overviewer.pid

wait

echo -------------------------
echo Overviewer Complete
echo ============================================


Last edited by Corona688; 09-16-2013 at 07:05 PM..
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 09-16-2013
You improved my Linux life immensely today with one character: &

Thank you for that lesson! Awesome.

So, in the interest of trying to learn from you and not just steal the work you've done...

I'm confused by the sequence of things here. The "trap" command (which I've never used) is early in the script. That doesn't delete stuff before we can use it? And does this end the script if overviewer is already in progress or wait for one instance to end and then fires off a new one immediately. I'm confused by the "wait" command.

Sorry for being such a newbie.

---------- Post updated at 02:58 PM ---------- Previous update was at 02:57 PM ----------

Oh the wait probably has to do with a background process... something I've not done before in my scripts. I think I'm catching up now.
# 6  
Old 09-16-2013
Quote:
Originally Posted by nbsparks
I'm confused by the sequence of things here. The "trap" command (which I've never used) is early in the script. That doesn't delete stuff before we can use it?
I put it in a trap so that, if you ctrl-C this or it gets KILL-ed, it still gets rid of /tmp/overviewer.pid

The trap command is meant to respond to software interrupts ( giving it a chance to respond to ctrl-C or a KILL command, for instance), and also has the special EXIT condition for "code to run when the program quits for whatever reason".

Quote:
And does this end the script if overviewer is already in progress
Yes, see the exit 1.

Quote:
I'm confused by the "wait" command.
It waits for all background process to finish before continuing.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 09-16-2013
OK... Starting to understand more. And now I have to go read MAN pages for each of these. I try to do that each time I learn from others on this forum.

So, can I still pipe (>>) after a process run in background?

i.e.
Code:
overviewer.py --verbose --quiet --processes 1 --config=/home/mcmaps/servers/bukkitpvt/overviewer.conf & >>/home/mcmaps/scripts/overviewer.log

Or wait... you did something with:
Code:
# Automatically append stdout to this file
exec 1>>/home/mcmaps/scripts/overviewer.log

So that captures everything?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Web Development

Javascript to check field is empty then execute rest of script

I have found this bit of code that nearly does what I want. Basically 3 input fields, I want to copy t2 to t3 as it's typed but only if t1 contains data AND t3 is empty: <input type="text" id="t1" /> <input type="text" id="t2" /> <input type="text" id="t3" /> <script> var t2 =... (4 Replies)
Discussion started by: barrydocks
4 Replies

2. Shell Programming and Scripting

ksh script to check if certain process is running

I have to kill the process "test" for a maintenance I do but want the script to check when it comes back up. I can get what I want when I run this while loop: while true;do ps -ef | grep test | grep -v grep | sed -e 's/^*//';sleep 60;done but I want the script to do it for me and as soon as... (6 Replies)
Discussion started by: seekryts15
6 Replies

3. UNIX for Dummies Questions & Answers

How a process can check if a particular process is running on different machine?

I have process1 running on one machine and generating some log file. Now another process which can be launched on any machine wants to know if process1 is running or not and also in case it is running it wants to stream the logs file generated by process1 on terminal from which process2 is... (2 Replies)
Discussion started by: saurabhnsit2001
2 Replies

4. Shell Programming and Scripting

Simple Script to Check running Process

#!/bin/sh CHECK='ps aux | grep start.jar | grep -v grep | wc -l' if then /usr/local/jre-1.7.0/bin/java - jar start.jar & else fi Could anybody advise whats up with this code im trying to put this in as a cron job to check that solr search engine is running every 10secs and if... (10 Replies)
Discussion started by: will_123
10 Replies

5. Shell Programming and Scripting

Script to check running of process

Hi, Can anyone please tell me how to write a shell script to check whether a process if running or not.... if its still running then wait for sometime and if not then run the next query. Also, Under my one main script main.sh I have to run 2 scripts simutaneously which take some time to... (2 Replies)
Discussion started by: lovepujain
2 Replies

6. Programming

How do I check if a process is running in C

How to I check if a process is running in C? I'm trying to use ps aux |grep "process name" but failing in doing that. How do I do that? Thanks, (1 Reply)
Discussion started by: cprogdude
1 Replies

7. Shell Programming and Scripting

How to write an expect script to check if a process is running?

I'm new to expecting and i want to create a script to ssh to a device,check is a process is running and display that the process is running or not.This is what i have so far After executing this script i get an error. #!/usr/bin/expect set timeout -1 set ip "machine ip goes here" set... (5 Replies)
Discussion started by: icchi
5 Replies

8. Shell Programming and Scripting

script to check if process is running

How do I make a shell script to see if a certain process is running. The process shows up on ps aux as /usr/sbin/daemon eg: if /usr/sbin/daemon else #nothin basically i want to run the process if it isnt running/ has been stopped. Thanks. (2 Replies)
Discussion started by: daydreamer
2 Replies

9. UNIX for Advanced & Expert Users

how to check if a process is running in a server from shell script.

I want to write a unix shell script that will check if a process (say debu) is running in the server or not. If no , then send a mail to the corresponding person to start the process??? (2 Replies)
Discussion started by: debu
2 Replies

10. Shell Programming and Scripting

script to check for a particular process and alert if its not running

Hai Friends, I have to develop a script that checks for a particular process say x.exe once every day and report if its not running say through a blat mail. I need this to run in a given list of pc from a single point. Any suggestion of how to go abt this Thnx for any help (1 Reply)
Discussion started by: goks
1 Replies
Login or Register to Ask a Question