/
mahalt
executable file
·73 lines (63 loc) · 1.83 KB
/
mahalt
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
#!/bin/sh -eu
# Ma_Sys.ma Shutdown script 3.0.4, Copyright (c) 2012, 2017, 2020 Ma_Sys.ma.
# For further info send an e-mail to Ma_Sys.ma@web.de.
#
# Maintenace:
# Keep these lines in your /etc/sudoers file:
#
# Cmnd_Alias SHUTDOWN = /usr/bin/mahalt
# linux-fan ALL=NOPASSWD: SHUTDOW
#
# You might need to replace "linux-fan" by your username.
# When using groups add a "%" before the group, e.g.: "%mahalters"
if ! [ "$#" = 0 ] && { [ "$1" = "--help" ] || [ "$1" = "-h" ]; }; then
head -n 4 "$0" | tail -n 3 | cut -c 3-
cat <<EOF
USAGE $0 [-f [-r]
-f Fast -- skip execution of hooks from /usr/lib/mahalt.d
-r Reboot instead of shutdown
EOF
exit 0
fi
if [ "$(id -u)" = 0 ]; then
echo "mahalt$$: Got root permissions."
# checked twice to allow stopping running mahalt as well as cancelling
# a "scheduled" mahalt.
if [ -e /tmp/nohalt ]; then
echo "mahalt$$: found NOHALT file /tmp/nohalt. CANCEL!"
exit 64
fi
if [ $# -ge 1 ] && [ "$1" = "-f" ]; then
echo "mahalt$$: Hooks skipped."
shift
else
echo "mahalt$$: Invoking hooks..." | tee /var/log/mahalt
run-parts --exit-on-error --regex=".*" --verbose \
--arg=--by-mahalt /usr/lib/mahalt.d 2>&1 | \
tee -a /var/log/mahalt
echo "mahalt$$: Hook execution complete." | \
tee -a /var/log/mahalt
fi
echo "mahalt$$: Terminating Java applications..."
killall -s TERM java || \
echo "mahalt$$: No Java applications were running."
echo "mahalt$$: syncing..."
sync
if [ -e /tmp/nohalt ]; then
echo "mahalt$$: found NOHALT file /tmp/nohalt. CANCEL!"
exit 64
fi
if [ $# -ge 1 ] && [ "$1" = "-r" ]; then
exec /sbin/reboot
fi
exec /sbin/halt -p
else
echo "mahalt$$: Restarting with root permissions..."
# Try sudo and use su on fail
rv=0
sudo "$0" "$@" || rv=$?
if ! [ "$rv" = 0 ] && ! [ "$rv" = 64 ]; then
exec su -c "$0 $*"
fi
exit "$rv"
fi