/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. SMARTCTL=/usr/sbin/smartctl
  19. DAEMON=/usr/sbin/smartd
  20. PIDFILE=/var/run/smartd.pid
  21. [ -x $SMARTCTL ] || exit 0
  22. [ -x $DAEMON ] || exit 0
  23. . /lib/lsb/init-functions
  24. RET=0
  25. [ -r /etc/default/rcS ] && . /etc/default/rcS
  26. [ -r /etc/default/smartmontools ] && . /etc/default/smartmontools
  27. smartd_opts="--pidfile $PIDFILE $smartd_opts"
  28. enable_smart() {
  29. log_action_begin_msg "Enabling S.M.A.R.T."
  30. for device in $enable_smart; do
  31. log_action_cont_msg "$device"
  32. if ! $SMARTCTL --quietmode=errorsonly --smart=on $device; then
  33. log_action_cont_msg "(failed)"
  34. RET=2
  35. fi
  36. done
  37. log_action_end_msg 0
  38. }
  39. check_start_smartd_option() {
  40. if [ ! "$start_smartd" = "yes" ]; then
  41. [ "$VERBOSE" = "yes" ] && log_warning_msg "Not starting S.M.A.R.T. daemon smartd, disabled via /etc/default/smartmontools"
  42. return 1
  43. else
  44. return 0
  45. fi
  46. }
  47. running_pid()
  48. {
  49. # Check if a given process pid's cmdline matches a given name
  50. pid=$1
  51. name=$2
  52. [ -z "$pid" ] && return 1
  53. [ ! -d /proc/$pid ] && return 1
  54. cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
  55. # Is this the expected child?
  56. [ "$cmd" != "$name" ] && return 1
  57. return 0
  58. }
  59. running()
  60. {
  61. # Check if the process is running looking at /proc
  62. # (works for all users)
  63. # No pidfile, probably no daemon present
  64. [ ! -f "$PIDFILE" ] && return 1
  65. # Obtain the pid and check it against the binary name
  66. pid=`cat $PIDFILE`
  67. running_pid $pid $DAEMON || return 1
  68. return 0
  69. }
  70. case "$1" in
  71. start)
  72. [ -n "$enable_smart" ] && enable_smart
  73. if check_start_smartd_option; then
  74. log_daemon_msg "Starting S.M.A.R.T. daemon" "smartd"
  75. if running; then
  76. log_progress_msg "already running"
  77. log_end_msg 0
  78. exit 0
  79. fi
  80. rm -f $PIDFILE
  81. if start-stop-daemon --start --quiet --pidfile $PIDFILE \
  82. --exec $DAEMON -- $smartd_opts; then
  83. log_end_msg 0
  84. else
  85. log_end_msg 1
  86. RET=1
  87. fi
  88. fi
  89. ;;
  90. stop)
  91. log_daemon_msg "Stopping S.M.A.R.T. daemon" "smartd"
  92. start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
  93. log_end_msg 0
  94. ;;
  95. reload|force-reload)
  96. log_daemon_msg "Reloading S.M.A.R.T. daemon" "smartd"
  97. if start-stop-daemon --stop --quiet --signal 1 \
  98. --pidfile $PIDFILE; then
  99. log_end_msg 0
  100. else
  101. log_end_msg 1
  102. RET=1
  103. fi
  104. ;;
  105. restart)
  106. if check_start_smartd_option; then
  107. log_daemon_msg "Restarting S.M.A.R.T. daemon" "smartd"
  108. start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile $PIDFILE
  109. rm -f $PIDFILE
  110. if start-stop-daemon --start --quiet --pidfile $PIDFILE \
  111. --exec $DAEMON -- $smartd_opts; then
  112. log_end_msg 0
  113. else
  114. log_end_msg 1
  115. RET=1
  116. fi
  117. fi
  118. ;;
  119. status)
  120. status_of_proc -p $SMARTDPID $SMARTD smartd && exit 0 || exit $?
  121. ;;
  122. *)
  123. echo "Usage: /etc/init.d/smartmontools {start|stop|restart|reload|force-reload|status}"
  124. exit 1
  125. esac
  126. exit $RET