Richard Bucker

systemd run once

Posted at — Nov 2, 2015

I’m building a reporting system that uses systemd timers to schedule the individual reports; each report being a self contained statically linked binary.At first I discovered the monotonic timers documented in systemd.timer but as I read and reread the man page I realized I was wrong. I had originally thought I could useOnCalendar=nowHowever that’s just wrong.  The OnCalendar field does not support monotonic timers. The only fields that support this functionality are:OnActiveSec=, OnBootSec=, OnStartupSec=, OnUnitActiveSec=, OnUnitInactiveSec=However, each of these have different behavior. For example OnBoot and OnStartup will trigger the timer regardless if the computed time is prior to now. While Boot and Startup will partially solve the problem they are not complete. I’m trying to run a report NOW; Boot and Startup will run it now and after a reboot or restart of systemd.The other 3 types require that the computed time is in the future. In one testcase I tried “1sec” which should have configured the timer 1 second in the future. The problem here is that by the time systemd reads the file and makes sense of the computed time the time may have already expired and so the report may not run. Several test cases failed and several passed. I cannot quite explain it except that.There is one other parameter that may play a role in this. Since I’m trying to run the report 1s after the active state… checking the AccuracySec field I see that the default accuracy is set to 1min. Meaning that the timing of the 1s monotonic timer is even more critical. Setting the AccuracySec to 1s might consume considerable resources so that’s not a good idea although possible.Finally, use OnActiveSec means that if I stop/start the timer that the report will run. Meaning it will run after a reboot or restart.In conclusion, with the exception of setting the exact one-time run based on the wallclock there isn’t a way to accomplish a “run now - run exactly once”.