This page looks best with JavaScript enabled

Configure Scheduled Tasks with systemd timer

1. Preparing the Scheduled Script

If it is a Bash script, the first line needs to specify the interpreter.

1
2
mkdir -p /root/scripts
vim /root/scripts/quick-clear.sh
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#!/bin/sh

/usr/local/bin/kubectl --kubeconfig /root/.kube/config get pods --all-namespaces -o wide | grep Evicted | awk '{print $1,$2}' | xargs --no-run-if-empty -L1 /usr/local/bin/kubectl --kubeconfig /root/.kube/config delete pod -n || true
/usr/local/bin/kubectl --kubeconfig /root/.kube/config get pods --all-namespaces -o wide | grep Error | awk '{print $1,$2}' | xargs --no-run-if-empty -L1 /usr/local/bin/kubectl --kubeconfig /root/.kube/config delete pod -n || true
/usr/local/bin/kubectl --kubeconfig /root/.kube/config get pods --all-namespaces -o wide | grep Completed | awk '{print $1,$2}' | xargs --no-run-if-empty -L1 /usr/local/bin/kubectl --kubeconfig /root/.kube/config delete pod -n || true
/usr/local/bin/kubectl --kubeconfig /root/.kube/config get pipelinerun --all-namespaces -o wide | grep Succeeded | awk '{print $1,$2}' | xargs --no-run-if-empty -L1 /usr/local/bin/kubectl --kubeconfig /root/.kube/config delete pipelinerun -n || true
/usr/local/bin/kubectl --kubeconfig /root/.kube/config get pipelinerun --all-namespaces -o wide | grep PipelineRunTimeout | awk '{print $1,$2}' | xargs --no-run-if-empty -L1 /usr/local/bin/kubectl --kubeconfig /root/.kube/config delete pipelinerun -n || true
/usr/local/bin/kubectl --kubeconfig /root/.kube/config get pipelinerun --all-namespaces -o wide | grep InvalidWorkspaceBindings | awk '{print $1,$2}' | xargs --no-run-if-empty -L1 /usr/local/bin/kubectl --kubeconfig /root/.kube/config delete pipelinerun -n || true
/usr/local/bin/kubectl --kubeconfig /root/.kube/config get pipelinerun --all-namespaces -o wide | grep Failed | awk '{print $1,$2}' | xargs --no-run-if-empty -L1 /usr/local/bin/kubectl --kubeconfig /root/.kube/config delete pipelinerun -n || true

/usr/local/bin/kubectl --kubeconfig /root/.kube/config describe -A pvc | grep -E "^Name:.*$|^Namespace:.*$|^Used By:.*$" | grep -B 2 "<none>" | grep -E "^Name:.*$|^Namespace:.*$" | cut -f2 -d: | paste -d " " - - | xargs --no-run-if-empty -n2 bash -c '/usr/local/bin/kubectl --kubeconfig /root/.kube/config -n ${1} delete pvc ${0}' || true

/usr/bin/docker images | grep none | awk '{print $3}' | xargs --no-run-if-empty /usr/bin/docker rmi || true
/usr/bin/docker images |grep -E "([0-9a-z]+[-]){3,}[0-9]{9}" |awk '{print $3}' | xargs --no-run-if-empty /usr/bin/docker rmi || true
/usr/bin/docker system prune -f || true

Do not forget to add executable permissions.

1
chmod +x /root/scripts/quick-clear.sh

2. Adding the timer

1
vim /etc/systemd/system/quick-clear.timer
[Unit]
Description=clear every week

[Timer]
OnCalendar=*-*-* 5:00:00
Unit=quick-clear.service

[Install]
WantedBy=timers.target

Inside [Timer] you can use various parameters to control how often the scheduled task runs:

  • OnActiveSec, how long after the timer takes effect the task starts to run
  • OnBootSec, how long after the system boots the task starts to run
  • OnStartupSec, how long after the Systemd process starts the task starts to run
  • OnUnitActiveSec, how long after that unit last ran before it runs again
  • OnUnitInactiveSec, how long after the timer was last deactivated before it runs again
  • OnCalendar, run based on absolute time rather than relative time
  • AccuracySec, the maximum number of seconds the task may be postponed for various reasons; the default is 60 seconds

3. Adding the service

1
vim /etc/systemd/system/quick-clear.service
[Unit]
Description=clear every week
Conflicts= quick-clear.timer

[Service]
ExecStart=/root/scripts/quick-clear.sh

4. Managing Scheduled Tasks

  • Reload the configuration
1
systemctl daemon-reload
  • Start the scheduled task
1
systemctl start quick-clear.timer
  • Check the task status
1
systemctl status quick-clear.timer
  • Enable it at boot
1
systemctl enable quick-clear.timer
  • Check the timer log
1
2
3
4
journalctl -u quick-clear.timer -f

-- Logs begin at Wed 2022-03-30 12:30:27 CST. --
May 26 14:30:17 node3 systemd[1]: Started clear every week.
  • Check the task execution log
1
journalctl -u quick-clear.service -f
  • List all scheduled tasks
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
systemctl list-timers

NEXT                        LEFT          LAST                        PASSED       UNIT                         ACTIVATES
Thu 2022-05-26 15:54:35 CST 1h 23min left Thu 2022-05-26 03:42:34 CST 10h ago      ua-messaging.timer           ua-messaging.service
Thu 2022-05-26 17:58:00 CST 3h 27min left Wed 2022-05-25 17:58:00 CST 20h ago      systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
Thu 2022-05-26 19:56:45 CST 5h 26min left Thu 2022-05-26 11:45:53 CST 2h 44min ago fwupd-refresh.timer          fwupd-refresh.service
Fri 2022-05-27 00:00:00 CST 9h left       Thu 2022-05-26 00:00:00 CST 14h ago      logrotate.timer              logrotate.service
Fri 2022-05-27 00:00:00 CST 9h left       Thu 2022-05-26 00:00:00 CST 14h ago      man-db.timer                 man-db.service
Fri 2022-05-27 00:00:37 CST 9h left       Thu 2022-05-26 14:30:07 CST 35s ago      motd-news.timer              motd-news.service
Fri 2022-05-27 04:38:51 CST 14h left      Thu 2022-05-26 14:29:47 CST 55s ago      apt-daily.timer              apt-daily.service
Fri 2022-05-27 06:09:35 CST 15h left      Thu 2022-05-26 06:37:25 CST 7h ago       apt-daily-upgrade.timer      apt-daily-upgrade.service
Sun 2022-05-29 03:10:09 CST 2 days left   Sun 2022-05-22 03:10:55 CST 4 days ago   e2scrub_all.timer            e2scrub_all.service
Mon 2022-05-30 00:00:00 CST 3 days left   Mon 2022-05-23 00:00:00 CST 3 days ago   fstrim.timer                 fstrim.service
Wed 2022-06-01 18:53:47 CST 6 days left   n/a                         n/a          quick-clear.timer            quick-clear.service

5. References


WeChat Official Account
WRITTEN BY
WeChat Official Account