]> git.proxmox.com Git - mirror_smartmontools-debian.git/blobdiff - debian/smartmontools.init
Remove '/var/lib/smartmontools' on purge (Closes: #766145)
[mirror_smartmontools-debian.git] / debian / smartmontools.init
index 69063ab1cab5753f4eee920e45c20c10e6b043c8..a64b0e6e06a50840295992383341d013831e7876 100644 (file)
 # 
 # smartmontools init.d startup script
 #
-# (C) 2003,04 Guido Günther <agx@sigxcpu.org>
+# (C) 2003,04,07 Guido Günther <agx@sigxcpu.org>
 # 
 # loosely based on the init script that comes with smartmontools which is
 # copyrighted 2002 by Bruce Allen <smartmontools-support@lists.sourceforge.net>
+#
+### BEGIN INIT INFO
+# Provides:          smartmontools
+# Required-Start:    $syslog $remote_fs
+# Required-Stop:     $syslog $remote_fs
+# Default-Start:     2 3 4 5
+# Default-Stop:      1
+# Short-Description: SMART monitoring daemon
+### END INIT INFO
 
 SMARTCTL=/usr/sbin/smartctl
-SMARTD=/usr/sbin/smartd
-SMARTDPID=/var/run/smartd.pid
+DAEMON=/usr/sbin/smartd
+PIDFILE=/var/run/smartd.pid
 [ -x $SMARTCTL ] || exit 0
-[ -x $SMARTD ] || exit 0
+[ -x $DAEMON ] || exit 0
 . /lib/lsb/init-functions
 
 RET=0
 
+[ -r /etc/default/rcS ] && . /etc/default/rcS
 [ -r /etc/default/smartmontools ] && . /etc/default/smartmontools
 
-smartd_opts="--pidfile $SMARTDPID $smartd_opts"
+smartd_opts="--pidfile $PIDFILE $smartd_opts"
 
 enable_smart() {
   log_action_begin_msg "Enabling S.M.A.R.T."
   for device in $enable_smart; do
-       log_action_cont_msg "$device"
-       if ! $SMARTCTL --quietmode=errorsonly --smart=on $device; then
-               log_action_cont_msg "(failed)"
-               RET=2
-       fi
+      log_action_cont_msg "$device"
+      if ! $SMARTCTL --quietmode=errorsonly --smart=on $device; then
+          log_action_cont_msg "(failed)"
+          RET=2
+      fi
   done
   log_action_end_msg 0
 }
 
 check_start_smartd_option() {
   if [ ! "$start_smartd" = "yes" ]; then
-    log_warning_msg "Not starting S.M.A.R.T. daemon smartd, disabled via /etc/default/smartmontools"
+    [ "$VERBOSE" = "yes" ] && log_warning_msg "Not starting S.M.A.R.T. daemon smartd, disabled via /etc/default/smartmontools"
     return 1
   else
     return 0
   fi
 }
 
+running_pid()
+{
+    # Check if a given process pid's cmdline matches a given name
+    pid=$1
+    name=$2
+    [ -z "$pid" ] && return 1 
+    [ ! -d /proc/$pid ] &&  return 1
+    cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
+    # Is this the expected child?
+    [ "$cmd" != "$name" ] &&  return 1
+    return 0
+}
+
+running()
+{
+# Check if the process is running looking at /proc
+# (works for all users)
+    # No pidfile, probably no daemon present
+    [ ! -f "$PIDFILE" ] && return 1
+    # Obtain the pid and check it against the binary name
+    pid=`cat $PIDFILE`
+    running_pid $pid $DAEMON || return 1
+    return 0
+}
+
 case "$1" in
   start)
         [ -n "$enable_smart" ] && enable_smart
        if check_start_smartd_option; then
+
            log_daemon_msg "Starting S.M.A.R.T. daemon" "smartd"
-            if start-stop-daemon --start --quiet --pidfile $SMARTDPID \
-                       --exec $SMARTD -- $smartd_opts; then 
-               log_end_msg 0
+           if running; then
+               log_progress_msg "already running"
+               log_end_msg 0
+               exit 0
+           fi
+           rm -f $PIDFILE
+           if start-stop-daemon --start --quiet --pidfile $PIDFILE \
+                       --exec $DAEMON -- $smartd_opts; then 
+               log_end_msg 0
            else
-               log_end_msg 1
-               RET=1
+               log_end_msg 1
+               RET=1
            fi
        fi
        ;;
   stop)
        log_daemon_msg "Stopping S.M.A.R.T. daemon" "smartd"
-       start-stop-daemon --stop --quiet --oknodo --pidfile $SMARTDPID
+       start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
        log_end_msg 0
        ;;
   reload|force-reload)
-       log_daemon_msg "Reloading S.M.A.R.T. daemon" "smartd"
+         log_daemon_msg "Reloading S.M.A.R.T. daemon" "smartd"
        if start-stop-daemon --stop --quiet --signal 1 \
-                       --pidfile $SMARTDPID; then
-            log_end_msg 0
+                       --pidfile $PIDFILE; then
+           log_end_msg 0
        else
-            log_end_msg 1
-            RET=1
+           log_end_msg 1
+           RET=1
        fi
-       ;;
+         ;;
   restart)
        if check_start_smartd_option; then
            log_daemon_msg "Restarting S.M.A.R.T. daemon" "smartd"
-           start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile $SMARTDPID
-            if start-stop-daemon --start --quiet --pidfile $SMARTDPID \
-                       --exec $SMARTD -- $smartd_opts; then 
-               log_end_msg 0
+           start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile $PIDFILE
+           rm -f $PIDFILE
+           if start-stop-daemon --start --quiet --pidfile $PIDFILE \
+                       --exec $DAEMON -- $smartd_opts; then 
+               log_end_msg 0
            else
-               log_end_msg 1
-               RET=1
+               log_end_msg 1
+               RET=1
            fi
        fi
         ;;
+  status)
+       status_of_proc $DAEMON smartd && exit 0 || exit $?
+       ;;
   *)
-       echo "Usage: /etc/init.d/smartmontools {start|stop|restart|reload|force-reload}"
+       echo "Usage: /etc/init.d/smartmontools {start|stop|restart|reload|force-reload|status}"
        exit 1
 esac