PageRenderTime 52ms CodeModel.GetById 30ms app.highlight 8ms RepoModel.GetById 12ms app.codeStats 1ms

/rc2.d/S20smartmontools

http://github.com/brinkman83/bashrc
Shell | 137 lines | 103 code | 11 blank | 23 comment | 23 complexity | 83f200865e45980f67948e24a2b35d3b MD5 | raw file
  1#!/bin/sh -e
  2# 
  3# smartmontools init.d startup script
  4#
  5# (C) 2003,04,07 Guido G√ľnther <agx@sigxcpu.org>
  6# 
  7# loosely based on the init script that comes with smartmontools which is
  8# copyrighted 2002 by Bruce Allen <smartmontools-support@lists.sourceforge.net>
  9#
 10### BEGIN INIT INFO
 11# Provides:          smartmontools
 12# Required-Start:    $syslog $remote_fs
 13# Required-Stop:     $syslog $remote_fs
 14# Default-Start:     2 3 4 5
 15# Default-Stop:      1
 16# Short-Description: SMART monitoring daemon
 17### END INIT INFO
 18
 19SMARTCTL=/usr/sbin/smartctl
 20DAEMON=/usr/sbin/smartd
 21PIDFILE=/var/run/smartd.pid
 22[ -x $SMARTCTL ] || exit 0
 23[ -x $DAEMON ] || exit 0
 24. /lib/lsb/init-functions
 25
 26RET=0
 27
 28[ -r /etc/default/rcS ] && . /etc/default/rcS
 29[ -r /etc/default/smartmontools ] && . /etc/default/smartmontools
 30
 31smartd_opts="--pidfile $PIDFILE $smartd_opts"
 32
 33enable_smart() {
 34  log_action_begin_msg "Enabling S.M.A.R.T."
 35  for device in $enable_smart; do
 36      log_action_cont_msg "$device"
 37      if ! $SMARTCTL --quietmode=errorsonly --smart=on $device; then
 38          log_action_cont_msg "(failed)"
 39          RET=2
 40      fi
 41  done
 42  log_action_end_msg 0
 43}
 44
 45check_start_smartd_option() {
 46  if [ ! "$start_smartd" = "yes" ]; then
 47    [ "$VERBOSE" = "yes" ] && log_warning_msg "Not starting S.M.A.R.T. daemon smartd, disabled via /etc/default/smartmontools"
 48    return 1
 49  else
 50    return 0
 51  fi
 52}
 53
 54running_pid()
 55{
 56    # Check if a given process pid's cmdline matches a given name
 57    pid=$1
 58    name=$2
 59    [ -z "$pid" ] && return 1 
 60    [ ! -d /proc/$pid ] &&  return 1
 61    cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
 62    # Is this the expected child?
 63    [ "$cmd" != "$name" ] &&  return 1
 64    return 0
 65}
 66
 67running()
 68{
 69# Check if the process is running looking at /proc
 70# (works for all users)
 71    # No pidfile, probably no daemon present
 72    [ ! -f "$PIDFILE" ] && return 1
 73    # Obtain the pid and check it against the binary name
 74    pid=`cat $PIDFILE`
 75    running_pid $pid $DAEMON || return 1
 76    return 0
 77}
 78
 79case "$1" in
 80  start)
 81        [ -n "$enable_smart" ] && enable_smart
 82	if check_start_smartd_option; then
 83
 84	    log_daemon_msg "Starting S.M.A.R.T. daemon" "smartd"
 85	    if running; then
 86	        log_progress_msg "already running"
 87	        log_end_msg 0
 88	        exit 0
 89	    fi
 90	    rm -f $PIDFILE
 91	    if start-stop-daemon --start --quiet --pidfile $PIDFILE \
 92	                --exec $DAEMON -- $smartd_opts; then 
 93	        log_end_msg 0
 94	    else
 95	        log_end_msg 1
 96	        RET=1
 97	    fi
 98	fi
 99	;;
100  stop)
101	log_daemon_msg "Stopping S.M.A.R.T. daemon" "smartd"
102	start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
103	log_end_msg 0
104	;;
105  reload|force-reload)
106	  log_daemon_msg "Reloading S.M.A.R.T. daemon" "smartd"
107	if start-stop-daemon --stop --quiet --signal 1 \
108	                --pidfile $PIDFILE; then
109	    log_end_msg 0
110	else
111	    log_end_msg 1
112	    RET=1
113	fi
114	  ;;
115  restart)
116	if check_start_smartd_option; then
117	    log_daemon_msg "Restarting S.M.A.R.T. daemon" "smartd"
118	    start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile $PIDFILE
119	    rm -f $PIDFILE
120	    if start-stop-daemon --start --quiet --pidfile $PIDFILE \
121	                --exec $DAEMON -- $smartd_opts; then 
122	        log_end_msg 0
123	    else
124	        log_end_msg 1
125	        RET=1
126	    fi
127	fi
128        ;;
129  status)
130	status_of_proc -p $SMARTDPID $SMARTD smartd && exit 0 || exit $?  
131	;;
132  *)
133	echo "Usage: /etc/init.d/smartmontools {start|stop|restart|reload|force-reload|status}"
134	exit 1
135esac
136
137exit $RET