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…

2 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. オテモニャン Says:

    5g6jhbe

    すっごいよ!
    オナニー
    オナニー
    逆援助
    SEX
    フェラチオ
    ソープ
    逆援助
    出張ホスト
    手コキ
    おっぱい
    フェラチオ
    中出し
    セックス
    デリヘル
    包茎
    逆援
    性欲

Leave a Reply