I'm wondering how to stop all units that are grouped together by the same target.My setup is as follows. I have several unit config files that read:[Unit]...[Service]...[Install]WantedBy=mycustom.targetWhen I run# systemctl start mycustom.targetThose units that are wanted by mycustom.target start correctly. Now, I would also like to be able stop all units that are wanted by mycustom.target. I tried:# systemctl stop mycustom.targetThis doesn't do anything though. Is there a way to make this work without having to stop all units that are (explicitly) wanted by the same target?
How to stop all units belonging to the same target?
systemd
There are two ways: an implicit and an explicit.First way  StopWhenUnneeded=The first way is to use the StopWhenUnneeded= directive. If a unit has StopWhenUnneeded=yes, it will be automatically stopped when there becomes no active unit which Wants=/Requires= the unit in question. Hence:this will only work if these units are WantedBy= only by mycustom.target;you will be unable to start any of these units manually (i. e. systemctl start myunit.service will start it and immediately stop it afterwards).Second way  a shell pipelineThe second way is to construct a simple shell pipeline, using systemctl show -p to extract the dependency list of mycustom.target.More specifically, systemctl show UNIT will show all properties of a unit in a KEY=VALUE form, and systemctl show -p PROPERTIES UNIT will do the same, limiting the set of shown properties. So:systemctl stop -- $(systemctl show -p Wants mycustom.target | cut -d= -f2)