File: nutshutdown.in

package info (click to toggle)
nut 2.8.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,356 kB
  • sloc: ansic: 123,904; sh: 14,718; cpp: 12,558; makefile: 5,212; python: 1,114; perl: 855; xml: 47
file content (91 lines) | stat: -rwxr-xr-x 3,882 bytes parent folder | download
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
#!/bin/sh

# Network UPS Tools (NUT) systemd-shutdown integration handler.
#
# NOTE: This script requires both nut-server package (or more specifically,
# the drivers for your device, which may be in further packages grouped
# by media/protocol and third-party dependencies), nut-client (upsmon),
# and their configuration files to be present locally and on still-mounted
# filesystems (may be read-only).
#
# Copyright (C) 2011-2025 by NUT contributors
# Michal Hlavinka, Laurent Bigonville, Arnaud Quette, Jim Klimov et al.
#
# See https://networkupstools.org/
# and https://github.com/networkupstools/nut/
#
# 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 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

if [ -s "@CONFPATH@/nut.conf" ]; then
    . "@CONFPATH@/nut.conf" || true
fi

[ x"${POWEROFF_QUIET-}" = xtrue ] \
|| POWEROFF_QUIET="false"

[ -x "@SBINDIR@/upsmon" ] || {
    $POWEROFF_QUIET || echo "$0: SKIP: could not locate '@SBINDIR@/upsmon''" >&2
    exit 1
}

# Will at most run the optional power-race avoidance sleep part
[ -x "@SBINDIR@/upsdrvctl" ] || {
    $POWEROFF_QUIET || echo "$0: WARNING: could not locate '@SBINDIR@/upsdrvctl' - will not command any UPS devices to shut down" >&2
}

if @SBINDIR@/upsmon -K >/dev/null 2>&1; then
    if [ -x "@SBINDIR@/upsdrvctl" ] ; then
        $POWEROFF_QUIET || {
            echo "$0: Commanding UPSes (if any) to shutdown" >&2
            [ -w /dev/console ] && echo "`TZ=UTC LANG=C date`: $0: Commanding UPSes (if any) to shutdown" >/dev/console || true
        }
        NUT_QUIET_INIT_NDE_WARNING=true
        export NUT_QUIET_INIT_NDE_WARNING
        @SBINDIR@/upsdrvctl shutdown || echo "$0: Something failed about UPS shutdown commands" >&2
    fi

    if [ -n "$POWEROFF_WAIT" ] ; then
        # Avoid the power-race condition (if wall power returned
        # while we were shutting down, so some UPSes would not
        # shutdown and/or powercycle the load as commanded above).
        # Sleep "long enough" to drain the battery if the UPS is
        # in fact on battery, or reboot if it became alive, so
        # this computer is not in limbo forever.
        $POWEROFF_QUIET || {
            echo "$0: Power-race avoidance: sleeping $POWEROFF_WAIT" >&2
            [ -w /dev/console ] && echo "`TZ=UTC LANG=C date`: $0: Power-race avoidance: sleeping $POWEROFF_WAIT" >/dev/console || true
        }

        # The argument may be anything compatible with /bin/sleep
        # (on OSes with systemd - assuming GNU coreutils or compatible,
        # so not necessarily a non-negative integer)
        /bin/sleep $POWEROFF_WAIT

        $POWEROFF_QUIET || {
            echo "$0: Power-race avoidance: sleep finished, rebooting..." >&2
            [ -w /dev/console ] && echo "`TZ=UTC LANG=C date`: $0: Power-race avoidance: sleep finished, rebooting..." >/dev/console || true
        }

        # We need to pass --force twice here to bypass systemd
        # and execute the reboot directly ourself.
        /bin/systemctl reboot --force --force
    else
        $POWEROFF_QUIET || echo "$0: Power-race avoidance: POWEROFF_WAIT is not configured at this time, proceeding to shutdown" >&2
    fi
else
    $POWEROFF_QUIET || echo "$0: SKIP: Not in FSD (killpower) mode at this time" >&2
fi

exit 0