Advanced Shutdown Script – Part 2 (check for running services)

I’ve updated my “Advanced Shutdown Script” to now check for running services/processes before shutting down, this way the script will check and see if I a set of services are running or not, and if so exit.  The reason why I wanted to have this feature is because my NAS box runs backups and I wouldn’t want it to shutdown while running a backup. Thanks to this page: Check if program is running with bash shell script
It wasn’t hard to implement…
So here it is:

#!/bin/sh
###
# 2/4/09
# adv-shutdown.sh
# Advanced Shutdown Script
# Ref – http://basskozz.wordpress.com/2009/02/04/advanced-shutdown-script-part-2-check-for-services/
###

SERVICE_LIST=’mysecurebackup’
IP_LIST=’
192.168.1.101 192.168.1.102 192.168.1.103 192.168.1.104

for SERVICE  in $SERVICE_LIST; do
   if ps ax | grep -v grep | grep $SERVICE > /dev/null
   then
      echo “$SERVICE service is running: $(date)” >> /home/user/scripts/adv-shutdown.log
      exit
   else
      echo “$SERVICE is NOT running: $(date)” >> /home/user/scripts/adv-shutdown.log
   fi
done

ALIVE=0
for IP  in $IP_LIST; do
   ping -q -c1 -w5 $IP
   if [ $? -eq 0 ]; then
      ALIVE=1
      echo “$IP is alive: $(date)” >> /home/user/scripts/adv-shutdown.log
      break
   fi
done

if [ $ALIVE -eq 0 ]; then
   echo “***ShutDown***  $(date)” >> /home/user/scripts/adv-shutdown.log
   echo “###########################” >> /home/user/scripts/adv-shutdown.log
   /sbin/shutdown -h now
fi

see also: http://pastebin.com/f4e400c22

You can put any number of services in the “SERVICE_LIST” variable separated by a space and it will check if any of them are running. i.e.:

SERVICE_LIST=’program1 program2 program3′

My backup program is called “mysecurebackup” so I only have that one in there right now.
Now I don’t need two separate lines in my ‘sudo crontab’ because the script will check and see if a backup is running, so my sudo crontab looks like this now:

# Run Advanced Shutdown script every 10minutes between 1-5:50am everyday
0,10,20,30,40,50 01-05 * * * /home/user/scripts/adv-shutdown.sh

Now to get back to work on a WOL script to power my NAS box up in the morning, stay tuned…

About these ads

3 Responses to Advanced Shutdown Script – Part 2 (check for running services)

  1. isaaclw says:

    Doing a while loop and calling sleep would work too. Then you would just call it once in crontab.

    Also you could do the “concatenating” in crontab, so that you wouldn’t have to type “>> /home/user/scripts/adv-shutdown.log” everytime there’s output.

  2. Find great Pec Workouts!…

    [...]Advanced Shutdown Script – Part 2 (check for running services) « BassKozz[...]…

  3. koekoek says:

    thanks for sharing, used this as a base to generate some custom script for my nas!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: