You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义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的提示:

  1. 第二个测试定时任务(echo输出的那个)缺少指定执行用户root
  2. 第一个logrotate任务对应的脚本权限是664,而正确的权限应该设置为644

备注:内容来源于stack exchange,提问作者Rajsekar Reddy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 14:59:36