ubuntu Docker 环境下设置crontab

设置crontab 第一个问题就是时区不对的问题,默认系统时区为UTC,时间不同步,设置定时任务时会有偏差。因此,先更改时区

解决办法:在Dockerfile中加入即可

RUN echo”Asia/Shanghai” > /etc/timezone 
RUN dpkg-reconfigure -f noninteractive tzdata

容器启动之后,设置了crontab,结果一直不生效,后发现crontab 服务没有启动,于是网上各种搜索,发现了如下命令

ubuntu下定时执行工具cron开启关闭重启
配置文件一般为/etc/init.d/cron

启动:sudo /etc/init.d/cron start
关闭:sudo /etc/init.d/cron stop
重启:sudo /etc/init.d/cron restart
重新载入配置:sudo /etc/init.d/cron reload

运行遇到了问题

Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service cron start

Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the start(8) utility, e.g. start cron

尝试了提到的命令start cron /status cron,均报错。后搜索发现启动其实非常简单直接执行cron即可!

标签:CrontabUbuntuDocker 发布于:2019-10-23 23:21:17