shut down system when multiple systemd units stop
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
I'm trying to shut down the computer when a specific list of systemd units stop.
The background is that I have a compute farm in Google cloud & AWS EC2, and each instance launches a couple services which perform a bunch of work and then exit when complete. Since these are cloud instances and cost money, the instance should shut down once it is no longer doing anything.
Most suggestions I've seen around this involve putting an ExecStopPost=/sbin/init 0
(or similar) in the systemd unit file. However I cannot shut the instance down until all the units have completed.
It's easy enough for me to create a target
unit which wraps the individual worker units, and then put a StopWhenUnneeded=true
in it so that the target stops once all the workers are stopped. But I don't see a way to use this to trigger the system to shut down (for example by invoking systemd-halt.service
).
So the question is: how can I get the system to shut down once a list of units, or my target unit, stops.
systemd
add a comment |Â
up vote
4
down vote
favorite
I'm trying to shut down the computer when a specific list of systemd units stop.
The background is that I have a compute farm in Google cloud & AWS EC2, and each instance launches a couple services which perform a bunch of work and then exit when complete. Since these are cloud instances and cost money, the instance should shut down once it is no longer doing anything.
Most suggestions I've seen around this involve putting an ExecStopPost=/sbin/init 0
(or similar) in the systemd unit file. However I cannot shut the instance down until all the units have completed.
It's easy enough for me to create a target
unit which wraps the individual worker units, and then put a StopWhenUnneeded=true
in it so that the target stops once all the workers are stopped. But I don't see a way to use this to trigger the system to shut down (for example by invoking systemd-halt.service
).
So the question is: how can I get the system to shut down once a list of units, or my target unit, stops.
systemd
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I'm trying to shut down the computer when a specific list of systemd units stop.
The background is that I have a compute farm in Google cloud & AWS EC2, and each instance launches a couple services which perform a bunch of work and then exit when complete. Since these are cloud instances and cost money, the instance should shut down once it is no longer doing anything.
Most suggestions I've seen around this involve putting an ExecStopPost=/sbin/init 0
(or similar) in the systemd unit file. However I cannot shut the instance down until all the units have completed.
It's easy enough for me to create a target
unit which wraps the individual worker units, and then put a StopWhenUnneeded=true
in it so that the target stops once all the workers are stopped. But I don't see a way to use this to trigger the system to shut down (for example by invoking systemd-halt.service
).
So the question is: how can I get the system to shut down once a list of units, or my target unit, stops.
systemd
I'm trying to shut down the computer when a specific list of systemd units stop.
The background is that I have a compute farm in Google cloud & AWS EC2, and each instance launches a couple services which perform a bunch of work and then exit when complete. Since these are cloud instances and cost money, the instance should shut down once it is no longer doing anything.
Most suggestions I've seen around this involve putting an ExecStopPost=/sbin/init 0
(or similar) in the systemd unit file. However I cannot shut the instance down until all the units have completed.
It's easy enough for me to create a target
unit which wraps the individual worker units, and then put a StopWhenUnneeded=true
in it so that the target stops once all the workers are stopped. But I don't see a way to use this to trigger the system to shut down (for example by invoking systemd-halt.service
).
So the question is: how can I get the system to shut down once a list of units, or my target unit, stops.
systemd
edited Oct 30 '17 at 4:30
asked Oct 30 '17 at 1:29
Patrick
47.9k11125176
47.9k11125176
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
After some experimentation, one working, not entirely ugly, solution I've come up with is to use the idea of the wrapper target
unit, but use a service
instead which shuts the system down on exit.
For example, the worker service:
# worker@.service
[Unit]
BindsTo=master.service
After=master.service
[Service]
ExecStart=/bin/myworker
[Install]
WantedBy=master.service
And then for the wrapper:
# master.service
[Unit]
StopWhenUnneeded=true
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/sbin/init 0
I can then do systemctl start master.service
to start all the workers (after systemctl enable
ing them), and when they all exit, master.service
will stop and execute init 0
.systemctl stop master.service
will also signal the worker units to stop, but will wait for them before executing the init 0
.
The ugliness is having to create a fake service which executes /bin/true
. It would be nicer if there were an existing systemd unit that when stopped would result in the system shutting down, but this is at least functional.
It's not that ugly though.
â EightBitTony
Oct 30 '17 at 9:45
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
After some experimentation, one working, not entirely ugly, solution I've come up with is to use the idea of the wrapper target
unit, but use a service
instead which shuts the system down on exit.
For example, the worker service:
# worker@.service
[Unit]
BindsTo=master.service
After=master.service
[Service]
ExecStart=/bin/myworker
[Install]
WantedBy=master.service
And then for the wrapper:
# master.service
[Unit]
StopWhenUnneeded=true
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/sbin/init 0
I can then do systemctl start master.service
to start all the workers (after systemctl enable
ing them), and when they all exit, master.service
will stop and execute init 0
.systemctl stop master.service
will also signal the worker units to stop, but will wait for them before executing the init 0
.
The ugliness is having to create a fake service which executes /bin/true
. It would be nicer if there were an existing systemd unit that when stopped would result in the system shutting down, but this is at least functional.
It's not that ugly though.
â EightBitTony
Oct 30 '17 at 9:45
add a comment |Â
up vote
2
down vote
accepted
After some experimentation, one working, not entirely ugly, solution I've come up with is to use the idea of the wrapper target
unit, but use a service
instead which shuts the system down on exit.
For example, the worker service:
# worker@.service
[Unit]
BindsTo=master.service
After=master.service
[Service]
ExecStart=/bin/myworker
[Install]
WantedBy=master.service
And then for the wrapper:
# master.service
[Unit]
StopWhenUnneeded=true
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/sbin/init 0
I can then do systemctl start master.service
to start all the workers (after systemctl enable
ing them), and when they all exit, master.service
will stop and execute init 0
.systemctl stop master.service
will also signal the worker units to stop, but will wait for them before executing the init 0
.
The ugliness is having to create a fake service which executes /bin/true
. It would be nicer if there were an existing systemd unit that when stopped would result in the system shutting down, but this is at least functional.
It's not that ugly though.
â EightBitTony
Oct 30 '17 at 9:45
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
After some experimentation, one working, not entirely ugly, solution I've come up with is to use the idea of the wrapper target
unit, but use a service
instead which shuts the system down on exit.
For example, the worker service:
# worker@.service
[Unit]
BindsTo=master.service
After=master.service
[Service]
ExecStart=/bin/myworker
[Install]
WantedBy=master.service
And then for the wrapper:
# master.service
[Unit]
StopWhenUnneeded=true
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/sbin/init 0
I can then do systemctl start master.service
to start all the workers (after systemctl enable
ing them), and when they all exit, master.service
will stop and execute init 0
.systemctl stop master.service
will also signal the worker units to stop, but will wait for them before executing the init 0
.
The ugliness is having to create a fake service which executes /bin/true
. It would be nicer if there were an existing systemd unit that when stopped would result in the system shutting down, but this is at least functional.
After some experimentation, one working, not entirely ugly, solution I've come up with is to use the idea of the wrapper target
unit, but use a service
instead which shuts the system down on exit.
For example, the worker service:
# worker@.service
[Unit]
BindsTo=master.service
After=master.service
[Service]
ExecStart=/bin/myworker
[Install]
WantedBy=master.service
And then for the wrapper:
# master.service
[Unit]
StopWhenUnneeded=true
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/sbin/init 0
I can then do systemctl start master.service
to start all the workers (after systemctl enable
ing them), and when they all exit, master.service
will stop and execute init 0
.systemctl stop master.service
will also signal the worker units to stop, but will wait for them before executing the init 0
.
The ugliness is having to create a fake service which executes /bin/true
. It would be nicer if there were an existing systemd unit that when stopped would result in the system shutting down, but this is at least functional.
answered Oct 30 '17 at 4:30
Patrick
47.9k11125176
47.9k11125176
It's not that ugly though.
â EightBitTony
Oct 30 '17 at 9:45
add a comment |Â
It's not that ugly though.
â EightBitTony
Oct 30 '17 at 9:45
It's not that ugly though.
â EightBitTony
Oct 30 '17 at 9:45
It's not that ugly though.
â EightBitTony
Oct 30 '17 at 9:45
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f401328%2fshut-down-system-when-multiple-systemd-units-stop%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password