Skip to content

Instantly share code, notes, and snippets.

@chengscott
Last active June 23, 2024 13:28
Show Gist options
  • Save chengscott/0e8532a3ecf4f7d38241dd8ef0cf7763 to your computer and use it in GitHub Desktop.
Save chengscott/0e8532a3ecf4f7d38241dd8ef0cf7763 to your computer and use it in GitHub Desktop.
Run different jobs at specific time points on weekdays using a systemd template unit
systemctl daemon-reload
systemctl enable --now job@{08:00,12:00,21:00}.timer
#!/bin/bash -e
function fn_0800 {
echo 'do something at 08:00'
}
function fn_1200 {
echo 'do something at 12:00'
}
function fn_2100 {
echo 'do something at 21:00'
}
function fn_default {
echo 'Invalid invocation'
date -R
exit -1
}
case "$1" in
08:00) fn_0800;;
12:00) fn_1200;;
21:00) fn_2100;;
*) fn_default;;
esac
[Unit]
Description=Run job.sh %i
[Service]
ExecStart=/path/to/job.sh %i
[Timer]
OnCalendar=Mon..Fri %i
AccuracySec=0
[Install]
WantedBy=timers.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment