登录
首页 >  科技周边 >  人工智能

Linux部署大模型,后台运行脚本分享

时间:2026-05-22 19:30:34 232浏览 收藏

本文详解了在Linux服务器上将大模型(如Qwen2.5-7B、MiniCPM-o-4.5等)从易中断的前台运行升级为稳定可靠的后台常驻服务的三大实战方案:轻量灵活的nohup日志守护、生产级的systemd开机自启与崩溃恢复、以及支持GPU加速与自动重启的Docker容器化部署,覆盖从快速验证到高可用上线的全场景需求,助你彻底告别SSH断连即服务宕机的困扰,真正实现大模型服务“一次部署、长期在线”。

Linux服务器部署大模型_后台常驻运行脚本分享

如果您已在Linux服务器上完成大模型(如Qwen2.5-7B、MiniCPM-o-4.5、Cosmos-Reason1-7B等)的本地部署,但每次关闭SSH终端服务即中断,则说明当前仍处于前台交互式运行模式。以下是多种可靠、可复用的后台常驻运行脚本方案,覆盖轻量级到生产级场景。

一、nohup + 重定向启动脚本

该方式无需额外安装组件,适用于快速验证或临时长期运行,通过忽略挂断信号并持久化日志实现基础后台化。

1、创建启动脚本文件:touch run_model_nohup.sh

2、赋予执行权限:chmod +x run_model_nohup.sh

3、写入以下内容(以Qwen2.5-7B-Instruct为例,端口设为8000):
#!/bin/bash
cd /root/qwen2.5-7b-instruct
source qwen_env/bin/activate
nohup python qwen_service.py --port 8000 > /var/log/qwen_service.log 2>&1 &

4、执行脚本:./run_model_nohup.sh

5、验证进程是否存活:ps aux | grep qwen_service.py

二、systemd服务单元脚本

systemd是Linux标准服务管理器,支持开机自启、崩溃自动重启、资源限制与集中日志,适合生产环境长期部署。

1、创建服务定义文件:sudo nano /etc/systemd/system/qwen-model.service

2、填入标准化配置(路径、用户、环境依实际调整):
[Unit]
Description=Qwen2.5-7B-Instruct API Service
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/root/qwen2.5-7b-instruct
Environment="PATH=/root/qwen_env/bin:/usr/local/bin:/usr/bin:/bin"
ExecStart=/root/qwen_env/bin/python qwen_service.py --port 8000
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
MemoryLimit=24G

[Install]
WantedBy=multi-user.target

3、重载配置并启用服务:sudo systemctl daemon-reload && sudo systemctl enable --now qwen-model.service

4、查看实时日志:sudo journalctl -u qwen-model.service -f

三、docker容器化后台脚本(适用已容器化模型)

对已封装为Docker镜像的大模型(如Qwen-Image-2512、Hunyuan-MT-7B-vllm),使用docker run -d配合健康检查与自动重启策略,实现零依赖后台托管。

1、编写可复用的启动脚本:touch start_model_docker.sh

2、填入以下内容(示例映射至宿主机8080端口,启用GPU,自动重启):
#!/bin/bash
docker run -d \
--gpus all \
--restart unless-stopped \
--name qwen-image-prod \--volume /data/qwen-output:/app/output \
registry.cn-hangzhou.aliyuncs.com/csdn_ai/qwen-image-2512:latest

3、执行并验证:chmod +x start_model_docker.sh && ./start_model_docker.sh && docker ps | grep qwen-image-prod

四、setsid脱离会话脚本(高隔离性场景)

setsid将进程直接置于init(PID 1)之下,彻底切断与终端会话的关联,避免任何SIGHUP或shell退出影响,适用于安全敏感或嵌套终端环境。

1、新建独立守护脚本:touch run_with_setsid.sh

2、写入命令(以Leather Dress Collection为例):
#!/bin/bash
cd /root/Leather-Dress-Collection
source leather-env/bin/activate
setsid python app.py --server-port 7861 > /var/log/leather-app.log 2>&1

3、后台执行且不阻塞当前终端:./run_with_setsid.sh &

4、确认父进程为1:ps -o pid,ppid,comm -C python | grep app.py

以上就是《Linux部署大模型,后台运行脚本分享》的详细内容,更多关于的资料请关注golang学习网公众号!

资料下载
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>