#!/bin/bash ################################################################################# # /etc/cron.15min/autoshutdn RM20070202 # # I run this script every 15 minutes on 192.168.1.4 (box) which is a server # for printer/scanner shares, backups, and recording/viewing of dvb-t timeshifts. # # It checks to see if our work stations (static IP's) are up on and then # if there are any dvb recording (at) jobs pending and if there is any video to dvd # processing using our dcript "create-dvd". # # If rick and leila (the workstations) are shutdown, there are no jobs in /var/spool/atjobs # on box and the script create-dvd and the ap dvbstream are not running, then the script # runs the command [shutdown -h now] Otherwise the script will run repeatedly as as a # cron job as long as the four conditions are not met. # # With Slackware, in order to run every X minutes/hours you'll need to copy this script # to whatever cron directory you choose, e.g. /etc/cron.15min, and then add the following # lines to /var/spool/cron/crontabs/root: # # # Run 15 minute cron jobs 24 X 7: # 0,15,30,45 * * * * /etc/cron.15min/autoshtdn # # You could probably put the executable script anywhere as long as the path in the # second line above points to the correct location. # # It is also a good idea to have logrotate look after the log file /var/log/autoshtdn # which is created by this script. I add it to the list of log files found before the # the first curly brace in /etc/logrotate.d/syslog. ################################################################################# # Create some variables IP1=192.168.1.2 # rick IP2=192.168.1.3 # leila QUDIR=/var/spool/atjobs # on box 192.168.1.4 # Test if Rick is up. # Test1=1 if rick is down. ping -c1 $IP1 > /dev/null 2>&1 if [ $? -eq 0 ];then TEST1=2 else TEST1=1 fi # Test if Leila is up. # Test2=1 if leila is down. ping -c1 $IP2 > /dev/null 2>&1 if [ $? -eq 0 ] ; then TEST2=2 else TEST2=1 fi # Test to see if there are any timeshifting jobs in /var/spool/atjobs. # TEST3 will equal "1" if there are no jobs and "2" if there are. if [ -e $QUDIR/* ] ; then TEST3=2 else TEST3=1 fi # Test to see if [create-dvd] is running # TEST4 will equal "1" if the script, create-dvd, is not running ps -A | grep create-dvd > /dev/null 2>&1 if [ $? -eq 0 ] ; then TEST4=2 else TEST4=1 fi # Test to see if [dvbstream] is running # TEST4 will equal "1" if the script, create-dvd, is not running ps -A | grep dvbstream > /dev/null 2>&1 if [ $? -eq 0 ] ; then TEST5=2 else TEST5=1 fi # Now if we have "1", "1", "1", we will shut down the server if [ $TEST1 -eq 1 -a $TEST2 -eq 1 -a $TEST3 -eq 1 -a $TEST4 -eq 1 -a $TEST5 -eq 1 ] ; then echo -e "\n`date` ran autoshutdn script `date`" >> /var/log/autoshtdn echo -e "rick=$TEST1, leila=$TEST2, atjobs=$TEST3, create-dvd=$TEST4, dvbstream=$TEST5, shutting down now.\n" >> /var/log/autoshtdn shutdown -h now else echo -e "\n`date` ran autoshutdn script `date`" >> /var/log/autoshtdn echo -e "rick=$TEST1, leila=$TEST2, atjobs=$TEST3, create-dvd=$TEST4, dvbstream=$TEST5, no shutdown now.\n" >> /var/log/autoshtdn # and the script will be run again in 15 minutes fi # End of script