Advertisement
GohoCraft

Bash - Autoshutdown Script

Dec 14th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.83 KB | None | 0 0
  1. #############################################################################################################
  2. #############################################################################################################
  3. #############################################################################################################
  4. ### /user/local/sbin/checkshutdown.sh:
  5.  
  6.  
  7. #!/bin/bash
  8. #
  9. #set -x
  10.  
  11. . /etc/autoshutdown.conf
  12.  
  13. logit()
  14. {
  15.     logger -p local0.notice -s -- AutoShutdown: $*
  16. }
  17.  
  18. shutdown()
  19. {
  20.         ### commands before shutdown
  21.  
  22.         /home/minecraft/start-gohocraft-network.sh stop
  23.         # Old cmd
  24.         # /sbin/halt
  25.         # my new shutdown cmd
  26.         /sbin/shutdown -h 1
  27.         # end of it :)
  28.  
  29. }
  30.  
  31. IsOnline()
  32. {
  33.         for i in $*; do
  34.         ping $i -c1
  35.         if [ "$?" == "0" ]; then
  36.           logit PC $i is still active, auto shutdown terminated
  37.           return 1
  38.         fi
  39.         done
  40.  
  41.     return 0
  42. }
  43.  
  44. IsRunning()
  45. {
  46.         for i in $*; do
  47.         if [ `pgrep -c $i` -gt 0 ] ; then
  48.           logit $i still active, auto shutdown terminated
  49.                   return 1
  50.                 fi
  51.         done
  52.  
  53.         return 0
  54. }
  55.  
  56. IsDamonActive()
  57. {
  58.         for i in $*; do
  59.                 if [ `pgrep -c $i` -gt 2 ] ; then
  60.                   logit $i still active, auto shutdown terminated
  61.                   return 1
  62.                 fi
  63.         done
  64.  
  65.         return 0
  66. }
  67.  
  68. IsPortInUse()
  69. {
  70.         for i in $*; do
  71.                 LANG=C netstat -an | grep -q "${myIp}:${i}.*ESTABLISHED$"
  72.                 Err=${?}
  73.                 if [ ${Err} -eq 0 ] ; then
  74.                   logit "Port ${i} is still in use, auto shutdown terminated"
  75.                   return 1
  76.                 fi
  77.         done
  78.  
  79.         return 0
  80. }
  81.  
  82. IsBusy()
  83. {
  84.     # Samba
  85.     if [ "x$SAMBANETWORK" != "x" ]; then
  86.         if [ `/usr/bin/smbstatus -b | grep $SAMBANETWORK | wc -l ` != "0" ]; then
  87.           logit samba connected, auto shutdown terminated
  88.           return 1
  89.         fi
  90.     fi
  91.  
  92.     #damons that always have one process running
  93.     IsDamonActive $DAMONS
  94.         if [ "$?" == "1" ]; then
  95.                 return 1
  96.         fi
  97.  
  98.     #backuppc, wget, wsus, ....
  99.         IsRunning $APPLICATIONS
  100.     if [ "$?" == "1" ]; then
  101.                 return 1
  102.         fi
  103.  
  104.         # check network-ports
  105.         if [ "x${NETWORKPORTS}" != "x" ]; then
  106.                 myIp=$(LANG=C /sbin/ifconfig | sed -n "/inet addr/ { s|^[a-z ]*:\([0-9\.]*\).*$|\1|p }" | head -n 1)
  107.                 IsPortInUse ${NETWORKPORTS}
  108.                 if [ "$?" == "1" ]; then
  109.                         return 1
  110.                 fi
  111.         fi
  112.  
  113.     # Read logged users
  114.     USERCOUNT=`who | wc -l`;
  115.     # No Shutdown if there are any users logged in
  116.     test $USERCOUNT -gt 0 && { logit some users still connected, auto shutdown terminated; return 1; }
  117.  
  118.         IsOnline $CLIENTS
  119.         if [ "$?" == "1" ]; then
  120.                 return 1
  121.         fi
  122.  
  123.     return 0
  124. }
  125.  
  126. COUNTFILE="/var/spool/shutdown_counter"
  127. OFFFILE="/var/spool/shutdown_off"
  128.  
  129. # turns off the auto shutdown
  130. if [ -e $OFFFILE ]; then
  131.     logit auto shutdown is turned off by existents of $OFFFILE
  132.     exit 0
  133. fi
  134.  
  135. if [ "$AUTO_SHUTDOWN" = "true" ] || [ "$AUTO_SHUTDOWN" = "yes" ] ; then
  136.     IsBusy
  137.     if [ "$?" == "0" ]; then
  138.         # was it not busy already last time? Then shutdown.
  139.         if [ -e $COUNTFILE ]; then
  140.                 # shutdown
  141.                 rm -f $COUNTFILE
  142.                 logit auto shutdown caused by cron
  143.                 shutdown
  144.                 exit 0
  145.         else
  146.             # shut down next time
  147.             touch $COUNTFILE
  148.             logit marked for shutdown in next try
  149.             exit 0
  150.         fi
  151.     else
  152.         rm -f $COUNTFILE
  153.         #logit aborted
  154.         exit 0
  155.     fi
  156. fi
  157.  
  158. logit malfunction
  159. exit 1
  160.  
  161. #############################################################################################################
  162. #############################################################################################################
  163. #############################################################################################################
  164. ### /etc/autoshutdown.conf:
  165.  
  166.  
  167. # Turn on auto shutdown
  168. AUTO_SHUTDOWN=yes
  169.  
  170. # Damons that always have one process running, only if more that one process is active we prevent the shutdown
  171. # The values are used with grep, so just a unique portion is sufficient
  172. DAMONS="rsync BackupPC_ mdadm smbd"
  173.  
  174. # Important applications that shall prevent the shutdown
  175. # The values are used with grep, so just a unique portion is sufficient
  176. APPLICATIONS="BackupPC_nightly BackupPC_dump wsus wget mlnet 7z apt-get"
  177.  
  178. # Network IP range for checking any open samba connections
  179. # The value is used with grep, so just a unique portion is sufficient
  180. SAMBANETWORK="192.168.1."
  181.  
  182. # Names or IP for computers that shall prevent the shutdown
  183. # We ping these computers in the list to check whether they are active.
  184. CLIENTS="192.168.0.210 192.168.0.218"
  185.  
  186. # List of ports
  187. # we check all of them, if there in use
  188. NETWORKPORTS="22 10000 25577 32400"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement