#! /bin/sh # smartmontools init file for smartd # Copyright (C) 2002-7 Bruce Allen # $Id: smartd.initd.in,v 1.36 2007/11/01 20:53:30 chrfranke Exp $ # For RedHat and cousins: # chkconfig: 2345 40 40 # description: Self Monitoring and Reporting Technology (SMART) Daemon # processname: smartd # For SuSE and cousins ### BEGIN INIT INFO # Provides: smartd # Required-Start: $syslog # X-UnitedLinux-Should-Start: $sendmail # Required-Stop: $syslog # X-UnitedLinux-Should-Stop: # Default-Start: 2 3 5 # Default-Stop: # Short-Description: Monitors disk and tape health via S.M.A.R.T. # Description: Start S.M.A.R.T. disk and tape monitor. ### END INIT INFO # This program is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the Free # Software Foundation; either version 2, or (at your option) any later # version. # You should have received a copy of the GNU General Public License (for # example COPYING); if not, write to the Free Software Foundation, Inc., 675 # Mass Ave, Cambridge, MA 02139, USA. # This code was originally developed as a Senior Thesis by Michael Cornwell # at the Concurrent Systems Laboratory (now part of the Storage Systems # Research Center), Jack Baskin School of Engineering, University of # California, Santa Cruz. http://ssrc.soe.ucsc.edu/. # Uncomment the line below to pass options to smartd on startup. # Note that distribution specific configuration files like # /etc/{default,sysconfig}/smartmontools might override these #smartd_opts="--interval=1800" SMARTD_BIN=/usr/local/sbin/smartd report_unsupported () { echo "Currently the smartmontools package has no init script for" echo "the $1 OS/distribution. If you can provide one or this" echo "one works after removing some ifdefs, please contact" echo "smartmontools-support@lists.sourceforge.net." exit 1 } # Red Hat or Yellow Dog or Mandrake if [ -f /etc/redhat-release -o -f /etc/yellowdog-release -o -f /etc/mandrake-release -o -f /etc/whitebox-release -o -f /etc/trustix-release -o -f /etc/tinysofa-release ] ; then # Source function library . /etc/rc.d/init.d/functions # Source configuration file. This should define the shell variable smartd_opts [ -r /etc/sysconfig/smartmontools ] && . /etc/sysconfig/smartmontools RETVAL=0 prog=smartd case "$1" in start) echo -n $"Starting $prog: " daemon $SMARTD_BIN $smartd_opts touch /var/lock/subsys/smartd echo ;; stop) echo -n $"Shutting down $prog: " killproc $SMARTD_BIN rm -f /var/lock/subsys/smartd echo ;; reload) echo -n $"Reloading $prog daemon configuration: " killproc $SMARTD_BIN -HUP RETVAL=$? echo ;; report) echo -n $"Checking SMART devices now: " killproc $SMARTD_BIN -USR1 RETVAL=$? echo ;; restart) $0 stop $0 start ;; status) status $prog ;; *) echo $"Usage: $0 {start|stop|reload|report|restart|status}" RETVAL=1 esac exit $RETVAL # Slackware elif [ -f /etc/slackware-version ] ; then # Source configuration file. This should define the shell variable smartd_opts. # Email smartmontools-support@lists.sourceforge.net if there is a better choice # of path for Slackware. [ -r /etc/sysconfig/smartmontools ] && . /etc/sysconfig/smartmontools case "$1" in start) echo -n "Starting smartd: " $SMARTD_BIN $smartd_opts echo ;; stop) echo -n "Shutting down smartd: " killall $SMARTD_BIN echo ;; restart) $0 stop sleep 1 $0 start ;; *) echo "Usage: smartd {start|stop|restart}" exit 1 esac exit 0 # SuSE elif [ -f /etc/SuSE-release ] ; then test -x $SMARTD_BIN || exit 5 # Existence of config file is optional SMARTD_CONFIG=/etc/smartd.conf # source configuration file. This should set the shell variable smartd_opts [ -r /etc/default/smartmontools ] && . /etc/default/smartmontools # Shell functions sourced from /etc/rc.status: # rc_check check and set local and overall rc status # rc_status check and set local and overall rc status # rc_status -v ditto but be verbose in local rc status # rc_status -v -r ditto and clear the local rc status # rc_failed set local and overall rc status to failed # rc_reset clear local rc status (overall remains) # rc_exit exit appropriate to overall rc status . /etc/rc.status # First reset status of this service rc_reset # Return values acc. to LSB for all commands but status: # 0 - success # 1 - misc error # 2 - invalid or excess args # 3 - unimplemented feature (e.g. reload) # 4 - insufficient privilege # 5 - program not installed # 6 - program not configured # # Note that starting an already running service, stopping # or restarting a not-running service as well as the restart # with force-reload (in case signalling is not supported) are # considered a success. case "$1" in start) echo -n "Starting smartd " ## Start daemon with startproc(8). If this fails ## the echo return value is set appropriate. # startproc should return 0, even if service is # already running to match LSB spec. startproc $SMARTD_BIN $smartd_opts # Remember status and be verbose rc_status -v ;; stop) echo -n "Shutting down smartd " killproc -TERM $SMARTD_BIN # Remember status and be verbose rc_status -v ;; try-restart|condrestart) ## Do a restart only if the service was active before. ## Note: try-restart is now part of LSB (as of 1.9). ## RH has a similar command named condrestart. if test "$1" = "condrestart"; then echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}" fi $0 status if test $? = 0; then $0 restart else rc_reset # Not running is not a failure. fi # Remember status and be quiet rc_status ;; restart | force-reload) $0 stop $0 start ;; reload) ## Like force-reload, but if daemon does not support ## signaling, do nothing (!) rc_failed 3 rc_status -v ;; status) echo -n "Checking for service smartd " ## Check status with checkproc(8), if process is running ## checkproc will return with exit status 0. # Status has a slightly different for the status command: # 0 - service running # 1 - service dead, but /var/run/ pid file exists # 2 - service dead, but /var/lock/ lock file exists # 3 - service not running # NOTE: checkproc returns LSB compliant status values. checkproc $SMARTD_BIN rc_status -v ;; probe) ## Optional: Probe for the necessity of a reload, print out the ## argument to this init script which is required for a reload. ## Note: probe is not (yet) part of LSB (as of 1.2) test $SMARTD_CONFIG -nt /var/run/smartd.pid && echo reload ;; *) echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}" exit 1 ;; esac rc_exit # Debian case elif [ -f /etc/debian_version ] ; then PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin SMARTDPID=/var/run/smartd.pid [ -x $SMARTD_BIN ] || exit 0 RET=0 # source configuration file [ -r /etc/default/smartmontools ] && . /etc/default/smartmontools smartd_opts="--pidfile $SMARTDPID $smartd_opts" case "$1" in start) echo -n "Starting S.M.A.R.T. daemon: smartd" if start-stop-daemon --start --quiet --pidfile $SMARTDPID \ --exec $SMARTD_BIN -- $smartd_opts; then echo "." else echo " (failed)" RET=1 fi ;; stop) echo -n "Stopping S.M.A.R.T. daemon: smartd" start-stop-daemon --stop --quiet --oknodo --pidfile $SMARTDPID echo "." ;; restart|force-reload) $0 stop $0 start ;; *) echo "Usage: /etc/init.d/smartmontools {start|stop|restart|force-reload}" exit 1 esac exit $RET elif [ -f /etc/gentoo-release ] ; then report_unsupported "Gentoo" elif [ -f /etc/turbolinux-release ] ; then report_unsupported "Turbolinux" elif [ -f /etc/environment.corel ] ; then report_unsupported "Corel" # PLEASE ADD OTHER LINUX DISTRIBUTIONS JUST BEFORE THIS LINE, USING elif elif uname -a | grep FreeBSD > /dev/null 2>&1 ; then # following is replaced by port install PREFIX=@@PREFIX@@ # Updated to try both the RCNG version of things from 5.x, or fallback to # oldfashioned rc.conf if [ -r /etc/rc.subr ]; then # This is RC-NG, pick up our values . /etc/rc.subr name="smartd" rcvar="smartd_enable" command="$SMARTD_BIN" load_rc_config $name elif [ -r /etc/defaults/rc.conf ]; then # Not a 5.x system, try the default location for variables . /etc/defaults/rc.conf source_rc_confs elif [ -r /etc/rc.conf ]; then # Worst case, fallback to system config file . /etc/rc.conf fi if [ -r /etc/rc.subr ]; then # Use new functionality from RC-NG run_rc_command "$1" else PID_FILE=/var/run/smartd.pid case "$1" in start) $SMARTD_BIN -p $PID_FILE $smartd_flags echo -n " smartd" ;; stop) kill `cat $PID_FILE` echo -n " smartd" ;; restart) $0 stop sleep 1 $0 start ;; *) echo "Usage: smartd {start|stop|restart}" exit 1 esac exit 0 fi elif uname -a | grep SunOS > /dev/null 2>&1 ; then # Source configuration file. This should define the shell variable smartd_opts. # Email smartmontools-support@lists.sourceforge.net if there is a better choice # of path for Solaris [ -r /etc/default/smartmontools ] && . /etc/default/smartmontools PID_FILE=/var/run/smartd.pid case "$1" in start) $SMARTD_BIN -p $PID_FILE $smartd_opts echo -n "smartd " ;; stop) [ -f $PID_FILE ] && kill `cat $PID_FILE` echo -n "smartd " ;; restart) $0 stop sleep 1 $0 start ;; *) echo "Usage: smartd {start|stop|restart}" exit 1 esac exit 0 # Cygwin elif uname | grep -i CYGWIN > /dev/null 2>&1 ; then # The following settings may be changed by the configuration file below # Service Name (must be unique) smartd_svcname=smartd # Service display name smartd_svcdisp="CYGWIN smartd" # Service description smartd_svcdesc="\ Controls and monitors storage devices using the Self-Monitoring \ Analysis and Reporting Technology System (S.M.A.R.T.) \ built into ATA and SCSI Hard Drives. \ http://smartmontools.sourceforge.net/" # Source configuration file. This should define the shell variable smartd_opts. # Email smartmontools-support@lists.sourceforge.net if there is a better choice # of path for Cygwin [ -r /etc/sysconfig/smartmontools ] && . /etc/sysconfig/smartmontools PID_FILE=/var/run/smartd.pid RETVAL=0 # Note: "[ -r $PID_FILE ]" is not used here. On Cygwin, this command may # return success even if the file is present but cannot be read by current user. # If smartd is running as service, smartd.pid is owned by local system account # which is different from any user ever executing this script. case "$1" in start) if cygrunsrv -L 2>/dev/null | grep "^${smartd_svcname}$" >/dev/null 2>&1; then echo -n "Starting service $smartd_svcname: " cygrunsrv -S "$smartd_svcname" else echo -n "Starting smartd as daemon: " $SMARTD_BIN -p $PID_FILE $smartd_opts fi RETVAL=$? ;; stop) echo -n "Shutting down smartd: " pid="`cat $PID_FILE 2>/dev/null`" && kill "$pid" RETVAL=$? ;; reload) echo -n "Reloading smartd configuration: " pid="`cat $PID_FILE 2>/dev/null`" && kill -HUP "$pid" RETVAL=$? ;; report) echo -n "Checking SMART devices now: " pid="`cat $PID_FILE 2>/dev/null`" && kill -USR1 "$pid" RETVAL=$? ;; restart) $0 stop sleep 1 $0 start exit $? ;; install) shift [ $# -eq 0 ] || smartd_opts="$*" dep= if cygrunsrv -L 2>/dev/null | grep "^syslogd$" >/dev/null 2>&1; then dep="-y syslogd" else echo "Warning: syslogd service not installed, smartd will write to windows event log."; fi echo "Installing service ${smartd_svcname}${smartd_opts+ with options '$smartd_opts'}:" cygrunsrv -I "$smartd_svcname" -d "$smartd_svcdisp" -f "$smartd_svcdesc" $dep \ -e CYGWIN="$CYGWIN" -p $SMARTD_BIN -a "-n -p ${PID_FILE}${smartd_opts+ }$smartd_opts" RETVAL=$? ;; remove) echo "Removing service $smartd_svcname:" cygrunsrv -R "$smartd_svcname" RETVAL=$? ;; status) echo -n "Checking smartd status: " if cygrunsrv -L 2>/dev/null | grep "^${smartd_svcname}$" >/dev/null 2>&1; then if cygrunsrv -Q "$smartd_svcname" 2>/dev/null | grep "State *: Running" >/dev/null 2>&1; then echo "running as service '$smartd_svcname'." elif ps -e 2>/dev/null | grep " ${SMARTD_BIN}$" >/dev/null 2>&1; then echo "installed as service '$smartd_svcname' but running as daemon." else echo "installed as service '$smartd_svcname' but not running." RETVAL=1 fi elif ps -e 2>/dev/null | grep " ${SMARTD_BIN}$" >/dev/null 2>&1; then echo "running as daemon." else echo "not running." RETVAL=1 fi exit $RETVAL ;; *) echo "Usage: $0 {start|stop|restart|reload|report|status}" echo " $0 {install [options]|remove}" exit 1 esac if [ "$RETVAL" -eq 0 ]; then echo "done"; else echo "ERROR"; fi exit $RETVAL # Add other OSes HERE, using elif... else report_unsupported "Unknown" fi # One should NEVER arrive here, except for a badly written case above, # that fails to exit. echo "SOMETHING IS WRONG WITH THE SMARTD STARTUP SCRIPT" echo "PLEASE CONTACT smartmontools-support@lists.sourceforge.net" exit 1