Supported snap hooks

Thanks for the suggestion, but if I understand correctly, it already does (following the paragraph on interface hooks). We should maybe make the link more prominent.

Ah sorry I misread thanks

1 Like

Is it possible to see what install hook prints to the console? Here’s my hook, a slightly modified example version:

#!/bin/sh -e

# Create a default config file
echo "sleep_time 5" > "$SNAP_DATA/hello.conf"

The hello.conf gets created, but there’re no messages in the console where snap install was executed.

The output is only shown if the hook exits with non-zero exit code for debugging purposes.

What information are you trying to show to the user during the install?

I’m trying to tell the user that manually connecting plugs is required after installation. Otherwise most of the functionality won’t work and the snap is useless (it needs several “unsafe” interfaces which don’t autoconnect).

Have you tried requesting auto-connection through the forum request documented at Process for aliases, auto-connections and tracks ?

Additionally, you can prompt them from a GUI window or a message from the terminal when they first use your application. For example, a zenity GUI prompt is implemented here:

Exactly what happens if a hook fails? For example, can a pre-refresh or post-refresh hook be used to prevent a snap refreshing if the hook determines that the refresh won’t work? Is this defined behaviour that we can rely upon?

1 Like

Yes this behavior can be relied on.


I’ve added a some extra text to the pre- and post-refresh hooks to hopefully make this behaviour and use-case clearer (cc @rbasak).


What’s the precise context in which a hook is run? I’d like a post-refresh hook run git-ubuntu.self-test (self-test is defined in snapcraft.yaml as an app) to verify that the new snap will work correctly. self-test already has a carefully defined environment that matches the main git-ubuntu app’s environment precisely. How can I ensure that when I invoke this from the post-refresh hook, the context is identical? Do I need to manually set the environment from inside the hook to the same as that defined in snapcraft.yaml against self-test? Or is there a better way?


Unfortunately not, what can make this slightly better is that if you don’t use the environment keyword for your self-test app, and do all setup except for running the app itself inside a script, you can then specify that script as a command-chain which can be shared between the app and the post-refresh hook with yaml like this:


then in your post-refresh hook you just call directly. Alternatively, you could make the post-refresh file itself a symlink to if you don’t need to do anything else in the post-refresh hook

1 Like

One thing I do not see documented is when to these hooks run relative to others and if the run before or after interfaces are connected

1 Like

also before or after services are started for i.e. first boot of an ubuntu core system with the snap seeded or just for initial installation

Hi @sergiusens

do you have information about this? We ran into some trouble during first boot on this.
We have a required snap that runs an install hook. Is this hook executed before or after the auto-connected interfaces?

Particularly we want to create a folder structure for a content-interface before any snap can write to this location. Both snaps are required snaps during image build. Currently we have this logic in the install hook of the slot snap.

Can we use an install hook to prompt the user for things like username and password in order to log into a service? If not, what approach do you suggest? :blush:

I would suggest merging the mentioned page with this. So much information is accumulating about snaps, and every time I find very difficult to find what I need :confused: I really like snaps, but as the complexity is growing, the documentation is becoming overwhelming :frowning_face:

This page seems to be missing any mention of the “check-health” hook.

This is because the feature isn’t yet fully implemented (see Health Checks). While some parts do work, such as using snapctl set-health and the health status appearing in the output from snap info, I don’t think the store yet has the functionality to process any health status. This is why we’ve held off documenting this fully and the hook specifically.

The explanation of install-device hook versus prepare-device hook seems confusing. install-device hook is described as supported only in UC20 gadget snaps, while prepare-device claims only in gadget snaps (not just UC20? is that deliberate?).

In addition, even though both hooks are defined in the gadget snap, for install-device, you’re referred to the “Installation process” page, while for prepare-device, you’re sent to “The gadget snap” page. This seems unnecessarily scattered; why are these two apparently related hooks not explained in the same place?

How do I access previous state of the snap from post-refresh hook? $SNAP will give me the current state but I want to access the state before the refresh and compare a file between the 2 states.