Skip to content

Instantly share code, notes, and snippets.

@mharsch
Last active December 30, 2023 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mharsch/b1108372982a1921235563ee5f603807 to your computer and use it in GitHub Desktop.
Save mharsch/b1108372982a1921235563ee5f603807 to your computer and use it in GitHub Desktop.
run a service on a schedule (e.g. off-peak electricity rates)
We want to run a long-running process during certain hours, days-of-the-week, etc.
We want the service to start on a schedule, and be stopped on a corresponding schedule.
Example: Residential electric rates are based on Time of Use (TOU) such that Weekdays
from 1p to 7p are considered 'Peak Hours' when rates are much higher than all other times.
Our long running process uses significant electricity, so we want to stop running it
once we reach the Peak Hours period, and resume running once the Peak period is over.
1.) We create a systemd service file for the long running process (foo.service)
2.) We create a systemd timer unit associated with the service (foo.timer). This timer handles
starting the foo service (once peak period is over).
3.) We create a systemd service file to represent 'peak hours begin' (peak.service). This unit
'Conflicts' with foo service, and therefore foo.service is stopped when peak.service is started.
The peak.service is of type oneshot and is not a long-running process. It's only function is
to stop the conflicting foo.service on time.
4.) We create a peak.timer file to schedule the start of the peak period (which stops foo.service)
mharsch@vhagar:~/teamredminer-v0.9.1-linux$ cat /etc/systemd/system/miner.service
[Unit]
Description=teamredminer plus tuning
[Service]
Type=simple
ExecStart=/home/mharsch/teamredminer-v0.9.1-linux/start_eth.sh
[Install]
WantedBy=multi-user.target
mharsch@vhagar:~/teamredminer-v0.9.1-linux$ cat /etc/systemd/system/miner.timer
[Unit]
Description=start mining off-peak
[Timer]
OnCalendar=Mon..Fri 19:00
[Install]
WantedBy=timers.target
mharsch@vhagar:~/teamredminer-v0.9.1-linux$ cat /etc/systemd/system/peak.service
[Unit]
Description=peak rate period trigger
Conflicts=miner.service
[Service]
Type=oneshot
ExecStart=/bin/echo 'shutting down miner during peak rates'
mharsch@vhagar:~/teamredminer-v0.9.1-linux$ cat /etc/systemd/system/peak.timer
[Unit]
Description=stop mining
[Timer]
OnCalendar=Mon..Fri 13:00
[Install]
WantedBy=timers.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment