]> code.delx.au - monosys/blob - bin/find-services-to-restart
notes: fix raspi install notes, also @home -> @username
[monosys] / bin / find-services-to-restart
1 #!/bin/bash
2
3 function get_pids_to_restart {
4 sudo lsof +c 0 / | \
5 awk '/DEL|(deleted)/ { print $2 }' | \
6 sort -u
7 }
8
9 function find_service_for_pid {
10 systemctl status "$1" | \
11 awk '$2 ~ /\.service$/ && NR == 1 { print $2 }'
12 }
13
14 function is_cron_child {
15 if [ "$1" != "cronie.service" ]; then
16 return 1
17 fi
18 if systemctl show cronie -p MainPID | grep -q "$2"; then
19 return 1
20 fi
21 return 0
22 }
23
24 function echo_kill_pid {
25 echo "sudo kill $1 # $(ps -p"$1" -o user=,cmd=)"
26 }
27
28 function echo_restart_service {
29 echo "sudo systemctl restart $1"
30 }
31
32 for pid in $(get_pids_to_restart); do
33 if [ "$pid" = 1 ]; then
34 echo "sudo systemctl daemon-reexec"
35 exit 0
36 fi
37
38 service="$(find_service_for_pid "$pid")"
39 if is_cron_child "$service" "$pid"; then
40 echo_kill_pid "$pid"
41
42 elif [ -n "$service" ]; then
43 echo_restart_service "$service"
44
45 else
46 echo_kill_pid "$pid"
47 fi
48
49 done | sort -u
50