- 测试脚本
- 设置自己的service
- 设置自己的timer
- 验证timer
前提:
想要使用 systemd 的timer 功能,你必须要有几个要件:
- 系统的timer.target 一定要启动
- 要有个sname.service 的服务存在(sname 是你自己指定的名称)
- 要有个sname.timer 的时间启动服务存在
测试脚本
remove-log.sh
#!/bin/bash
date >> /home/xx/remove-log
echo "run remove " >> /home/xx/remove-log
service文件:remove-log.service --> /etc/systemd/system/
[Unit]
Description= Remove gateway log service
[Service]
ExecStart=/home/xx/remove-log.sh
Type=oneshot
[Install]
WantedBy=multi-user.target
timer文件:remove-log.timer --> /etc/systemd/system/
[Unit]
Description=remove gateway log timer
[Timer]
#start remove-log.timer after bootup 1min, then run remove-log.service every 30s
OnBootSec=1min
OnUnitActiveSec=30s
#every 2:00 on sunday
#OnCalendar=Sun *-*-* 02:00:00
#Persistent=true
#Unit=remove-log.service
[Install]
WantedBy=multi-user.target
验证timer:
systemctl daemon-reload
systemctl enable remove-log.timer
systemctl start remove-log.timer