Can oneshot service be used to decide whether simple service can be run?

I have a daemon simple service and i want to decide to run/ exit based on the output from oneshot service.

can i use “after” flags to decide to skip running the simple daemon service.

I return "exit 0 " for oneshot service if it succeeds and “exit 1” if it fails. And if it fails i want to skip running simple daemon service.

i can get this to work because during installation, the oneshot service cannot exit 1.

If you want this only during installation, it is easier to make your simple daemon install-mode: disable and use snapctl start --enable $SNAP_NAME.<service name> from the one-shot daemon…

In general though I would expect what you attempt to work (but since this relies 100% on systemd behavior, I’d check its documentation about service ordering too)

I want the ordering to work in general, like

oneshot --> exit0 --> start daemon services

oneshot --> exit1 --> do not start daemon services

But when the snap is installing, if the oneshot service returns “exit 1”. the installation fails.

Why does the one-shot service die at install? The behavior you describe definitely indicates that the ordering works as expected

I am sorry for the confusion, its not during install but when i restart the snap, it fails:

“snap restart turftank-mk2m-sw”

error: cannot perform the following tasks:

  • Run service command “restart” for running services of snap “turftank-mk2m-sw” (systemctl command [start snap.turftank-mk2m-sw.check-production-test.service] failed with exit status 1: Job for snap.turftank-mk2m-sw.check-production-test.service failed. See “systemctl status snap.turftank-mk2m-sw.check-production-test.service” and “journalctl -xe” for details. )"

Have you found out why exactly it fails ?

I’m asking because you could simply permanently make the simple daemon disabled and add some condition to one-shot1 that then calls snapctl start... (I.e. without --enable) every time, so one-shot would effectively become the “starter”

When i try to do what you suggested, where you mentioned by enabling and disabling the daemon simple services from oneshot,

I get this error:

error: error running snapctl: snap “turftank-mk2m-sw” has “service-control” change in progres

Wow !

not an error i have seen before ! Did you use install-mode: disable in your snapcraft.yaml as suggested ?

yeah i did use this mode

and you did a fresh install with the snap previously uninstalled (install-mode is only applied then, not on any kind of upgrades)

I tested it again now, when i installed the snap, the method you mentioned worked. But when i refreshed it did not work. Because there is snap refresh change going on, and that prevents the snapctl command to be run

Right, install-mode only applies at install (but then it is permanent) if you installed originally without install-mode it wont be changed on refreshes … similar to the install hook that only runs at first install, snapd makes a very strict distinction here between refresh and install …

I solved this by changing my one-shot service to daemon simple. Then, i wait till there are no snapd changes in progress and then i disable or enable my other services.

Thanks for the help

1 Like