23

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

guettli
  • 3,113
  • 14
  • 59
  • 110

4 Answers4

37

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@*'
weirdan
  • 496
  • 4
  • 5
  • Does not work for me: `systemctl status 'autossh@*'` output: `autossh@\x2a.service - ... Active: inactive (dead)` Version: `systemd 195` – guettli Aug 22 '16 at 10:00
  • 2
    Globbing is available starting from systemd 209: https://lists.freedesktop.org/archives/systemd-devel/2014-February/017146.html – weirdan Aug 22 '16 at 14:16
  • If you have a lot of services to restart, the while loop in guettli's answer will work better, as the wildcard seems to try to restart them all at once. Tested with ~1000 php-fpm services, load went up to 400 and I had to reboot the server. – boutch55555 Jun 27 '17 at 13:00
  • 1
    Works if the processes are already running, but not if they have to be started for the first time after they have been enabled. But you can use `systemctl restart autossh@{1..2}` (with any N instead of `2`) which is slightly less general but it will also start the processes if they are `inactive (dead)`. – giorgiosironi Nov 21 '17 at 13:21
8

@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
ku4eto
  • 143
  • 1
  • 5
7

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.

guettli
  • 3,113
  • 14
  • 59
  • 110
  • 1
    Beware: `cut` may parse the wrong field in case there are failed services in list-units output. – Juuso Ohtonen Feb 09 '18 at 07:49
  • this version solves the cut problem `systemctl list-units -t service --full | grep my_pattern | awk '{print $1}' | xargs -i systemctl stop \{\}` – Tk421 Jun 02 '20 at 01:20
2

I don't know if it's there an option for a wildcard on the terminal for systemd. The what you can do is adding one on your systemd scripts.

The %i would do the trick I think but is related on the way you scripted the instantiated services.

You may find an explanation here referred as specifiers

which shows that:

%n

full unit name

%p

For instantiated units, this refers to the string before the "@" character of the unit name. For non-instantiated units, this refers to the name of the unit with the type suffix removed.

%i

For instantiated units: this is the string between the "@" character and the suffix of the unit name.unit name

I'm not directly answering your question, but for what I guess you are trying to achieve. If you think your solution may be found following this idea, please share your systemd script, so we can eventually illustrate with examples and maybe even providing you the final script.

If you feel confident in editing your own script to reach a solution that way, here you have an example (I won't quote it as I don't know if it is relevant for the solution, and is too specific to what I'm proposing)

ignivs
  • 449
  • 5
  • 11