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…

Advertisements

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)


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


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: