问题
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.
回答1:
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.
回答2:
Check the folder #HOME/.pm2/logs
See for example the folder structure section here: http://pm2.keymetrics.io/docs/usage/quick-start/
Also consider using a configuration file with an explicit logs folder that is relative to your scripts. (Note this folder must exist before pm2 can use it.) See http://pm2.keymetrics.io/docs/usage/application-declaration/
{
"apps": [
{
"script": "app/server.js",
"log_date_format": "YYYY-MM-DD HH:mm Z",
"error_file": "logs/server.web.error.log",
"out_file": "logs/server.web.out.log",
...
Nice way to follow these log files is to run tail
tail -f logs/*.log
UPDATE: To be clear, using a configuration file works for python scripts. Just create a json configuration file that specifies your script and where you want the output to go. For example
{
"apps": [
{
"name": "Test Python",
"script": "test.py",
"out_file": "test.out.log",
}
]
}
Then run it pm2 start test.json Look for the process id in the results. Use this process ID to stop your process and to see where the log file is. E.g. pm2 show 3
来源:https://stackoverflow.com/questions/37959217/pm2-doesnt-log-python3-print-statements