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