2

在 CentOS 7 服务器上,我通过 SCL 安装了 Python 3.6。(https://www.softwarecollections.org/en/scls/rhscl/rh-python36/

我在 .bashrc 中有这一行来启用 SCL 的 Python 3.6

source scl_source enable rh-python36

我已经安装了 pipenv:

pip install --user pipenv

我通过命令行运行 Python 程序:

pipenv run python myprogram.py

所有这些都很好。我有一个使用用户 pipenv 的 Flask 应用程序。我正在尝试创建一个 systemd 单元文件来启动/停止/重新加载 Flask Web 应用程序。如何让 sytemd 单元文件使用通过 SCL 的 Python 和 pip 安装的用户 pipenv?

我尝试从 root 执行命令行,但出现此错误:

[root@localhost ~]# source scl_source enable rh-python36
[root@localhost ~]# /home/user/.local/bin/pipenv run python /home/user/hello.py 
Traceback (most recent call last):
  File "/home/user/.local/bin/pipenv", line 7, in <module>
    from pipenv import cli
ModuleNotFoundError: No module named 'pipenv'

但是,我可以通过加载用户的 bash shell 通过 su -c 执行命令:

 su -c 'bash -lc /home/user/.local/bin/pipenv run python hello.py' user

但这条线似乎很尴尬。我可以在 systemd 单元文件的 ExecStart 行中使用的正确行是什么?为了使用用户的 pipenv,应该包含哪些环境变量?

4

1 回答 1

1

这是我的工作 systemd 单元文件:

[Unit]
Description=Python app
# Requirements
Requires=network.target
# Dependency ordering
After=network.target

[Service]
# Let processes take awhile to start up
TimeoutStartSec=0
RestartSec=10
Restart=always
Environment="APP_SITE_SETTINGS=/home/app/.config/settings.cfg"
Environment="PYTHONPATH=/home/app/.local/lib/python3.6/site-packages"
WorkingDirectory=/home/app/app-site

User=app
Group=app
PermissionsStartOnly=true

KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

# Main process
ExecStartPre=/bin/mkdir -p /run/app
ExecStartPre=/bin/chown app:app /run/app
#ExecStartPre=source scl_source enable rh-python36
ExecStart=/usr/bin/scl enable rh-python36 -- /home/app/.local/bin/pipenv run uwsgi \
  --socket 127.0.0.1:6003 \
  --buffer-size 65535 \
  --enable-threads \
  --single-interpreter \
  --threads 1 \
  -L \
  --stats /run/app/uwsgi_stats.socket \
  --lazy-apps \
  --master-fifo /run/stocks/uwsgimasterfifo \
  --processes 1 \
  --harakiri 960 \
  --max-worker-lifetime=21600 \
  --ignore-sigpipe \
  --ignore-write-errors \
  --disable-write-exception \
  --mount /=run:app \
  --manage-script-name

[Install]
WantedBy=multi-user.target
于 2018-07-17T06:00:02.450 回答