在 systemd 服务文件中运行 Apache Superset 不起作用。错误:找不到 Flask 应用程序

在 systemd 服务文件中运行 Apache Superset 不起作用。错误:找不到 Flask 应用程序

在 Linux 中创建 bash 脚本的新手。我想创建一个 bash 脚本来安装和设置 Apache Superset 仪表板。我用它来安装所有依赖项和要求,没有任何问题。现在的问题是我想为 Apache Superset 创建一个系统服务文件,以便该服务在后台运行。仅当我通过 SSH 连接到实例时,运行此命令才会持续: superset run -h 0.0.0.0 -p 8088 --with-threads --reload --debugger 如果我关闭连接,则超集将停止运行。

这就是为什么我想为 Apache Superset 创建一个 systemd 服务文件。

以下是在 bash 脚本中创建 systemd 的部分:

cat <<EOL | sudo tee /etc/systemd/system/superset.service
[Unit]
Description=Apache Superset service
After=network.target

[Service]
Type=simple
User=ubuntu
WorkingDirectory=/home/ubuntu
Environment="PATH=/home/ubuntu/venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
ExecStart=/home/ubuntu/venv/bin/superset run -h 0.0.0.0 -p 8088 --with-threads --reload --debugger
Restart=always

[Install]
WantedBy=multi-user.target
EOL

# Start and enable Superset service
sudo systemctl enable superset.service
sudo systemctl start superset.service

当我检查 superset.service 的状态时,它说它正在运行。但是当我检查监听的端口时,Python和Superset没有监听8088。这是运行后的日志journalctl -u superset.service

-- Logs begin at Fri 2023-08-18 02:58:49 UTC, end at Fri 2023-08-18 03:12:20 UTC. --
Aug 18 03:11:25 ip-172-31-122-121 systemd[1]: Started Apache Superset service.
Aug 18 03:11:28 ip-172-31-122-121 superset[34618]: Usage: superset [OPTIONS] COMMAND [ARGS]...
Aug 18 03:11:28 ip-172-31-122-121 superset[34618]: Try 'superset --help' for help.
Aug 18 03:11:28 ip-172-31-122-121 superset[34618]: Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" o>
Aug 18 03:11:28 ip-172-31-122-121 systemd[1]: superset.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
Aug 18 03:11:28 ip-172-31-122-121 systemd[1]: superset.service: Failed with result 'exit-code'.
Aug 18 03:11:28 ip-172-31-122-121 systemd[1]: superset.service: Scheduled restart job, restart counter is at 1.
Aug 18 03:11:28 ip-172-31-122-121 systemd[1]: Stopped Apache Superset service.
Aug 18 03:11:28 ip-172-31-122-121 systemd[1]: Started Apache Superset service.

这段代码已经在 bash 脚本中了export FLASK_APP=superset。我不知道我还缺少什么,或者还有其他方法来设置它吗?

顺便说一句,Amazon EC2 实例上的 Ubuntu 版本是 20.04。

答案1

如果该FLASK_APP变量仅在设置服务文件的脚本中设置,则该变量对服务本身不可见。

Environment您可能需要将其添加到服务文件的设置列表中。

所以像这样:

Environment="PATH=/home/ubuntu/venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
Environment="FLASK_APP=superset"

或者,您可以创建一个包装脚本,服务文件将调用该脚本而不是您当前拥有的命令,并将 包含export FLASK_APP=superset在其中。

相关内容