I’ve created a snap which consist of an app and its service. However, right after snap install, service will start automatically which is not the behavior I want.
What I’m trying to achieve is:
service to be disabled by default after snap install
have ability to enable/start service by setting environment variables within the app
But part where I fail is when I try to set value of this env. variable within the app. After this if I go into shell within the snap, I still see that “echo $DAEMON” is equal to “DISABLED” and with this daemon will fail to start.
I tried few things without much success. Does anyone have any suggestion how I could change env. variable within snap environment “on fly” and with it start/stop app related service?
You can disable the service with an install hook that runs snapctl stop --disable <snap-name>.<service-name>. Then your app can inspect the environment variable and if it’s what you expect, enable the service again with snapctl start --enable <snap-name>.<service-name>.
Another approach we’ve been using is to have a simple shell wrapper around the service that ensures all of its prerequisites are set up, and either exit (possibly with an error code) or exec $daemon "$@" otherwise.
ideally I would like to not have a bash wrapper around my Python app. But, considering snapctl is failing for me at this point, if you have an example of what you’re suggesting I’d be willing to give it a try.
Right, what I pasted was output of running all those commands inside a snap. I also tried same by being root user (instead of sudo) and ended up with same errors …
How were you running inside the snap? sudo should not work inside a snap, which is why I’m not sure if you were running inside a snap. For example, I would run something inside a snap like this:
$ snap run --shell docker
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
$ snapctl services
Service Startup Current Notes
docker.dockerd disabled inactive -
$ snapctl start --enable docker
error: error running snapctl: unknown flag `enable'
$ sudo snapctl start --enable docker
bash: /usr/bin/sudo: Permission denied
$ exit
$ sudo snap run --shell docker
# snapctl start --enable docker
# snapctl services
Service Startup Current Notes
docker.dockerd enabled active -
I do realize that it is misleading when we do snap run --shell as an unpriviledged user it says “use sudo …” when that in fact will never work from inside a strictly confined snap.
Also, to clarify, are you running these commands from inside a strictly confined snap or a classic snap?
Thank you for your clarification, this did the trick:
sudo snap run --shell auto-cpufreq
After which I’m able to run:
snapctl stop --disable auto-cpufreq
Since I’m still in development I’m using devmode as confinement, but even after I’m done I’m planning to use classic.
Now I just need to figure out how I’m going to make this call as part of my Python app, if you have any suggestions for that as well please let me know.
Regardless, thank you for your assistance, much appreciated!
Just another approach, I feel like if there’s a prerequisite of a service that isn’t met, the service itself should react to that. With snapctl you need an external thing (like the configure hook) to manage the service. But what if the user disables the service manually and then the configure hook runs - the service is enabled again.