使用supervisorctl启动脚本失败问题排查求助
问题排查:Supervisor管理FastAPI服务无法通过IP访问
配置文件内容
start.sh 启动脚本
#!/bin/bash cd api uvicorn app.main:app --host=0.0.0.0 --workers=2
Supervisor配置文件
[program:uvicorn] socket=tcp://localhost:8000 command=/home/ubuntu/start.sh numprocs=2 process_name=fastapi-uvicorn-%(process_num)d stdout_logfile=/var/log/supervisor/fastapi_access.log stderr_logfile=/var/log/supervisor/fastapi_error.log autostart=true autorestart=true stopasgroup=true stopsignal=QUIT
初始错误日志
/home/ubuntu/start.sh: line 2: cd: api: No such file or directory /home/ubuntu/start.sh: line 4: uvicorn: command not found /home/ubuntu/start.sh: line 2: cd: api: No such file or directory /home/ubuntu/start.sh: line 4: uvicorn: command not found /home/ubuntu/start.sh: line 2: cd: api: No such file or directory /home/ubuntu/start.sh: line 4: uvicorn: command not found
已尝试操作与当前状态
/home/ubuntu/目录下仅有api目录与start.sh脚本- 已全局安装所有依赖,尝试过激活虚拟环境,无效
- 更新1:添加
directory=/home/ubuntu/后解决目录问题,但uvicorn命令找不到 - 更新2:添加
uvicorn完整路径后服务启动成功,但无法通过IP访问API,手动运行start.sh则正常
排查解决方案
1. 移除Supervisor配置中的socket参数
当前配置里的socket=tcp://localhost:8000会强制服务绑定到本地回环地址,与启动脚本中--host=0.0.0.0的配置冲突。Supervisor的socket参数是用于内部进程通信的,并非服务对外监听地址,删除该参数即可让启动脚本的监听配置生效。
修改后的Supervisor配置片段:
[program:uvicorn] command=/home/ubuntu/start.sh numprocs=2 # 保留其他原有配置...
2. 重启Supervisor服务使配置生效
执行以下命令刷新配置并重启服务:
supervisorctl reread supervisorctl update supervisorctl restart uvicorn
3. 验证端口监听状态
服务重启后,检查8000端口的监听地址:
netstat -tulpn | grep 8000
输出应显示0.0.0.0:8000,表示服务已绑定所有网卡。
4. 确认防火墙规则
检查服务器防火墙是否开放8000端口:
ufw status
若端口未开放,执行开放命令:
ufw allow 8000/tcp
5. 优化启动脚本的路径可靠性
如果使用虚拟环境,建议在启动脚本中直接指定虚拟环境内uvicorn的完整路径,避免环境变量问题:
#!/bin/bash cd /home/ubuntu/api # 替换为你的虚拟环境路径 /home/ubuntu/venv/bin/uvicorn app.main:app --host=0.0.0.0 --workers=2
内容的提问来源于stack exchange,提问作者Rodrigo Petrizzo
相关产品推荐
相关产品推荐

