Search code examples
node.jsdockerloggingcronsupervisord

View NodeJS Application Logs Via Cron Tasks on Supervisor on Docker


So the setup is...

Docker > Supervisor > Cron > NodeJS Task.

I am currently getting my supervisor to output it's log and error log to docker with the current supervisor setup.

[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

This shows the cron tasks running, ie

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)

However it doesn't show anything from my NodeJS application.

If i run my NodeJS application from the terminal with nodejs index.js i get console.log outputs as i would expect.

I know how to get my NodeJS output if i was running it from supervisor (it would be the same as cron setup above) however as im triggering it from cron it is different.

Now i suspect where I am going wrong is where i redirect my logs to with in my crontab...?

My crontab currently looks like the following (Running simple ubuntu:14.04 docker image)...

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

This fire the app perfectly.

I can also view the logs by jumping into the container and running cat /var/log/supervisor/supervisord.log. It shows both the output of my app and also the cron output that i pasted above. However it doesn't appear on docker-compose logs -f.

Help much appreciated!


Solution

  • So, this may not be the best way to do it, but it works for me so hey!

    Firstly in my Dockerfile i generated a blank log as so...

    RUN touch /var/log/cron.log
    

    Then I amended my crontab file to output to here...

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

    Then I added a section to my supervisor config to tail the logs...

    [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
    

    Works a treat, the reason for the touch is so that it doesn't error that the file doesn't exist, as no log file exists until cron tries to write to it.

    Looking forward if there is issues with this solution or if someone has a better one...