gpt4 book ai didi

node.js - 通过 Docker 上 Supervisor 的 Cron 任务查看 Node JS 应用日志

转载 作者:太空宇宙 更新时间:2023-11-03 22:12:11 25 4
gpt4 key购买 nike

所以设置是...

Docker > Supervisor > Cron > NodeJS 任务。

我目前正在让我的主管使用当前的主管设置将其日志和错误日志输出到 docker。

[program:cron]
command=cron -f
startsecs=10
priority=100
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

这显示了正在运行的 cron 任务,即

app_1  |   'Supervisord is running as root and it is searching '
app_1 | 2016-08-26 12:54:30,519 CRIT Supervisor running as root (no user in config file)
app_1 | 2016-08-26 12:54:30,519 WARN Included extra file "/etc/supervisor/conf.d/supervisord.conf" during parsing
app_1 | 2016-08-26 12:54:30,536 INFO RPC interface 'supervisor' initialized
app_1 | 2016-08-26 12:54:30,536 CRIT Server 'unix_http_server' running without any HTTP authentication checking
app_1 | 2016-08-26 12:54:30,537 INFO supervisord started with pid 8
app_1 | 2016-08-26 12:54:31,542 INFO spawned: 'cron' with pid 11
app_1 | 2016-08-26 12:54:41,584 INFO success: cron entered RUNNING state, process has stayed up for > than 10 seconds (startsecs)

但是它没有显示我的 NodeJS 应用程序中的任何内容。

如果我使用 nodejs index.js 从终端运行我的 NodeJS 应用程序,我会得到 console.log 输出,正如我所期望的。

如果我从主管运行它,我知道如何获取 NodeJS 输出(它将与上面的 cron 设置相同),但是当我从 cron 触发它时,它是不同的。

现在我怀疑我出错的地方是我将日志重定向到 crontab 中的位置...?

我的 crontab 目前如下所示(运行简单的 ubuntu:14.04 docker 镜像)...

* * * * * cd /home/app && nodejs /home/app/index.js >> /var/log/supervisor/supervisord.log 2>&1

这完美地启动了应用程序。

我还可以通过跳入容器并运行 cat/var/log/supervisor/supervisord.log 来查看日志。它显示了我的应用程序的输出以及我上面粘贴的 cron 输出。但是它不会出现在docker-compose logs -f上。

非常感谢您的帮助!

最佳答案

所以,这可能不是最好的方法,但它对我有用,所以嘿!

首先,在我的 Dockerfile 中,我生成了一个空白日志,如下所示...

RUN touch /var/log/cron.log

然后我修改了我的 crontab 文件以输出到这里...

* * * * * cd /home/app && nodejs /home/app/index.js >> /var/log/cron.log 2>&1

然后我在我的主管配置中添加了一个部分来跟踪日志......

[program:cronlogs]
command=tail -f /var/log/cron.log
startsecs=20
priority=200
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr

很好用,touch的原因是这样它就不会错误地认为文件不存在,因为在 cron 尝试写入日志文件之前不存在日志文件。

期待此解决方案是否存在问题或者是否有人有更好的解决方案......

关于node.js - 通过 Docker 上 Supervisor 的 Cron 任务查看 Node JS 应用日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39165860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com