在 CentOS 系统中,你可以使用 systemd 定时器来实现延迟执行任务
/etc/systemd/system/my-service.service。在这个文件中,定义你想要执行的任务。例如:[Unit]
Description=My custom service
[Service]
Type=oneshot
ExecStart=/usr/bin/echo "Hello, World!"
这个服务将在启动时执行一次 echo "Hello, World!" 命令。
/etc/systemd/system/my-service.timer。在这个文件中,定义定时器的配置。例如:[Unit]
Description=Run my-service.service after 10 minutes
[Timer]
OnBootSec=10min
Unit=my-service.service
[Install]
WantedBy=timers.target
这个定时器将在系统启动后的 10 分钟执行 my-service.service。
sudo systemctl enable --now my-service.timer
sudo systemctl list-timers --all
这将显示所有已配置的定时器及其状态。你应该能看到 my-service.timer 及其预计的下次触发时间。
sudo systemctl stop my-service.timer
sudo systemctl disable my-service.timer
这将停止定时器并禁用它,使其不再自动启动。