Please enable Javascript to view the contents

使用 systemd timer 配置定时任务

 ·  ☕ 2 分钟

1. 准备定时脚步

如果是 Bash 脚本,第一行需要指定解释器。

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

别忘了添加可执行权限。

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

2. 添加 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

[Timer] 中可以使用各种参数控制定时任务的执行周期:

  • OnActiveSec,定时器生效后,多少时间开始执行任务
  • OnBootSec,系统启动后,多少时间开始执行任务
  • OnStartupSec,Systemd 进程启动后,多少时间开始执行任务
  • OnUnitActiveSec,该单元上次执行后,等多少时间再次执行
  • OnUnitInactiveSec,定时器上次关闭后多少时间,再次执行
  • OnCalendar,基于绝对时间,而不是相对时间执行
  • AccuracySec,如果因为各种原因,任务必须推迟执行,推迟的最大秒数,默认是60秒

3. 添加 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. 管理定时任务

  • 加载配置
1
systemctl daemon-reload 
  • 启动定时任务
1
systemctl start quick-clear.timer
  • 查看任务状态
1
systemctl status quick-clear.timer
  • 开机自启动
1
systemctl enable quick-clear.timer
  • 查看定时日志
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.
  • 查看任务执行日志
1
journalctl -u quick-clear.service -f
  • 查看全部定时任务
 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. 参考


微信公众号
作者
微信公众号