—
參考資源
—
流程
- 建立一個服務 .service
- 建立一個計時器 .timer ,執行剛剛建立的服務
- 啟用、開始服務及計時器
—
建立服務
範例服務的功能很簡單,就是將目前時間附加寫入 cron.log 內
/etc/systemd/system/test.service
1 2 3 4 5 |
[Unit] Description=Timer Service [Service] ExecStart=sh -c "echo `date` >> /docker/cron.log" |
啟用、開始服務
1 2 |
# systemctl enable test.service # systemctl start test.service |
—
建立計時器
/etc/systemd/system/test.timer
1 2 3 4 5 6 7 8 9 10 |
[Unit] Description=Run Timer Service [Timer] OnCalendar=*:*:0/5 AccuracySec=100ms Unit=test.service [Install] WantedBy=multi-user.target |
- 0/xx 除多少就代表幾秒跑一次
啟動、開始計時器
1 2 |
# systemctl enable test.timer # systemctl start test.timer |
後續如果有修改,需要重新讀取
1 |
# systemctl daemon-reload |
—
排程執行
1 |
OnCalendar=* *-*-* *:*:* |
- * 星期幾
Wed
Mon,Sun
Mon..Thu,Sat,Sun - *-*-* 西元年月日
*-1-1 - *:*:* 時分秒
00:00:00
—
不能每秒執行
因為使用 Docker 的 PHP 來執行,一開始以為是容器執行效能問題,結果看了 Log 才發現是系統本身的問題。
每 2 秒執行一次是正常的
1 2 3 |
Jul 07 01:48:01 hoyo systemd[1]: 0.service: Start request repeated too quickly. Jul 07 01:48:01 hoyo systemd[1]: 0.service: Failed with result 'start-limit-hit'. Jul 07 01:48:01 hoyo systemd[1]: Failed to start Hello World Service. |
—
1 total views, 1 views today