File: nut-server.init

package info (click to toggle)
nut 2.7.4-8
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,964 kB
  • sloc: ansic: 70,278; sh: 12,814; python: 2,202; cpp: 1,715; makefile: 1,406; perl: 705; xml: 40
file content (179 lines) | stat: -rw-r--r-- 5,324 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#! /bin/sh
### BEGIN INIT INFO
# Provides:          nut-server upsd
# Required-Start:    $local_fs $syslog $network $remote_fs udev
# Required-Stop:     $local_fs $syslog $network $remote_fs udev
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Network UPS Tools initscript
# Description:       This script take care of starting and stopping the
#                    Network UPS Tools components. When needed, it also
#                    handle the UPS hardware shutdown.
### END INIT INFO

# Author: Arnaud Quette <aquette@debian.org>

PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin

NAME=nut-server
DESC="NUT - power devices information server and drivers"
CONFIG=/etc/nut/nut.conf
pid_dir=/var/run/nut
upsd_pid=${pid_dir}/upsd.pid
upsd=/sbin/upsd
upsdrvctl=/sbin/upsdrvctl
log=">/dev/null 2>/dev/null"

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

# set upsd specific options. use "man upsd" for more info
# this parameter is now located in nut.conf, and not in /etc/default/nut anymore
# FIXME: retrieved from 'nut' script during update 
UPSD_OPTIONS=""

# Exit if the package is not installed
[ -x "$upsd" ] || exit 0

# Include NUT nut.conf
[ -r $CONFIG ] && . $CONFIG

# Explicitly require the configuration to be done in /etc/nut/nut.conf
# redundant with nut-client
if [ "x$MODE" = "xnone" -o -z "$MODE" ] ; then
    log_action_msg "$NAME disabled, please adjust the configuration to your needs"
    log_action_msg "Then set MODE to a suitable value in $CONFIG to enable it"
    # exit success to avoid breaking the install process!
    exit 0
fi

# Check if /var/run/nut exists and has the correct perms
check_var_directory() {
  [ ! -d ${pid_dir} ] && mkdir -p ${pid_dir} \
    && chown root:nut ${pid_dir} \
    && chmod 770 ${pid_dir} \
    && [ -x /sbin/restorecon ] && /sbin/restorecon ${pid_dir}
}

# check if the right components are running
check_status() {
  case "$MODE" in
    standalone|netserver)
      status_of_proc -p $upsd_pid $upsd upsd
      # FIXME: need driver(s) status too!
      ;;
    none|netclient|*)
      # defered to nut-client
      #status_of_proc -p $upsmon_pid $upsmon upsmon
      ;;
  esac
}

start_stop_server () {
  case "$MODE" in
    standalone|netserver)
      case "$1" in
        start)
          # First, start driver(s)
          ! $upsdrvctl start >/dev/null 2>&1  &&  \
            log_progress_msg " (driver(s) failed)." || log_progress_msg " driver(s)."
          # Then, data server (upsd)
          start-stop-daemon -S -p $upsd_pid -x $upsd \
            -- $UPSD_OPTIONS >/dev/null 2>&1 &&
            log_progress_msg "upsd" || log_progress_msg "(upsd failed)"
          ;;
        stop)
          # FIXME: should stop nut-client first!
          # Reverse order for stop
          start-stop-daemon -K -o -p $upsd_pid -n upsd && #>/dev/null 2>&1 &&
            log_progress_msg "upsd" || log_progress_msg "(upsd failed)"
          ! /sbin/upsdrvctl stop >/dev/null 2>&1  &&  \
            log_progress_msg "(driver(s) failed)" || log_progress_msg "driver(s)"
          ;;
      esac
      ;;
    none|netclient|*)
      # now handled by nut-client
      return 1
      ;;
  esac
}

case "$1" in

  start)
    log_daemon_msg "Starting $DESC"
    check_var_directory
    start_stop_server start #&& log_progress_msg "upsd"
    #start_stop_client start && log_progress_msg "upsmon"
    log_end_msg 0
    ;;

  stop)
    log_daemon_msg "Stopping $DESC"
    start_stop_server stop #&& log_progress_msg "upsd"
    #start_stop_client stop && log_progress_msg "upsmon"
    log_end_msg 0
    ;;

  reload)
    $upsd   -c reload >/dev/null 2>&1
    #$upsmon -c reload >/dev/null 2>&1
    ;;

  restart|force-reload)
    log_daemon_msg "Restarting $DESC"
    #start_stop_client stop
    start_stop_server stop
    sleep 5
    check_var_directory
    start_stop_server start #&& log_progress_msg "upsd"
    #start_stop_client start && log_progress_msg "upsmon"
    log_end_msg 0
    ;;

  status)
    #log_daemon_msg "Checking status of $DESC"
    echo "Checking status of $DESC"
    check_status
    exit $?
    ;;

  poweroff)
    wait_delay=`sed -ne 's#^ *POWEROFF_WAIT= *\(.*\)$#\1#p' /etc/nut/nut.conf`
    # UPS poweroff action is actually done here.
    # But nut-monitor (Ie nut-client) does the check and call nut-server if needed!
    # This action MUST NOT be called directly, and thus is not exposed in 'Usage'
    case "$MODE" in
      standalone|netserver)
        log_daemon_msg "Shutting down the UPS ..."
        if $upsdrvctl shutdown ; then
          # FIXME (needed?): sleep 5
          log_progress_msg "Waiting for UPS to cut the power"
          log_end_msg 0
        else
          log_progress_msg "Shutdown failed."
          log_progress_msg "Waiting for UPS batteries to run down"
          log_end_msg 0
        fi
        if [ -n "$wait_delay" ] ; then
          log_daemon_msg " (will reboot after $wait_delay) ..."
          sleep "$wait_delay"
          invoke-rc.d reboot stop
        fi
        ;;
      none|netclient|*)
        # nothing to do
        ;;
    esac
    ;;

  *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|reload|restart|force-reload|status}" >&2
    exit 1
    ;;
esac

exit 0