Systemd: Restart all instances of an Instantiated Service at once
I use the nice feature of systemd: Instantiated Services.
Is there a simple way to reload all instantiated services with one call?
Example: I don't want to run all like this:
systemctl restart autossh@foo
systemctl restart autossh@bar
systemctl restart autossh@blu
I tried this, but this does not work
systemctl restart autossh@*
Related: Start N processes with one systemd service file
Update
First I was fascinated by Instantiated Services, but later I realized that running a configuration management tool like Ansible makes more sense. I learned: Keep the tools simple. Many tools starts to implement condition-checking (if .. else ...) and loops. For example webservers or mailserver congfiguration. But this should be solved at a different (upper) level: configuration management. See: https://github.com/guettli/programming-guidelines#dont-use-systemd-instantiated-units
Solution 1:
Systemd (starting from systemd-209) supports wildcards, however your shell is likely trying to expand them. Use quotes to pass wildcards to the systemctl/service command verbatim:
systemctl restart 'autossh@*'
Solution 2:
Not nice, but this works for systems with an old systemd:
systemctl list-units -t service --full| cut -d' ' -f1| grep mypattern | while read s; do systemctl restart $s; done
Of course the solution from above answer (systemctl restart 'autossh@*'
) is better.
Solution 3:
@weirdan Answer is correct, but is missing something for certain distributions.
For Centos 7 and similar, you can do:
systemctl (start|stop|restart|status) my-service@*
BUT, (start) will work ONLY, if you specify the flag "--all" :
systemctl (start) my-service@* --all
Otherwise, it will not find the services, since they do not exist. This is systemd intended feature.
For Ubuntu based systems, it works pretty much the same way, but the difference is, that the "--all" flag must be specified for all of the systemctl arguments, otherwise it will not do anything.
systemctl (start|stop|restart|status) 'my-service@*' --all