Using Cron Within Snaps

I’m not sure how you can make the timer customizable, but if you’re okay with having the timer schedule statically defined in the snap, you can have a daemon in your snap set to run on a timer schedule, and then disable this daemon initially in the install hook and then your my-snap app would simply just enable or disable that daemon. So something like this for the app:

#/bin/bash -e
case "$1" in 
    start)
        snapctl start --enable "$SNAP_NAME.mysvc"
        ;;
    stop)
        snapctl stop --disable "$SNAP_NAME.mysvc"        ;;
esac

Then you would have an install hook that looks like this:

#/bin/bash
snapctl stop --disable "$SNAP_NAME.mvsvc"

The docs for the timer spec are here: Timer string format (admittedly they should be on docs.snapcraft.io but aren’t).

Also note that disabling the service on install may not work due correctly due to unfinished design at How to manage services with sockets/timers

2 Likes