How to run snap app at specific time

Hello
I wrote backup app and want create snap for it and it is supposed to run this app form time to time (not permanently).

I googled over 2h and I am confusing… I find out:

  • snap package is mounted read only in user system
  • snapcraft.yaml define how to build snap
  • meta/snap.yaml in side package configure snap at runtime

I also find out here that meta/snap.yaml file can be configured (I don’t know how) to run snap app at specific time:


      # The service is activated by a timer, app must be a daemon. See timer
      # documentation for examples.
      # (snapd 2.33+)
      timer: 

Fortunatelly I have snapd v2.38 . So I think I can eventually use this feature.
I see 2 problems:

  1. How can I change meta/snap.yaml ? I ask because it is not clear to me whether is this file automatically generated or not.
  2. I need to allow users define they own scheduler (they should decide when they want to make backup). But when snap package is read only mounted (especially meta/snap.yaml file), then: How can I define scheduler from my app (it is supposed to run my app with command line options in order to define backup parameters).

best regards
Szyk Cech

1 Like

Snap timers are designed for application developers to use and are not user-configurable. You cannot change meta/snap.yaml. If you want you can create a systemd timer unit or a crontab entry to use schedule execution at a given time.

2 Likes

Thank you for your reply.
I have one more question:
Have I declare (in snapcraft.yaml) access in to /etc/system/systemd directory?
Or can I simply use sudo when I write config into user system?

Snaps cannot write to that location. Even as root snap application processes run inside the execution sandbox.

Perhaps what you want is to say, run once an hour after installation? You can define a snap timer in snapcraft.yaml, snapd will generate a corresponding systemd timer.

1 Like

It is backup app. I can’t imagine in what time period it should be run. It depends how many directories will be chosen to backup, how large they are, and what are target directories (some of them can be slow as pendrive, or network), and kind of type data is also important. The best solution for this kind of app is make them fully configurable. But it seems it is not possible…
It is a pity as it is very basic use case which is not covered by snap…
Thank you.

Everything is a basic feature. It is impossible to cover all the features someone may ask for.

who is it that needs to change the timer?

You have two or three approaches, depending on the answer to that question.

  1. the snap developer can ship the snap with a service that runs on a timer. This is useful for things that intrinsically need to run periodically, such as daily reporting service, or an hourly bandwidth usage summariser, or something like that.

  2. the system administrator (the same person that installed the snap) can create a timer themselves, to trigger something in the snap periodically. They access to the full syntax described in systemd.timer.

  3. the user could run a configuration script that requests a background service do something periodically. This one is the most tricky because the thing crosses a bunch of permission / sandbox lines, and getting it right might be hard.

Which one is it?

the user could run a configuration script that requests a background service do something periodically. This one is the most tricky because the thing crosses a bunch of permission / sandbox lines, and getting it right might be hard.

This is probably this case. Except it is not script - it is C++ app which is supposed to performs backup specific dirs (witch filter and compression) to specific target dirs.

how would you handle this case outside of snaps?

Now magic is encapsulated in my InstallKopia.sh script:

#!/bin/bash

lSystemdDir=’/etc/systemd/system’

sudo systemctl disable Kopia.timer
sudo rm -f $lSystemDir/Kopia.service
sudo rm -f $lSystemDir/Kopia.timer

sudo rm -f /usr/local/systemd/system/Kopia.service
sudo rm -f /usr/local/systemd/system/Kopia.timer

sudo mkdir -p $lSystemdDir
sudo cp ./Kopia.service $lSystemdDir
sudo chmod 644 $lSystemdDir/Kopia.service

sudo cp ./Kopia.timer $lSystemdDir/Kopia.timer
sudo chmod 644 $lSystemdDir/Kopia.timer

IFS=$’\n’

lDestIni=’/etc/xdg/xdg-plasma/Energo Kod/Kopia.ini’
sudo mkdir -p $(dirname $lDestIni)
sudo cp ./Kopia.ini $lDestIni
sudo chmod 644 $lDestIni

lDestIni=’/etc/xdg/Energo Kod/Kopia.ini’
sudo mkdir -p $(dirname $lDestIni)
sudo cp ./Kopia.ini $lDestIni
sudo chmod 644 $lDestIni

#sudo systemctl start Kopia.timer
sudo systemctl enable Kopia.timer
sudo systemctl start Kopia.timer
sleep 5
sudo systemctl daemon-reload

I want do the same with snap installer.

where is the bit where the user sets the time?

Now it is set in

/etc/systemd/system/Kopia.timer

And it is not customizable at the moment. But I want to make command line option for this.
My intention is:

  • make option: define directories for backup
  • make option: define directories where to store backups
  • make option: how often make backups (I wan’t store Kopia.timer template and update /etc/systemd/system with sudo permissions)

I think it is simple and power full.
Btw: If you know what generic packages allowed this please let me know (but I am not interested with distro specific packages).