You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过systemd启动waitress-serve服务失败的问题求助

项目环境

我有一个基于Flask的Web服务,可通过命令行启动。项目环境结构如下:

BASEDIR/
    startMyService.sh
    |-venv/
        |-/bin
        |...
    |-/WebService...
    |-Flask相关目录

我通过以下方式创建并使用虚拟环境运行服务($BASEDIR为项目所在路径):

sudo python -m venv ./venv
source $BASEDIR/venv/bin/activate
waitress-serve --port=8080 --call 'WebService:create_app'

注:执行上述命令时,命令行前缀出现(venv),表示虚拟环境已激活。

Web服务启动脚本

我将上述命令封装到shell脚本startMyService.sh中,代码如下($BASEDIR为脚本所在目录):

#!/bin/bash

# 激活与脚本同目录的虚拟环境
python -m venv $BASEDIR/venv
source $BASEDIR/venv/bin/activate
pip install waitress
pip install flask
pip install ... 其他依赖包

# 重新安装以wheel包提供的Web应用
$BASEDIR/venv/bin/pip3 uninstall myapp --yes
$BASEDIR/venv/bin/pip3  install $BASEDIR/dist/myapp-0.1.0-py3-none-any.whl --force-reinstall

# 查找并终止已在运行的服务实例
PROCESSID=`ps aux | grep \"WebService:create_app\" | grep -v \"grep\" | awk '{print $2}'`

if [[ $PROCESSID != \"\" ]]
then
    echo \"killing existing instances of WebService with PID $PROCESSID\"
    kill -9 $PROCESSID
fi

# 后台启动Web服务器
echo \"Starting webService...\"
waitress-serve --port=8080 --call 'WebService:create_app'

Systemd服务异常问题

我尝试将该脚本配置为systemd服务,当前的.service文件内容如下:

[Unit]
Description=My web application
Documentation=some url

[Service]
PIDFile=/run/pidfile.pid
Type=simple
ExecStart=theApplicationDirectory/startMyService.sh
ExecStop=/bin/kill -9 $MAINPID

[Install]
WantedBy=default.target

当我执行sudo systemctl start myservice.service启动服务时,服务最终处于“dead/inactive”状态。通过journalctl查看日志得到以下提示:

...
Starting status webpage in testing mode
May 06 11:33:00 servername startMyService.sh[726211]: Error: Bad module 'WebViewer'
May 06 11:33:00 servername startMyService.sh[726211]: Usage:
May 06 11:33:00 servername startMyService.sh[726211]:     waitress-serve [OPTS] MODULE:OBJECT

我推测问题在于shell脚本中的虚拟环境未成功激活,服务使用了系统默认Python而非虚拟环境中的Python。

内容的提问来源于stack exchange,提问作者WolfiG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 09:51:18