自定义Nginx Docker镜像中Cron服务正常运行但定时任务不执行的问题求助
自定义Nginx Docker镜像中Cron服务正常运行但定时任务不执行的问题求助
各位好,我在构建自定义Nginx Docker镜像时碰到了一个棘手的问题:Cron服务明明显示处于运行状态,但配置的定时任务完全没执行——不管是logrotate的日志轮转任务,还是用来测试的输出任务,都没有任何反应。
先给大家贴一下我的Dockerfile内容:
FROM nginx:latest # Remove sym links from nginx image RUN rm /var/log/nginx/access.log RUN rm /var/log/nginx/error.log RUN apt-get update && apt-get install -y logrotate && rm -rf /var/lib/apt/lists/* COPY nginx.conf /etc/nginx/nginx.conf # COPY conf.d /etc/nginx/conf.d # COPY cert/ /etc/nginx/certs COPY data /data COPY --chown=root:root nginx /etc/logrotate.d/ RUN echo -e '#!/bin/sh\nlogrotate -f /etc/logrotate.d/nginx\n' > /etc/cron.daily/logrotate-nginx && chmod +x /etc/cron.daily/logrotate-nginx EXPOSE 80 443 CMD service cron start && nginx -g 'daemon off;'
下面是容器内的相关状态:
- 确认Cron服务在运行:
root@f11ddfdb471a:/# service cron status cron is running.
- 我的crontab配置内容:
*/1 * * * * root /usr/sbin/logrotate /etc/logrotate.d/nginx * * * * * echo "Hello world!" >> /var/log/cron.log 2>&1
- 但测试用的日志文件是空的,任务完全没触发:
root@f11ddfdb471a:/# cat /var/log/cron.log root@f11ddfdb471a:/# cat /var/log/cron.log
不止是测试任务,logrotate的日志轮转任务也完全没动静。
我已经尝试过的操作:
- 手动启动Cron服务,并且多次重启Nginx容器
- 手动执行定时任务对应的命令,都能正常工作
后来找到了问题的根源,感谢@AlexeyTen的提示:
- 第二个测试定时任务(echo输出的那个)缺少指定执行用户
root - 第一个logrotate任务对应的脚本权限是664,而正确的权限应该设置为644
备注:内容来源于stack exchange,提问作者Rajsekar Reddy
相关产品推荐
相关产品推荐

