Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #############################################################################################################
- #############################################################################################################
- #############################################################################################################
- ### /user/local/sbin/checkshutdown.sh:
- #!/bin/bash
- #
- #set -x
- . /etc/autoshutdown.conf
- logit()
- {
- logger -p local0.notice -s -- AutoShutdown: $*
- }
- shutdown()
- {
- ### commands before shutdown
- /home/minecraft/start-gohocraft-network.sh stop
- # Old cmd
- # /sbin/halt
- # my new shutdown cmd
- /sbin/shutdown -h 1
- # end of it :)
- }
- IsOnline()
- {
- for i in $*; do
- ping $i -c1
- if [ "$?" == "0" ]; then
- logit PC $i is still active, auto shutdown terminated
- return 1
- fi
- done
- return 0
- }
- IsRunning()
- {
- for i in $*; do
- if [ `pgrep -c $i` -gt 0 ] ; then
- logit $i still active, auto shutdown terminated
- return 1
- fi
- done
- return 0
- }
- IsDamonActive()
- {
- for i in $*; do
- if [ `pgrep -c $i` -gt 2 ] ; then
- logit $i still active, auto shutdown terminated
- return 1
- fi
- done
- return 0
- }
- IsPortInUse()
- {
- for i in $*; do
- LANG=C netstat -an | grep -q "${myIp}:${i}.*ESTABLISHED$"
- Err=${?}
- if [ ${Err} -eq 0 ] ; then
- logit "Port ${i} is still in use, auto shutdown terminated"
- return 1
- fi
- done
- return 0
- }
- IsBusy()
- {
- # Samba
- if [ "x$SAMBANETWORK" != "x" ]; then
- if [ `/usr/bin/smbstatus -b | grep $SAMBANETWORK | wc -l ` != "0" ]; then
- logit samba connected, auto shutdown terminated
- return 1
- fi
- fi
- #damons that always have one process running
- IsDamonActive $DAMONS
- if [ "$?" == "1" ]; then
- return 1
- fi
- #backuppc, wget, wsus, ....
- IsRunning $APPLICATIONS
- if [ "$?" == "1" ]; then
- return 1
- fi
- # check network-ports
- if [ "x${NETWORKPORTS}" != "x" ]; then
- myIp=$(LANG=C /sbin/ifconfig | sed -n "/inet addr/ { s|^[a-z ]*:\([0-9\.]*\).*$|\1|p }" | head -n 1)
- IsPortInUse ${NETWORKPORTS}
- if [ "$?" == "1" ]; then
- return 1
- fi
- fi
- # Read logged users
- USERCOUNT=`who | wc -l`;
- # No Shutdown if there are any users logged in
- test $USERCOUNT -gt 0 && { logit some users still connected, auto shutdown terminated; return 1; }
- IsOnline $CLIENTS
- if [ "$?" == "1" ]; then
- return 1
- fi
- return 0
- }
- COUNTFILE="/var/spool/shutdown_counter"
- OFFFILE="/var/spool/shutdown_off"
- # turns off the auto shutdown
- if [ -e $OFFFILE ]; then
- logit auto shutdown is turned off by existents of $OFFFILE
- exit 0
- fi
- if [ "$AUTO_SHUTDOWN" = "true" ] || [ "$AUTO_SHUTDOWN" = "yes" ] ; then
- IsBusy
- if [ "$?" == "0" ]; then
- # was it not busy already last time? Then shutdown.
- if [ -e $COUNTFILE ]; then
- # shutdown
- rm -f $COUNTFILE
- logit auto shutdown caused by cron
- shutdown
- exit 0
- else
- # shut down next time
- touch $COUNTFILE
- logit marked for shutdown in next try
- exit 0
- fi
- else
- rm -f $COUNTFILE
- #logit aborted
- exit 0
- fi
- fi
- logit malfunction
- exit 1
- #############################################################################################################
- #############################################################################################################
- #############################################################################################################
- ### /etc/autoshutdown.conf:
- # Turn on auto shutdown
- AUTO_SHUTDOWN=yes
- # Damons that always have one process running, only if more that one process is active we prevent the shutdown
- # The values are used with grep, so just a unique portion is sufficient
- DAMONS="rsync BackupPC_ mdadm smbd"
- # Important applications that shall prevent the shutdown
- # The values are used with grep, so just a unique portion is sufficient
- APPLICATIONS="BackupPC_nightly BackupPC_dump wsus wget mlnet 7z apt-get"
- # Network IP range for checking any open samba connections
- # The value is used with grep, so just a unique portion is sufficient
- SAMBANETWORK="192.168.1."
- # Names or IP for computers that shall prevent the shutdown
- # We ping these computers in the list to check whether they are active.
- CLIENTS="192.168.0.210 192.168.0.218"
- # List of ports
- # we check all of them, if there in use
- NETWORKPORTS="22 10000 25577 32400"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement