nut-debian/debian/nut-server.init

179 lines
5.1 KiB
Plaintext
Raw Normal View History

2010-02-24 23:58:53 +02:00
#! /bin/sh
### BEGIN INIT INFO
2012-01-28 20:26:35 +02:00
# Provides: nut-server upsd
2011-09-29 21:17:51 +03:00
# Required-Start: $local_fs $syslog $network $remote_fs udev
# Required-Stop: $local_fs $syslog $network $remote_fs udev
2010-02-24 23:58:53 +02:00
# 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
2012-01-28 20:26:35 +02:00
NAME=nut-server
2012-05-02 21:31:36 +03:00
DESC="NUT - power devices information server and drivers"
2010-02-24 23:58:53 +02:00
CONFIG=/etc/nut/nut.conf
2012-01-28 20:26:35 +02:00
pid_dir=/var/run/nut
upsd_pid=${pid_dir}/upsd.pid
upsd=/sbin/upsd
upsdrvctl=/sbin/upsdrvctl
log=">/dev/null 2>/dev/null"
2010-02-24 23:58:53 +02:00
2012-01-28 20:26:35 +02:00
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
2010-02-24 23:58:53 +02:00
. /lib/lsb/init-functions
# set upsd specific options. use "man upsd" for more info
2012-01-28 20:26:35 +02:00
# this parameter is now located in nut.conf, and not in /etc/default/nut anymore
# FIXME: retrieved from 'nut' script during update
2010-02-24 23:58:53 +02:00
UPSD_OPTIONS=""
2012-01-28 20:26:35 +02:00
# Exit if the package is not installed
[ -x "$upsd" ] || exit 0
2010-02-24 23:58:53 +02:00
# Include NUT nut.conf
2012-01-28 20:26:35 +02:00
[ -r $CONFIG ] && . $CONFIG
2010-02-24 23:58:53 +02:00
# Explicitly require the configuration to be done in /etc/nut/nut.conf
2012-01-28 20:26:35 +02:00
# redundant with nut-client
2010-02-24 23:58:53 +02:00
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() {
2012-01-28 20:26:35 +02:00
[ ! -d ${pid_dir} ] && mkdir -p ${pid_dir} \
&& chown root:nut ${pid_dir} \
&& chmod 770 ${pid_dir}
2010-02-24 23:58:53 +02:00
}
# check if the right components are running
check_status() {
case "$MODE" in
standalone|netserver)
2012-01-28 20:26:35 +02:00
status_of_proc -p $upsd_pid $upsd upsd
# FIXME: need driver(s) status too!
2010-02-24 23:58:53 +02:00
;;
2012-01-28 20:26:35 +02:00
none|netclient|*)
# defered to nut-client
#status_of_proc -p $upsmon_pid $upsmon upsmon
2010-02-24 23:58:53 +02:00
;;
esac
}
start_stop_server () {
case "$MODE" in
standalone|netserver)
case "$1" in
start)
2012-01-28 20:26:35 +02:00
# First, start driver(s)
2010-02-24 23:58:53 +02:00
! $upsdrvctl start >/dev/null 2>&1 && \
2012-05-02 21:31:36 +03:00
log_progress_msg " (driver(s) failed)." || log_progress_msg " driver(s)."
2012-01-28 20:26:35 +02:00
# 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)"
2010-02-24 23:58:53 +02:00
;;
stop)
2012-05-02 21:31:36 +03:00
# FIXME: should stop nut-client first!
2012-01-28 20:26:35 +02:00
# Reverse order for stop
start-stop-daemon -K -o -p $upsd_pid -n upsd && #>/dev/null 2>&1 &&
2012-05-02 21:31:36 +03:00
log_progress_msg "upsd" || log_progress_msg "(upsd failed)"
2010-02-24 23:58:53 +02:00
! /sbin/upsdrvctl stop >/dev/null 2>&1 && \
2012-05-02 21:31:36 +03:00
log_progress_msg "(driver(s) failed)" || log_progress_msg "driver(s)"
2010-02-24 23:58:53 +02:00
;;
esac
;;
none|netclient|*)
2012-01-28 20:26:35 +02:00
# now handled by nut-client
2010-02-24 23:58:53 +02:00
return 1
;;
esac
}
case "$1" in
start)
log_daemon_msg "Starting $DESC"
check_var_directory
2012-01-28 20:26:35 +02:00
start_stop_server start #&& log_progress_msg "upsd"
#start_stop_client start && log_progress_msg "upsmon"
2010-02-24 23:58:53 +02:00
log_end_msg 0
;;
stop)
log_daemon_msg "Stopping $DESC"
2012-01-28 20:26:35 +02:00
start_stop_server stop #&& log_progress_msg "upsd"
#start_stop_client stop && log_progress_msg "upsmon"
2010-02-24 23:58:53 +02:00
log_end_msg 0
;;
reload)
$upsd -c reload >/dev/null 2>&1
2012-01-28 20:26:35 +02:00
#$upsmon -c reload >/dev/null 2>&1
2010-02-24 23:58:53 +02:00
;;
restart|force-reload)
log_daemon_msg "Restarting $DESC"
2012-01-28 20:26:35 +02:00
#start_stop_client stop
2010-02-24 23:58:53 +02:00
start_stop_server stop
sleep 5
check_var_directory
2012-01-28 20:26:35 +02:00
start_stop_server start #&& log_progress_msg "upsd"
#start_stop_client start && log_progress_msg "upsmon"
2010-02-24 23:58:53 +02:00
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`
2012-01-28 20:26:35 +02:00
# 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
2010-02-24 23:58:53 +02:00
;;
*)
N=/etc/init.d/$NAME
2012-01-28 20:26:35 +02:00
echo "Usage: $N {start|stop|reload|restart|force-reload|status}" >&2
2010-02-24 23:58:53 +02:00
exit 1
;;
esac
exit 0