在 Debian 系統中配置定時任務可以通過 cron 或者 Systemd Timer 來實現。以下是詳細的配置步驟:
cron 配置定時任務安裝 cron(如果尚未安裝):
sudo apt-get update
sudo apt-get install cron -y
編輯 crontab 文件:
crontab -e
添加定時任務:
在打開的編輯器中,添加定時任務的配置。每行都包含要執行的命令、參數與選項,以及要執行任務的時間。例如,每分鐘運行一次 test.sh 腳本:
*/1 * * * * /path/to/your/script/test.sh
保存并退出編輯器。
確保腳本具有可執行權限:
chmod +x /path/to/your/script/test.sh
啟動 cron 服務:
sudo /etc/init.d/cron start
設置 cron 服務開機自啟:
sudo /etc/init.d/cron enable
Systemd Timer 配置定時任務創建服務單元文件:
創建一個服務單元文件,例如 demo.service,并放到 /etc/systemd/system/ 目錄下:
[Unit]
Description=My Cron Service
[Service]
ExecStart=/bin/bash /root/hello.sh
創建定時器單元文件:
創建一個定時器單元文件,例如 ntpsync.timer,并放到 /etc/systemd/system/ 目錄下:
[Unit]
Description=NTP Time Synchronization Timer
[Timer]
OnCalendar=*-*-* *:00:00
Persistent=true
[Install]
WantedBy=multi-user.target
啟用并啟動定時器:
sudo systemctl enable ntpsync.timer
sudo systemctl start ntpsync.timer
查看定時器狀態:
systemctl status ntpsync.timer
通過以上步驟,你可以在 Debian 系統中配置定時任務。根據具體需求選擇使用 cron 或 Systemd Timer。