在CentOS系统中,你可以使用systemd的定时器(timer)功能来设置重复任务。以下是创建一个重复定时器的步骤:
my-task.service的文件:[Unit]
Description=My custom task
[Service]
Type=oneshot
ExecStart=/path/to/your/script.sh
将/path/to/your/script.sh替换为你想要执行的脚本的路径。
my-task.timer:[Unit]
Description=Run my-task.service every day at 10:00 AM
[Timer]
OnCalendar=*-*-* 10:00:00
Persistent=true
Unit=my-task.service
[Install]
WantedBy=timers.target
在这个例子中,我们设置了定时器每天上午10点执行my-task.service。你可以根据需要修改OnCalendar选项来设置不同的时间。
systemctl命令启用并启动定时器:sudo systemctl enable --now my-task.timer
这将启用定时器并在当前时间立即启动它。
sudo systemctl list-timers --all
这将显示所有已启用的定时器及其状态。
通过以上步骤,你可以在CentOS系统中设置一个重复的定时任务。如果你想要修改定时器的执行频率,只需调整OnCalendar选项即可。更多关于systemd定时器的信息,你可以查阅官方文档:https://www.freedesktop.org/wiki/Software/systemd/Time/