Discussion:
Starting service conditionally and retrying when failure
(too old to reply)
sdrb
2017-10-05 04:51:26 UTC
Permalink
Raw Message
Hi,

I'd like to ask how to achieve following functionality in systemd:

I'd like to start alsa-restore.service only once just after one of the
specified files appears. Those file matches: /dev/snd/control*.

For now I have something like this:

[Unit]
Description=Save/Restore Sound Card State
ConditionPathExistsGlob=/dev/snd/control*
After=sysinit.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=-/sbin/alsactl restore -f /etc/asound.state

[Install]
WantedBy=multi-user.target


but during the start when there is no any of /dev/snd/control* files -
the condition is not meet and the service is not going up.
Those files appears later.

So that what I'd like to achieve is to wait for appearing of those files
and then starting alsa-restore service.

Is it possible to do in systemd? If yes - how to do this?
Camilo Aguilar
2017-10-05 05:13:39 UTC
Permalink
Raw Message
you need to active your service unit when your files show up by monitoring
the path using an additional path unit:
https://www.freedesktop.org/software/systemd/man/systemd.path.html
Post by sdrb
Hi,
I'd like to start alsa-restore.service only once just after one of the
specified files appears. Those file matches: /dev/snd/control*.
[Unit]
Description=Save/Restore Sound Card State
ConditionPathExistsGlob=/dev/snd/control*
After=sysinit.target
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=-/sbin/alsactl restore -f /etc/asound.state
[Install]
WantedBy=multi-user.target
but during the start when there is no any of /dev/snd/control* files -
the condition is not meet and the service is not going up.
Those files appears later.
So that what I'd like to achieve is to wait for appearing of those files
and then starting alsa-restore service.
Is it possible to do in systemd? If yes - how to do this?
_______________________________________________
systemd-devel mailing list
https://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
*Camilo Aguilar*
Software Engineer
http://github.com/c4milo
Michael Chapman
2017-10-05 05:27:55 UTC
Permalink
Raw Message
Post by Camilo Aguilar
you need to active your service unit when your files show up by monitoring
https://www.freedesktop.org/software/systemd/man/systemd.path.html
Anpother approach would be to have the unit get triggered by the device
appearing in udev.

See the systemd.device(5) manpage for details. You just need a udev rule
that adds a "systemd" TAG and a SYSTEMD_WANTS property to add a dependency
upon the unit you wish to trigger.
Mantas Mikulėnas
2017-10-05 05:38:33 UTC
Permalink
Raw Message
Post by sdrb
Hi,
I'd like to start alsa-restore.service only once just after one of the
specified files appears. Those file matches: /dev/snd/control*.
[Unit]
Description=Save/Restore Sound Card State
ConditionPathExistsGlob=/dev/snd/control*
After=sysinit.target
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=-/sbin/alsactl restore -f /etc/asound.state
[Install]
WantedBy=multi-user.target
but during the start when there is no any of /dev/snd/control* files - the
condition is not meet and the service is not going up.
Those files appears later.
So that what I'd like to achieve is to wait for appearing of those files
and then starting alsa-restore service.
Isn't that exactly what ALSA's
standard /usr/lib/udev/rules.d/90-alsa-restore.rules does?
--
Mantas Mikulėnas <***@gmail.com>
sdrb
2017-10-05 05:57:14 UTC
Permalink
Raw Message
Post by sdrb
Hi,
I'd like to start alsa-restore.service only once just after one of
the specified files appears. Those file matches: /dev/snd/control*.
[Unit]
Description=Save/Restore Sound Card State
ConditionPathExistsGlob=/dev/snd/control*
After=sysinit.target
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=-/sbin/alsactl restore -f /etc/asound.state
[Install]
WantedBy=multi-user.target
but during the start when there is no any of /dev/snd/control* files
- the condition is not meet and the service is not going up.
Those files appears later.
So that what I'd like to achieve is to wait for appearing of those
files and then starting alsa-restore service.
Isn't that exactly what ALSA's
standard /usr/lib/udev/rules.d/90-alsa-restore.rules does?
Thanks for all the responses. As I prefer the easiest solutions I have
chosen Camilo's solution with serviced.path.
Problem fixed. Thanks.

Loading...