在没有 root
权限的情况下,仍可以安装和使用 Python 守护进程管理工具(如 supervisor
):
- 使用用户空间安装:你可以通过
pip install --user
将supervisor
安装到当前用户的家目录下,而不需要root
权限。 - 配置文件路径:
supervisor
的配置文件、日志文件和进程管理文件需要放在当前用户有权限的目录中。 - 启动方式:由于没有
root
权限,supervisor
不能以系统服务的方式运行,但可以通过当前用户直接启动。
以下是具体步骤:
1. 安装 supervisor
使用 pip
安装到当前用户的家目录:
pip install --user supervisor
安装完成后,supervisor
的可执行文件会位于 ~/.local/bin
目录下。确保将该目录添加到 PATH
环境变量中:
export PATH=$HOME/.local/bin:$PATH
2. 创建 supervisor
配置文件
在用户目录下创建一个 supervisor
配置文件目录:
mkdir -p ~/supervisor/conf.d
生成默认配置文件:
echo_supervisord_conf > ~/supervisor/supervisord.conf
编辑配置文件 ~/supervisor/supervisord.conf
,添加你的 Python 程序配置。在文件末尾添加以下内容:
[include]
files = /home/your_username/supervisor/conf.d/*.conf
然后在 ~/supervisor/conf.d/
目录下创建一个新的配置文件,例如 image_webserver.conf
:
touch ~/supervisor/conf.d/image_webserver.conf
编辑 image_webserver.conf
,添加以下内容:
[program:image_webserver]
command=python /data/service/image_webserver.py
directory=/data/service
autostart=true
autorestart=true
stderr_logfile=/home/your_username/supervisor/logs/image_webserver_err.log
stdout_logfile=/home/your_username/supervisor/logs/image_webserver_out.log
user=your_username
environment=HOME="/home/your_username",USER="your_username",PATH="/home/your_username/.local/bin:%(ENV_PATH)s"
说明:
command
:启动你的 Python 脚本。directory
:设置工作目录。autostart
和autorestart
:确保程序自动启动和重启。stderr_logfile
和stdout_logfile
:日志文件路径。user
:运行程序的用户。environment
:设置环境变量,确保conda
和python
可用。
3. 启动 supervisor
使用以下命令启动 supervisor
:
supervisord -c ~/supervisor/supervisord.conf
这会以后台进程的方式启动 supervisor
,并加载你的配置文件。
4. 管理进程
使用 supervisorctl
管理你的进程:
supervisorctl -c ~/supervisor/supervisord.conf
在 supervisorctl
交互界面中,你可以使用以下命令:
status
:查看进程状态。start image_webserver
:启动你的程序。stop image_webserver
:停止你的程序。restart image_webserver
:重启你的程序。reload
:重新加载配置文件。exit
:退出supervisorctl
。
5. 停止 supervisor
如果需要停止 supervisor
,可以使用以下命令:
supervisorctl -c ~/supervisor/supervisord.conf shutdown
6. 日志文件
日志文件会保存在 ~/supervisor/logs/
目录下,你可以随时查看日志以排查问题。
7. 开机自启动(可选)
如果你希望 supervisor
在用户登录时自动启动,可以将以下命令添加到你的 ~/.bashrc
或 ~/.profile
文件中:
if [ -z "$(ps aux | grep supervisord | grep -v grep)" ]; then
supervisord -c ~/supervisor/supervisord.conf
fi
FAQ
1. refused connection 无法连接到服务
supervisorctl status
# 错误提示:
http://localhost:9001 refused connection
http://localhost:9001 refused connection
错误表示 supervisorctl
无法连接到 supervisord
的服务端。这通常是因为 supervisord
没有正确启动,或者配置文件中指定的端口和权限有问题。
- 检查
supervisord
是否正在运行
首先,检查 supervisord
是否已经启动:
ps aux | grep supervisord
如果没有任何输出,说明 supervisord
没有启动。你需要手动启动它:
supervisord -c ~/supervisor/supervisord.conf
如果 supervisord
已经启动,但仍然无法连接,可能是配置文件中的端口或权限设置有问题。
- 检查配置文件中的
[inet_http_server]
配置
supervisorctl
默认通过 HTTP 连接到 supervisord
,因此需要在配置文件中启用 [inet_http_server]
部分。
打开你的 supervisord.conf
文件:
nano ~/supervisor/supervisord.conf
确保以下内容存在并正确配置:
[inet_http_server]
port = 127.0.0.1:9001
username = your_username ; 可选,设置用户名
password = your_password ; 可选,设置密码
说明:
port
:指定supervisord
监听的地址和端口。127.0.0.1:9001
表示只允许本地连接。username
和password
:可选,用于身份验证。
保存文件后,重启 supervisord
:
supervisorctl -c ~/supervisor/supervisord.conf shutdown
supervisord -c ~/supervisor/supervisord.conf
- 检查端口是否被占用
如果端口 9001
被其他进程占用,supervisord
将无法启动。你可以检查端口占用情况:
netstat -tuln | grep 9001
如果端口被占用,可以修改 supervisord.conf
中的 port
配置,换一个未被占用的端口。
2. 查看应用日志
使用 nohup 直接运行脚本时,日志会输出到当前目录的 nohup.out 文件中。但现在你使用 supervisor 管理进程,日志的输出位置由 supervisor 的配置文件决定。
- 查看
supervisor
配置文件中的日志路径
在 supervisor
的配置文件中,每个 [program]
块都会指定 stdout_logfile
和 stderr_logfile
,这些是日志文件的输出路径。
例如,在你的 image_webserver.conf
中:
[program:image_webserver]
command=python /data/service/image_webserver.py
directory=/data/service
autostart=true
autorestart=true
stderr_logfile=/home/your_username/supervisor/logs/image_webserver_err.log
stdout_logfile=/home/your_username/supervisor/logs/image_webserver_out.log
user=your_username
environment=HOME="/home/your_username",USER="your_username",PATH="/home/your_username/.local/bin:%(ENV_PATH)s"
stdout_logfile
:标准输出日志路径(例如:/home/your_username/supervisor/logs/image_webserver_out.log
)。stderr_logfile
:标准错误日志路径(例如:/home/your_username/supervisor/logs/image_webserver_err.log
)。
你可以通过以下命令查看日志:
cat /home/your_username/supervisor/logs/image_webserver_out.log
cat /home/your_username/supervisor/logs/image_webserver_err.log
2. 如果日志路径未配置
如果 stdout_logfile
和 stderr_logfile
没有配置,supervisor
会将日志输出到默认路径。默认情况下,日志会输出到 supervisord
的日志目录中,通常是 ~/supervisor/logs/
。
你可以检查以下文件:
ls ~/supervisor/logs/
3. 查看 supervisord
的日志
如果仍然找不到日志,可以查看 supervisord
的主日志文件,通常位于 ~/supervisor/logs/supervisord.log
:
cat ~/supervisor/logs/supervisord.log
这里可能会记录一些关于进程启动失败或日志输出问题的信息。
4. 修改日志路径
如果你希望将日志输出到特定路径(例如当前目录的 nohup.out
),可以修改 supervisor
的配置文件:
[program:image_webserver]
command=python /data/service/image_webserver.py
directory=/data/service
autostart=true
autorestart=true
stderr_logfile=/data/service/nohup.out
stdout_logfile=/data/service/nohup.out
user=your_username
environment=HOME="/home/your_username",USER="your_username",PATH="/home/your_username/.local/bin:%(ENV_PATH)s"
然后重启 supervisord
:
supervisorctl -c ~/supervisor/supervisord.conf reload
5. 临时查看日志
如果你只是想临时查看日志,可以使用 supervisorctl
的 tail
命令:
supervisorctl -c ~/supervisor/supervisord.conf tail image_webserver stdout
supervisorctl -c ~/supervisor/supervisord.conf tail image_webserver stderr
这会实时输出标准输出和标准错误的日志内容。