-
Notifications
You must be signed in to change notification settings - Fork 11
/
start_dependent_systemd_service
executable file
·51 lines (44 loc) · 2.14 KB
/
start_dependent_systemd_service
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
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/bash
#
# TODO: Maybe reread https://unix.stackexchange.com/questions/326181/systemd-unit-service-restart-if-another-service-start-or-reload
help() {
echo 'usage: start_dependent_systemd_service serviceA serviceB'
echo ' start_dependent_systemd_service serviceA serviceB --help'
echo
echo ' Will watch/monitor dbus for systemd events and will start'
echo ' serviceB when it sees that serviceA has started.'
echo
echo ' ATTENTION: this script will NOT sanitize the serviceA'
echo ' or serviceB parameters and pass them straight'
echo ' to awk and the shell!!! YOU HAVE BEEN WARNED!!!'
echo
echo ' Dependencies: gdbus, awk'
echo
echo ' Note: this script was originally created to solve the'
echo ' following problem:'
echo ' * you start some containers with `docker-compose`'
echo ' * you (or something) does'
echo ' `systemctl stop docker; systemctl stop docker`'
echo ' * your "docker-compose containers" will not get'
echo ' started...'
echo ' To solve this particular problem you better use'
echo ' `restart: until-stopped`.'
echo
exit 1
}
[ "$1" == "--help" ] && help
[ "$1" == "" ] && echo "You have to pass the name of serviceA to the script" >&2 && exit 1
[ "$2" == "" ] && echo "You have to pass the name of serviceB to the script" >&2 && exit 2
set -e # stop on error
set -u # stop on undefined variable
set -o pipefail # stop part of pipeline failing
SERVICE_A="$1"
SERVICE_B="$2"
# gdbus will output something like this, that is interesting for us:
#
# /org/freedesktop/systemd1: org.freedesktop.systemd1.Manager.JobRemoved (uint32 12045, objectpath '/org/freedesktop/systemd1/job/12045', 'testing.service', 'done')
#
gdbus monitor --system \
--dest org.freedesktop.systemd1 \
--object-path /org/freedesktop/systemd1 \
| awk "/^\/org\/freedesktop\/systemd1: org.freedesktop.systemd1.Manager.JobRemoved \(uint32.* '${SERVICE_A}.service', 'done'\)$/ { system(\"systemctl is-active ${SERVICE_A} && systemctl start ${SERVICE_B}.service\") }"