1. Prepare the timing script

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/bin/sh
/usr/local/bin/kubectl get pods --all-namespaces -o wide | grep Evicted | awk '{print $1,$2}' | xargs -L1 /usr/local/bin/kubectl delete pod -n
/usr/local/bin/kubectl get pods --all-namespaces -o wide | grep Error | awk '{print $1,$2}' | xargs -L1 /usr/local/bin/kubectl delete pod -n
/usr/local/bin/kubectl get pods --all-namespaces -o wide | grep Completed | awk '{print $1,$2}' | xargs -L1 /usr/local/bin/kubectl delete pod -n
/usr/local/bin/kubectl get pipelinerun --all-namespaces -o wide | grep Succeeded | awk '{print $1,$2}' | xargs -L1 /usr/local/bin/kubectl delete pipelinerun -n
/usr/local/bin/kubectl get pipelinerun --all-namespaces -o wide | grep PipelineRunTimeout | awk '{print $1,$2}' | xargs -L1 /usr/local/bin/kubectl delete pipelinerun -n
/usr/local/bin/kubectl get pipelinerun --all-namespaces -o wide | grep InvalidWorkspaceBindings | awk '{print $1,$2}' | xargs -L1 /usr/local/bin/kubectl delete pipelinerun -n
/usr/local/bin/kubectl get pipelinerun --all-namespaces -o wide | grep Failed | awk '{print $1,$2}' | xargs -L1 /usr/local/bin/kubectl delete pipelinerun -n

/usr/local/bin/kubectl describe -A pvc | grep -E "^Name:.*$|^Namespace:.*$|^Used By:.*$" | grep -B 2 "<none>" | grep -E "^Name:.*$|^Namespace:.*$" | cut -f2 -d: | paste -d " " - - | xargs -n2 bash -c '/usr/local/bin/kubectl -n ${1} delete pvc ${0}'
/usr/bin/docker images | grep none | awk '{print $3}' | xargs /usr/bin/docker rmi
/usr/bin/docker images |grep -E "([0-9a-z]+[-]){3,}[0-9]{9}" |awk '{print $3}' | xargs /usr/bin/docker rmi

2. add timer

1
vim /etc/systemd/system/quick-clear.timer
1
2
3
4
5
6
7
8
9
[Unit]
Description=clear every week

[Timer]
OnUnitActiveSec=168h
Unit=quick-clear.service

[Install]
WantedBy=timers.target

Various parameters can be used in [Timer] to control the execution period of the timed task.

  • OnActiveSec, how much time to start executing the task after the timer takes effect
  • OnBootSec, how much time to start the task after the system is started
  • OnStartupSec, how long after the Systemd process starts, how long after the task starts
  • OnUnitActiveSec, how much time to wait after the last execution of the unit to execute again
  • OnUnitInactiveSec, how much time since the timer was last closed, executed again
  • OnCalendar, based on absolute time, not relative time execution
  • AccuracySec, if the task must be postponed for various reasons, the maximum number of seconds to postpone, the default is 60 seconds

3. add service

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

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

4. Manage timed tasks

  • Loading configuration

    1
    
    systemctl daemon-reload 
    
  • Start a timed task

    1
    
    systemctl start quick-clear.timer
    
  • View Task Status

    1
    
    systemctl status quick-clear.timer
    
  • Boot up

    1
    
    systemctl enable quick-clear.timer
    
  • View Task 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.
    
  • View All Timed 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