一、创建执行服务脚本
示例如下,三个文件:
- backup.service
- backup.timer
- run_backup.sh
文件内容分别为
backup.service
[Unit]
Description=leanote backup.
After=network.target
[Service]
User=changhonghe
WorkingDirectory=/home/changhonghe/github/leanoteBackup # 此处替换运行目录
ExecStart=/usr/bin/bash /home/changhonghe/github/leanoteBackup/run_backup.sh >> git.log # 此处编写脚本运行路径,建议绝对路径
[Install]
WantedBy=multi-user.target
backup.timer
[Unit]
Description=Run Leanote_backup.
[Timer]
OnBootSec=0s
OnUnitActiveSec=30min
Unit=leanoteBackup.service # 运行的service服务
run_backup.sh
# 这个就不多说了,你的shell执行脚本
脚本生效
sudo cp ddns.timer /usr/lib/systemd/system/
sudo cp ddns.service /usr/lib/systemd/system/
sudo systemctl daemon-reload #加载服务
sudo systemctl start leanoteBackup.service
sudo systemctl start leanoteBackup.timer