I'm using PM2 to run a Python program in the background like so
pm2 start helloworld.py
and it works perfectly fine. However, within helloworld.py
I have several print statements that act as logs. For example, when a network request comes in or if a database value is updated. When I run helloworld.py
like so:
python3 helloworld.py
all these print statements are visible and I can debug my application. However, when running
pm2 logs helloworld
none of these print statements show up.
This question is a few months old, so maybe you figured this out a while ago, but it was one of the top google hits when I was having the same problem so I thought I'd add what I found.
Seems like it's an issue with how python buffers sys.stdout. In some platforms/instances, when called by say pm2 or nohup, the sys.stdout stream may not get flushed until the process exits. Passing the "-u" argument to the python interpreter stops it from buffering sys.stdout. In the process.json for pm2 I added "interpreter_args": "-u" and I'm getting logs normally now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With