CentOS中常用的定时器命令是systemd-timer
,它是基于systemd
的定时器单元,用于替代传统的cron
服务。以下是一些与systemd-timer
相关的命令:
创建定时器单元文件
sudo systemctl edit --full mytimer.timer
这将在/etc/systemd/system/
目录下创建一个名为mytimer.timer
的文件。
编辑定时器单元文件 可以直接编辑该文件来设置定时器的触发时间和行为。
[Unit]
Description=Run myservice.service every hour
[Timer]
OnCalendar=*-*-* *:00:00
Persistent=true
[Install]
WantedBy=timers.target
重新加载systemd配置
sudo systemctl daemon-reload
启动定时器
sudo systemctl start mytimer.timer
停止定时器
sudo systemctl stop mytimer.timer
启用定时器开机自启
sudo systemctl enable mytimer.timer
禁用定时器开机自启
sudo systemctl disable mytimer.timer
查看定时器状态
sudo systemctl status mytimer.timer
查看所有定时器
systemctl list-timers --all
at
命令虽然systemd-timer
是更现代的选择,但at
命令仍然在一些CentOS版本中使用。
安装at
服务
sudo yum install at
启动并启用at
服务
sudo systemctl start atd
sudo systemctl enable atd
提交任务到at
队列
echo "your-command-here" | at now + 1 hour
查看待处理任务
atq
删除待处理任务
atrm <job-id>
systemd-timer
提供了比cron
更强大的功能和更好的集成。/etc/systemd/system/
或/lib/systemd/system/
目录下。systemd
。通过这些命令,你可以有效地管理和调度CentOS系统中的定时任务。