阅读量:4
在Ubuntu中,可使用cron或systemd设置任务执行时间,具体如下:
- 使用Cron:
- 打开终端,输入
crontab -e编辑当前用户的crontab文件。 - 按格式
分 时 日 月 星期 命令添加定时任务,例如0 6 * * * /path/to/script.sh表示每天早上6点执行脚本。 - 保存并退出编辑器,cron会自动加载新配置。
- 打开终端,输入
- 使用Systemd:
- 创建服务文件,如
/etc/systemd/system/mytask.service,内容为[Unit] Description=My Task [Service] ExecStart=/path/to/script.sh。 - 创建定时器文件,如
/etc/systemd/system/mytask.timer,内容为[Unit] Description=Run My Task [Timer] OnCalendar=*-*-* 06:00:00 Persistent=true [Install] WantedBy=timers.target,其中OnCalendar指定执行时间。 - 启用并启动定时器:
sudo systemctl enable --now mytask.timer。
- 创建服务文件,如
以上就是关于“ubuntu定时器如何设置任务执行时间”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm