在 CentOS 中,“替换 cron 定时任务”通常有以下几种常见场景,下面分别说明。
crontab -l
crontab -e
示例:
*/5 * * * * /usr/bin/php /var/www/test.php
先准备好一个 cron 文件,例如 mycron.txt:
*/10 * * * * /opt/script.sh
然后执行:
crontab mycron.txt
⚠️ 这会覆盖该用户原来的所有 cron 任务
CentOS 系统 cron 目录:
/etc/cron.d/
/etc/cron.daily/
/etc/cron.hourly/
/etc/cron.weekly/
/etc/cron.monthly/
vi /etc/cron.d/mytask
内容示例:
0 2 * * * root /opt/backup.sh
修改后无需重启 cron(centos 6/7/8 一般自动加载)。
CentOS 7/8 更推荐用 systemd timer 替代 cron。
systemctl list-timers
systemctl stop mytask.timer
systemctl disable mytask.timer
# 修改或重新创建 timer 和 service
systemctl daemon-reload
systemctl enable --now mytask.timer
/etc/cron.d/*,确保权限正确:chmod 644 /etc/cron.d/mytask
tail -f /var/log/cron
如果你能具体说明:
我可以给你更精确的操作命令。