a programmable alarm clock using systemd
I've taught my laptop to wake up at 7:30 in the morning. When it does, it will run whatever's in my ~/bin/goodmorning script. Then, if the lid is still closed, it will go back to sleep again.
So, it's a programmable alarm clock that doesn't need the laptop to be left turned on to work.
But it doesn't have to make noise and wake me up (I rarely want to be woken up by an alarm; the sun coming in the window is a much nicer method). It can handle other tasks like downloading my email, before I wake up. When I'm at home and on dialup, this tends to take an hour in the morning, so it's nice to let it happen before I get up.
This took some time to figure out, but it's surprisingly simple. Besides ~/bin/goodmorning, which can be any program/script, I needed just two files to configure systemd to do this.
[Unit] Description=good morning [Timer] Unit=goodmorning.service OnCalendar=*-*-* 7:30 WakeSystem=true Persistent=false [Install] WantedBy=multi-user.target
[Unit] Description=good morning RefuseManualStart=true RefuseManualStop=true [Service] Type=oneshot ExecStart=/bin/systemd-inhibit --what=handle-lid-switch --why=goodmorning /bin/su joey -c /home/joey/bin/goodmorning
After installing these files, run (as root):
systemctl enable goodmorning.timer; systemctl start goodmorning.timer
Then, you'll also need to edit
LidSwitchIgnoreInhibited=no -- this overrides the default, which
is not to let systemd-inhibit block sleep on lid close.
The WakeSystem=true relies on some hardware support for waking from sleep; my laptop supported it with no trouble but I don't know how broadly available that is.
I don't think this would be anywhere near as easy to do without systemd, logind, etc. Especially the handling of waking the system at the right time, and the behavior around lid sleep inhibiting.
(It would perhaps be better to use the per-user systemd, not the system wide one. Then I could change the time the alarm runs without using root. What's prevented me from doing this is that systemd-inhibit uses policykit, and policykit prevents it from being used in this situation. It's a lot easier to run it as root and use su, than it is to reconfigure policykit.)