我有一个 Tornado 程序,并且所有相关的 python 库都安装在名为 bob 的非 root 用户中:
pip install --user xxx
现在我想在主管中运行它:
[program:testpro]
command=python /path/to/myfile.py
user=bob ; set the user to bob
redirect_stderr=true
stdout_logfile=/path/to/log
numproces=1
autostart=true
但是失败了,处于supervisorctl状态:
testpro FATAL Exited too quickly (process log may have details)
我查看日志,发现它无法导入相对的 python 库,因此它不能以 bob 身份运行。
然后我更改为:
[program:testpro]
command=sudo -u bob -i python /path/to/myfile.py
;user=bob ;comment this
redirect_stderr=true
stdout_logfile=/path/to/log
numproces=1
autostart=true
运行正常
那么,选项user
有什么作用呢?以及如何在选项中配置运行用户?
最佳答案
我已经解决了这个问题:
在程序部分添加环境
选项:
environment=HOME="/home/bob",USER="bob"
参见Supervisor doc - Subprocess Environment :
No shell is executed by supervisord when it runs a subprocess, so environment variables such as USER, PATH, HOME, SHELL, LOGNAME, etc. are not changed from their defaults or otherwise reassigned. This is particularly important to note when you are running a program from a supervisord run as root with a user= stanza in the configuration. Unlike cron, supervisord does not attempt to divine and override “fundamental” environment variables like USER, PATH, HOME, and LOGNAME when it performs a setuid to the user defined within the user= program config option. If you need to set environment variables for a particular program that might otherwise be set by a shell invocation for a particular user, you must do it explicitly within the environment= program config option. An example of setting these enviroment variables is as below.
[program:apache2]
command=/home/chrism/bin/httpd -c "ErrorLog
/dev/stdout" -DFOREGROUND user=chrism
environment=HOME="/home/chrism",USER="chrism"
关于tornado - 程序部分中的主管配置用户选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27291448/