improvements
[distro-setup] / mailbindwatchdog
1 #!/bin/bash
2
3 # When the system boots, systemd-resolved seems to recreate /run/systemd/resolve,
4 # or something, because the bindmounts to that directory do not always exist
5 # for units starting up at the same time. Anyways, removing and creating that
6 # directory definitely has the effect of deleting the bindmount, so
7 # here I solve for that ever happening.
8
9 if ! test "$BASH_VERSION"; then echo "error: shell is not bash" >&2; exit 1; fi
10 shopt -s inherit_errexit 2>/dev/null ||: # ignore fail in bash < 4.4
11 set -eE -o pipefail
12 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" exit status: $?, PIPESTATUS: ${PIPESTATUS[*]}" >&2' ERR
13
14 if (( $# == 0 )); then
15 echo error: expected service argument >&2
16 exit 1
17 fi
18
19 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
20
21 sleep 5
22 while true; do
23 sleep 20
24 for unit; do
25 pid=$(systemctl show --property MainPID --value $unit 2>/dev/null ||:)
26 case $pid in
27 [1-9]*)
28 if ! nsenter -t $pid -m timeout 20 mountpoint /run/systemd/resolve &>/dev/null; then
29 echo mail bind restart of $unit
30 timeout 60 systemctl restart $unit ||:
31 fi
32 ;;
33 esac
34 done
35 done