-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathautoreboot-on-segfaults.sh
executable file
·61 lines (50 loc) · 1.69 KB
/
autoreboot-on-segfaults.sh
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
#!/bin/sh
# ------------------------------------------------------------------------------
# Automatically reboots the system if there has been more than MAX_SEGFAULTS
# segmentation faults on the current boot. This is only intended as a temporary
# solution; one should really fix the software or the server instead!
#
# It is best to launch this using SystemD service & timer.
#
# Author : Esa Jokinen (oh2fih)
# Home : https://github.com/oh2fih/Misc-Scripts
# ------------------------------------------------------------------------------
MAX_SEGFAULTS="${MAX_SEGFAULTS:-10}"
REBOOT_WAITING_TIME="${REBOOT_WAITING_TIME:-5}"
SHUTDOWN_SCHEDULED="/run/systemd/shutdown/scheduled"
### Check for sudo privileges and the requirements.
if ! [ "$(id -u)" = 0 ]; then
echo "*** ERROR! This script requires sudo privileges."
exit 126
fi
if ! command -v journalctl > /dev/null 2>&1; then
echo "*** ERROR! This script requires journalctl!"
exit 127
fi
if ! command -v shutdown > /dev/null 2>&1; then
echo "*** ERROR! This script requires shutdown!"
exit 127
fi
### Skip the checks if reboot is already scheduled.
if test -f "$SHUTDOWN_SCHEDULED"; then
SHUTDOWN_DATE=$(
date -d @"$(
head -n 1 < "$SHUTDOWN_SCHEDULED" \
| cut -c6-15
)"
)
echo "Reboot/shutdown already scheduled at ${SHUTDOWN_DATE}."
exit 3
fi
### Count segfaults & schedule a reboot.
SF_COUNT=$(
journalctl -b -t "kernel" \
| grep -c "segfault at"
)
if [ "$SF_COUNT" -gt "$MAX_SEGFAULTS" ]; then
echo "Detected ${SF_COUNT} segfaults (>${MAX_SEGFAULTS}); scheduling reboot."
shutdown -r "$REBOOT_WAITING_TIME"
exit 1
else
echo "Detected ${SF_COUNT} segfaults (<=${MAX_SEGFAULTS}) on current boot."
fi