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:

# 2/4/09
# Advanced Shutdown Script
# Ref –


   if ps ax | grep -v grep | grep $SERVICE > /dev/null
      echo “$SERVICE service is running: $(date)” >> /home/user/scripts/adv-shutdown.log
      echo “$SERVICE is NOT running: $(date)” >> /home/user/scripts/adv-shutdown.log

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

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

see also:

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/

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


4 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!

  4. […] pupils visible, but he’s definitely facing forward. Distracted by a crime in the distance? An Animal Man-esque awareness of the reader? Trying to Click

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

%d bloggers like this: