-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhost-trigger-check
executable file
·48 lines (42 loc) · 1.64 KB
/
host-trigger-check
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
#!/bin/bash
#
# This script is part of apcupsd-docker by Leroy Förster.
# It is designed to be run by regularly (e.g. by cron). If it reads a first line '1' in the TRIGGERFILE, it will run the 'action()' function and replace the '1' with a '0'.
# Please change the content of the TRIGGERFILE variable and the action() function according to your needs. This has to be run on the host, not within a container, to be able to shut the machine down.
# Path to trigger file shared on host/container via Volume
USERNAME="USERNAMEGOESHERE"
TRIGGERDIR="/home/$USERNAME/apcupsd-discord/trigger"
TRIGGERFILE="$TRIGGERDIR/apcupsdtrigger"
# The message that will be send through Discord
MSG="💤 Host triggered clean shut down due to low UPS power 💤"
# Put everything you want to do on a shutdown condition inside this function.
action(){
echo "Detected '1' in shutdown trigger '$TRIGGERFILE'."
/bin/bash -c "/home/$USERNAME/apcupsd-discord/eventscriptoverrides/common-notifydiscord '$MSG'"
# give time to finish sending messages
/usr/bin/sleep 5
# Shutdown in 1 min if supported or shutdown now
/sbin/shutdown -P +1 || /sbin/shutdown/shutdown -P now
}
### Do not change below unless you know what you are doing ###
if [ ! -d "$TRIGGERDIR" ]; then
echo "no trigger dir"
mkdir $TRIGGERDIR
echo "created trigger dir"
fi
if test -f "$TRIGGERFILE"; then
echo "trigger exists"
read first_line < $TRIGGERFILE
if [ "$first_line" == "1" ]; then
echo "first line is 1"
echo "0" > $TRIGGERFILE
action;
echo "ran action"
fi
else
echo "creating trigger file"
touch $TRIGGERFILE
echo "0" > $TRIGGERFILE
echo "created trigger file"
fi
exit 0