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

February 4, 2009

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 – https://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…


Advanced Shutdown Script – Powersaving

February 3, 2009

My NAS box is a power-hungry beast (Ubuntu 8.10 – p4 3ghz 4GB ram, 3x250gb RAID5, 200gb OS), and while I know I could go out and buy a readymade NAS, what would be the fun in that?, and I could also build a VIA or ATOM based system to do simple NAS tasks and replace the power-hungry p4, that would cost money, and I am trying to pinch-a-penny in these economic times…

So I usually leave the NAS box on 24/7 but I am starting to feel bad about that, and my electricity bill is making me feel it
But I need to leave the NAS on at night because it is my media server for my TV’s (XBMC)…
So I created 2 crontab lines:

# Weekdays
30 02 * * 1-5 /sbin/shutdown -h now
# Weekends
30 04 * * 6-7 /sbin/shutdown -h now

This is great, I just turn the NAS on in the morning when I need it… but this isn’t the optimal setting because I usually go to sleep well before 2:30am on weeknights and before 4:30am on weekends, however there are a few nights that I stay up past my bedtime 😛 So I wanted to account for that in crontab. But on average I would have to say I usually go to sleep about 2-3hrs before these times.

So I gots-ta-thinkin and I figured that there must be a better way to handle this with a script, and after a quick question to Linux Questions: Need help creating an advanced shutdown script for power-saving?

And I got my answer and here is my new adv-shutdown.sh script:

#!/bin/sh
###
# 2/3/09
# adv-shutdown.sh
# Advanced Shutdown Script
# Ref – https://basskozz.wordpress.com/2009/02/03/advanced-shutdown-script-powersaving/
###

IP_LIST=’192.168.1.101 192.168.1.102 192.168.1.103 192.168.1.104

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/f3777e743

Simply place this script in your /home/user/scripts directory (if ‘scripts’ directory doesn’t exist ‘mkdir scripts’ from your home directory) replace the IP_LIST ip addresses to match your own (leave a space in-between ip addresses), and replace all of the paths from ‘user’ to your local user account name, and remember to grant execute permissions by issuing “sudo chmod +x adv-shutdown.sh“.
This script will keep a log of shutdown tries in /home/user/scripts/adv-shutdown.log
Now just add this to the root crontab by running ‘sudo crontab -e‘ and enter the following:

###
# 2/3/09
# Advanced Shutdown Script
# https://basskozz.wordpress.com/2009/02/03/advanced-shutdown-script-powersaving/
###
# Run Advanced Shutdown script every 10minutes between 1-5:50am Mon,Tues,Wed,Fri,Sat,Sun
0,10,20,30,40,50 01-05 * * 1,2,3,5,6,7 /home/user/scripts/adv-shutdown.sh
# Run Advanced Shutdown script every 10minutes between 2-5:50am Thurs (S3 Backup Night)
0,10,20,30,40,50 02-05 * * 4 /home/user/scripts/adv-shutdown.sh

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

I created two lines here because my NAS box backs up to Amazon S3 on Thursday nights, so I wanted to give it a little more time to work.

UPDATE – new script:Advanced Shutdown Script – Part 2 (check for running services)


How to setup a RAID5 software (mdadm) array w/ email notifications via Gmail – the easy way

December 7, 2008

Long title I know…
So I finally setup a NAS using Ubuntu Intrepid (8.10) Desktop, at first I was looking into using a pre-built solution such as FreeNAS or Openfiler but that limited the boxes ability to do other things (like serving up webpages using LAMP, etc.) besides I like having a Desktop GUI that I can play with if things go wrong.  So after figuring out what version of Ubuntu to use (Hardy for it’s LTS or Intrepid for latest and greatest) I decided on Intrepid.

My setup:
MoBo: Intel D865GBF Desktop Board
CPU: P4 3.0Ghz
RAM: 4x1GB G.Skill Sticks
VIDEO: FX5200 AGP card
IDE1: Seagate 250GB (ST380013A)
IDE2: Maxtor 250GB (Diamond Max Plus9)
SATA1: Western Digital 320GB (WD3200JD-22KLB0)
SATA2: Seagate 200GB (ST300822AS) – ***UBUNTU INSTALLED ON THIS ONE***

So I will be using IDE1,IDE2, & SATA1 for my RAID5 Array.

After a quick shout to the Ubuntu Community I found this wonderfully simple guide on setting up a RAID5 array using mdadm: http://bfish.xaedalus.net/?p=188

I did however run into one problem with the guide:

sudo mdadm –detail –scan >> /etc/mdadm/mdadm.conf

Which adds the following to my mdadm.conf file:

ARRAY /dev/md0 level=raid5 num-devices=3 metadata=00.90 UUID=13f95aef:9c364189:75b10d3a:87a53e2f

but after I’ve added that to the mdadm.conf and when I run the following:

sudo mdadm –examine –scan
mdadm: metadata format 00.90 unknown, ignored.
ARRAY /dev/md0 level=raid5 num-devices=3 UUID=13f95aef:9c364189:75b10d3a:87a53e2f

Notice: mdadm: metadata format 00.90 unknown, ignored.
So I had to remove “metadata=00.90” from my mdadm.conf file and I no longer get that message.
Anyone know why the metadata entry isn’t working?

And now on to setting up email notifications via Gmail – The easy way…

After spending literally hours trying to get postfix working and running into problems getting the SSL CA certificates working I gave up on postfix all together and started looking for an alternative, and after a few google searches I found one: http://www.manu-j.com/blog/wordpress-exim4-ubuntu-gmail-smtp/75/

This guide walks you thru setting up exim4 to work with Gmail, and it works perfectly, the only problem I ran into with this guide was that you need to comment out the whole “login:” section of /etc/exim4/exim4.conf.template like so:

#Commented out so that Gmail’s Public_name can equal “LOGIN”
#login:
# driver = plaintext
# public_name = LOGIN
#.ifndef AUTH_CLIENT_ALLOW_NOTLS_PASSWORDS
# Return empty string if not non-TLS AND looking up $host in passwd-file
# yields a non-empty string; fail otherwise.
# client_send = “<; ${if and{\
# {!eq{$tls_cipher}{}}\
# {!eq{PASSWDLINE}{}}\
# }\
# {}fail}\
# ; ${extract{1}{::}{PASSWDLINE}}\
# ; ${sg{PASSWDLINE}{\\N([^:]+:)(.*)\\N}{\\$2}}”
#.else
# Return empty string if looking up $host in passwd-file yields a
# non-empty string; fail otherwise.
# client_send = “<; ${if !eq{PASSWDLINE}{}\
# {}fail}\
# ; ${extract{1}{::}{PASSWDLINE}}\
# ; ${sg{PASSWDLINE}{\\N([^:]+:)(.*)\\N}{\\$2}}”
#.endif
#####################################################
### end auth/30_exim4-config_examples
#####################################################

Now simply edit your /etc/mdadm/mdadm.conf file to read:

# instruct the monitoring daemon where to send mail alerts
MAILADDR {YourEmailAddress_Where-you-want-mdadm-notifications-sent@email.com}

Now you should be all set give it a test by running:

sudo mdadm –monitor –scan –test

And you should have an email within minutes 😀