Say I want a a program to be active between 12:00h and 13:00h on a desktop machine (i.e. if PC is on at either time cannot be guaranteed). I'll assume I can use systemd units for this.
Starting service A
with a timer and accounting for poweroff times is simple:
A.timer
[Timer]
Unit=A
OnCalendar=*-*-* 12:00:00
Persistent=true
Stopping A
could be done with a conflicting service as suggested earlier
A.service (with conflict):
#add conflict to A.service
[Unit]
Description=This is A.service
Conflicts=killA.service
#make sure killA considers A to be active
RemainAfterExit=yes
killA.service:
[Unit]
Description=kills A
[Service]
Type=oneshot
ExecStart=/bin/true
#kill only if A is running:
Requires=A.service
killA.timer
[Timer]
Unit=killA
OnCalendar=*-*-* 13:00:00
Persistent=true
Now there are several possible scenarios:
- Powered on from 11h-14h: start and stop as expected
Powered on from 11h to 12:30h: start as expected but never stopped -> what will happen at next poweron?
- at 12:45: will A start? (last start less than 24h ago)
- at 14h: killA should not run as A should not be active
- at 12:15 next day? (last start less than 24 h ago, but killA start was missed)
- at 12:45 next day? (last start >24h ago, but killA <24h ago. Will it be stopped by killA?)
Powered on from 12:30 to 14h: start and stop as expected (due to persistence in A.timer)
powered on at 14h: A starts due to persistence. Will killA deactivate it immediately due to dependency? Can starting A be avoided in the first place?
In short:
How to use systemd timers to make sure a service is active in a given time interval and inactive outside the time interval independently of shutdowns/boots ?
Hands-on use-case example: allow ssh
-access only at working hours.
PS: Maybe I just don't get systemd timers very well, yet?