温馨提示×

ubuntu 定时器如何配置

小樊
51
2025-08-27 12:49:15
栏目: 智能运维

Ubuntu配置定时器常用cronsystemd timer两种方式,具体如下:

  • 使用cron配置
    1. 确保cron服务已安装并启动:sudo apt install cronsudo systemctl start cronsudo systemctl enable cron
    2. 编辑当前用户的crontab文件:crontab -e
    3. 按格式添加定时任务,如0 8 * * * /path/to/script.sh(每天8点执行脚本)。
    4. 保存退出,可使用crontab -l查看任务列表。
  • 使用systemd timer配置
    1. 创建服务单元文件,如sudo nano /etc/systemd/system/mytask.service,内容为[Unit] Description=My Task [Service] ExecStart=/path/to/script.sh
    2. 创建定时器单元文件,如sudo nano /etc/systemd/system/mytask.timer,内容为[Unit] Description=Run My Task [Timer] OnCalendar=*-*-* 08:00:00 Persistent=true [Install] WantedBy=timers.target
    3. 重新加载systemd配置:sudo systemctl daemon-reload
    4. 启用并启动定时器:sudo systemctl enable --now mytask.timer

0