summaryrefslogtreecommitdiff
path: root/autorestart/scripts/systemd-restart-as-needed
blob: c8099ee81b0ddcb640f56c5db5451ba5dcd0b89f (plain)
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
#!/bin/bash

TMP="/tmp"
DELETED="$TMP/restart.deleted"
OWNED="$TMP/restart.owned"
UPDATED="$TMP/restart.updated"
UNITS="$TMP/restart.units"

lsof -n | awk '{ if ($5 == "DEL" || $10 == "(deleted)") print $9,$2; }' | sort -uk 1,2 > $DELETED
find /var/db/pkg/ -name CONTENTS | xargs cat | awk '{ if ($1 != "dir") print $2; }' | xargs realpath 2> /dev/null | sort > $OWNED
join $DELETED $OWNED | awk '{ print $2 }' | sort -gu > $UPDATED

(for pid in $(cat $UPDATED) ; do
	systemctl status $pid | head -n 1
done) | sort -u > $UNITS

if [ -s $UNITS ] ; then
	(
		echo "Restarting units:"
		for unit in $(awk '{print $2}' $UNITS) ; do
			if ( echo $unit | grep $( ls -1 /etc/autorestart/ignore.d/* | sed -e 's/^/-f /' ) > /dev/null ) ; then
				echo "Skipping $unit"
			else
				systemctl restart $unit
				systemctl status $unit
			fi
			echo
		done
	) | mailx -a "Content-Type: text/plain; charset=utf-8" \
			 -s "$(hostname) systemd units requiring restart" $(whoami)
fi

rm -f $TMP/restart.*