CentOS 定时器支持的脚本类型与写法
一、支持的脚本类型
二、常用定时器与适用场景
三、快速示例
*/1 * * * * /usr/bin/python3 /opt/scripts/hello.py >> /var/log/hello.log 2>&1
[Unit]
Description=Run hello.sh
[Service]
Type=oneshot
ExecStart=/usr/local/bin/hello.sh
[Unit]
Description=Run hello.sh every 30s
[Timer]
OnActiveSec=60
OnUnitActiveSec=30
Unit=hello.service
[Install]
WantedBy=timers.target
systemctl daemon-reload
systemctl enable --now hello.timer
@reboot /usr/bin/python3 /opt/scripts/startup.py >> /var/log/startup.log 2>&1
以上示例分别展示了在 cron 与 systemd.timer 中调用 Python 脚本的常见写法;Shell、PHP 等脚本只需替换解释器与脚本路径即可。
四、权限与环境注意事项