6

Is there a way to make a service wanted by multiple targets (or to have multiple install directives) so that when installed a symbolic link is created in all places.

My example is the following systemd file

[Unit]
Description=Run script to fix-audio mappings

[Service]
Type=forking
ExecStart=/bin/bash -c '/usr/local/bin/fix-audio &'
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

What this does is run a script to fix the audio mappings for my machine (without it sound only comes from the subwoofer - which is pretty useless). Unfortunately after sleep/suspend/hibernate the mappings seem to reset so I also need this script to be triggered when waking up.

Is there a way to say WantedBy=multi-user.target,sleep.target or similar?

I cannot seem to find any documentation about this. Alternatively, is there a better way to achieve what I need?

7

Yes there is, quoting the relevant section of the man page for system.unit:

WantedBy=, RequiredBy= This option may be used more than once, or a space-separated list of unit names may be given. A symbolic link is created in the .wants/ or .requires/ directory of each of the listed units when this unit is installed by systemctl enable. This has the effect that a dependency of type Wants= or Requires= is added from the listed unit to the current unit. The primary result is that the current unit will be started when the listed unit is started. See the description of Wants= and Requires= in the [Unit] section for details.

WantedBy=foo.service in a service bar.service is mostly equivalent to Alias=foo.service.wants/bar.service in the same file. In case of template units, systemctl enable must be called with an instance name, and this instance will be added to the .wants/ or .requires/ list of the listed unit. E.g. WantedBy=getty.target in a service getty@.service will result in systemctl enable getty@tty2.service creating a getty.target.wants/getty@tty2.service link to getty@.service.

Although if you want to run this for several targets make sure to adjust ordering if necessary (e.g. for shutdown or suspend) via Before= or After= directives (which are also explained in the man page ;) ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.