-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathneedrestart.sh
37 lines (32 loc) · 875 Bytes
/
needrestart.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
#!/bin/bash
# https://codepre.com/%F0%9F%90%A7-how-to-determine-which-services-to-restart-on-a-linux-system.html
IP=$(ip -o route get to 8.8.8.8 | sed -n 's/.*src \([0-9.]\+\).*/\1/p')
if ! [[ -d "/lib/modules/$(uname -r)" ]]; then
{
echo from: root
echo subject: Kernel update detected on "${IP}".
echo
echo "Reboot recommended."
} | sudo sendmail -d -t pi
exit 0
fi
SVCarray=()
while read line; do
if echo "${line}" | grep -q 'SVC'; then
IFS=" " read svc service <<< "${line}"
SVCarray+=($(echo "${service%%.*}"))
fi
done < <(sudo needrestart -b)
if (( ${#SVCarray[@]} )); then
{
echo from: root
echo subject: Some services on "${IP}" need a restart.
echo
echo "Services:"
for (( i=0; i<${#SVCarray[@]}; i++ )); do
echo "- ${SVCarray[i]}"
done
echo
echo "Run 'sudo needrestart' to restart services."
} | sudo sendmail -d -t pi
fi