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 😀


Ubuntu has problems with power saving (muti-display)

September 29, 2008

Using Ubuntu 8.04.1 (up to date)
I have a dual monitor setup, and I recently switched from “Separate X-Sessions” to a “TwinView” and now my monitors won’t go into standby/sleep (power saving) state. I’ve tried changing the settings in System>Power Management>Display> “Put display to sleep when inactive for” but no matter what I set it to (for a time limit) it NEVER goes into sleep mode. I’ve left the computer on for hours on end, and they never go into sleep. But when I switch from “TwinView” to “Separate X-Screens” the monitors go to sleep right on time, EVERYTIME.

p.s. I am NOT using Compiz (don’t know if that makes a difference or not).
System>Preferences>Appearance>Visual Effects = NONE.

Hopefully this will be fixed soon…

Checkout the bug reports
Ubuntu’s Launchpad: https://bugs.launchpad.net/ubuntu/+bug/275308
Gnome’s BugZilla: http://bugzilla.gnome.org/show_bug.cgi?id=554247


Installing Ubuntu on a OLPC Xo – Part 1

July 10, 2008

Well thanks to the Ubuntu On OLPC XO wiki page I was able to successfully install Ubuntu on my OLPC.
I did however run into a few hitches.

1. Wireless Internet Connection

The wiki page doesn’t make reference to how to setup WEP wireless connection, it only mentions WPA (NETWORKING) so I had to do a bit of research: How to connect to wireless (WEP) in ubuntu?
turns out that the following does the trick:

iwconfig eth1 essid "mySSID" key mywepkey
sudo dhclient eth1

Now I had to figure out how to add this to a startup script so that it would automatically connect to my wireless router upon boot up: Automatic Wireless Connection (At Startup)?
All I had to do was add the following to /etc/network/interfaces

# Wireless Connection
iface eth1 inet dhcp
wireless-key hex-key
wireless-essid ssid

auto eth1

where hex-key = my WEP hex key & ssid = my routers wireless SSID

2. Mouse Problems

The Customizing Ubuntu for XO wiki page explains how to fix the tap-click issue and sensitivity, however the sensitivity issue I am having is that my mouse is too insensitive, My mouse pointer FLY’s across the screen when I move my finger, so instead of using the wiki’s recommened:
xset m 1/4 0
I am using:
xset m 25/20 0
and it is much better now.

3. Screen Saver

The first thing I noticed was there was no screensaver installed, when I went to Settings > Screensaver Settings, I got the following error: screensaver-settings-error
I was able to install xscreensaver, but I got another error every time I made a change to xscreensaver’s settings “Directory does not exist: /usr/share/backgrounds” a quick google search and I found a solution: https://bugs.launchpad.net/ubuntu/+source/xscreensaver/+bug/129769
sudo apt-get install screensaver-default-images
Fixed 🙂

Part 2 of my setup woe’s comming soon…


Linux + Xorg + VMware = trouble

July 4, 2008

Apparently there is a severe bug that relates to multiple distributions of Linux (I am using Ubuntu) and it has to do with VMware and Xorg. This bug causes the keyboard to go all out of whack when using VMware in full screen mode. As of right now there isn’t a solution to the problem but there are a few workarounds:

  1. execute ‘setxkbmap’ from the terminal
  2. In Ubuntu, Go to Applications > Other > Keyboard Layout, and remove the keyboard layout and then add it back and re-apply

Hopefully someone will correct this issue soon. But for now it seems like everyone is passing the buck (Xorg is blaming VMware, VMware is blaming Linux & Xorg, etc…)

If anyone has some incite or other work-arounds please post.

Links:
LaunchPad: https://bugs.launchpad.net/ubuntu/+source/linux/+bug/195982
VMware Forums: http://communities.vmware.com/message/975867
Ubuntu Forums Posts: