Notification on reboot initiation or coming up from reboot

I have a snap that provides a content slot for common folder usage. Other snaps use plug to access / modify this folder. Requirement is to have this folder in clean sate after coming up from a reboot.

Hence, I am looking for a way for my snap to get notified when the system is about to start rebooting or get notified once system has come up from reboot. Hooks are not applicable for me as they will triggered after refresh too.

One idea to consider would be to have a helper “oneshot” service in your snap that does the job on reboot - see https://snapcraft.io/docs/services-and-daemons

Had thought about this already. Problem is this oneshot service will run everytime snap comes up after a restart . I want to take action only after coming up from a reboot or before going to reboot.

I don’t this there’s a clear, non-racy way to detect the service is being run as part of the boot process. You could probably call systemctl is-system-running and look for initializing, but I don’t think any of the interfaces allows running systemctl binary.

Other ideas would be to use a /tmp to leave a flag. This is a tmpfs which is mounted when a snap mount namespace is assembled. If it’s a service, this would be done during the startup. @zyga-snapd can probably give some tips as to whether the /tmp would be discarded when the snap is refreshed.

The simpliest thing is probably just looking at /proc/uptime.

Please think about the consumers, and potential for races between the service that cleans the directory and ones that use that location.

If the files shared are small I really think you want to have a layout defining a tmpfs although that does not work well with sharing (due to what is really being shared being the public view and the tmpfs is the private view). Given that, perhaps it’s easier to organize the “owner” to clean up on startup. Having said that I don’t see a super clean solution that works precisely the way you want (clean on boot).

Thanks @mborzecki @zyga-snapd

Right. But how to differentiate between post reboot and post resfresh / restart?

Can think of using /proc/uptime, if it is safe to assume that refresh are not triggered by snapd until a specific time after boot. If above assumption can be made, then I can stitch a solution.