У меня есть приложение, написанное на Flask с использованием библиотеки для рисования pyvista.
Приложение запускается на сервере без графической подсистемы (Ubuntu Server 22.04 LTS). Приложение ориентировано для запуска от пользователя myapp со своим venv. Оно прекрасно запускается и работает через flask или через gunicorn app:app.
Я создал для unicorn демона /etc/systemd/system/myapp.service:
[Unit]
Description=Gunicorn instance to serve MyApp
After=network.target
[Service]
User=myapp
Group=myapp
WorkingDirectory=/myapp
Environment="PATH=/myapp/venv/bin"
ExecStart=/myapp/venv/bin/gunicorn --workers 1 app:app
[Install]
WantedBy=multi-user.target
Но с ним вышла проблема: при запуске через демон получаю ошибку в журнале:
Jul 07 13:44:11 test systemd[1]: Started Gunicorn instance to serve myapp.
Jul 07 13:44:11 test gunicorn[37758]: [2023-07-07 13:44:11 +0500] [37758] [INFO] Starting gunicorn 20.1.0
Jul 07 13:44:11 test gunicorn[37758]: [2023-07-07 13:44:11 +0500] [37758] [INFO] Listening at: http://127.0.0.1:8000 (37758)
Jul 07 13:44:11 test gunicorn[37758]: [2023-07-07 13:44:11 +0500] [37758] [INFO] Using worker: sync
Jul 07 13:44:11 test gunicorn[37759]: [2023-07-07 13:44:11 +0500] [37759] [INFO] Booting worker with pid: 37759
Jul 07 13:44:12 test gunicorn[37759]: 2023-07-07 13:44:12.948564: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
Jul 07 13:44:13 test gunicorn[37759]: 2023-07-07 13:44:13.014851: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
Jul 07 13:44:13 test gunicorn[37759]: 2023-07-07 13:44:13.849162: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
Jul 07 13:44:15 test gunicorn[37767]: sh: 1: which: not found
Jul 07 13:44:15 test gunicorn[37759]: [2023-07-07 13:44:15 +0500] [37759] [ERROR] Exception in worker process
Jul 07 13:44:15 test gunicorn[37759]: Traceback (most recent call last):
Jul 07 13:44:15 test gunicorn[37759]: File "/myapp/venv/lib/python3.10/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
Jul 07 13:44:15 test gunicorn[37759]: worker.init_process()
Jul 07 13:44:15 test gunicorn[37759]: File "/myapp/venv/lib/python3.10/site-packages/gunicorn/workers/base.py", line 134, in init_process
Jul 07 13:44:15 test gunicorn[37759]: self.load_wsgi()
Jul 07 13:44:15 test gunicorn[37759]: File "/myapp/venv/lib/python3.10/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
Jul 07 13:44:15 test gunicorn[37759]: self.wsgi = self.app.wsgi()
Jul 07 13:44:15 test gunicorn[37759]: File "/myapp/venv/lib/python3.10/site-packages/gunicorn/app/base.py", line 67, in wsgi
Jul 07 13:44:15 test gunicorn[37759]: self.callable = self.load()
Jul 07 13:44:15 test gunicorn[37759]: File "/myapp/venv/lib/python3.10/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
Jul 07 13:44:15 test gunicorn[37759]: return self.load_wsgiapp()
Jul 07 13:44:15 test gunicorn[37759]: File "/myapp/venv/lib/python3.10/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
Jul 07 13:44:15 test gunicorn[37759]: return util.import_app(self.app_uri)
Jul 07 13:44:15 test gunicorn[37759]: File "/myapp/venv/lib/python3.10/site-packages/gunicorn/util.py", line 359, in import_app
Jul 07 13:44:15 test gunicorn[37759]: mod = importlib.import_module(module)
Jul 07 13:44:15 test gunicorn[37759]: File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
Jul 07 13:44:15 test gunicorn[37759]: return _bootstrap._gcd_import(name[level:], package, level)
Jul 07 13:44:15 test gunicorn[37759]: File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
Jul 07 13:44:15 test gunicorn[37759]: File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
Jul 07 13:44:15 test gunicorn[37759]: File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
Jul 07 13:44:15 test gunicorn[37759]: File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
Jul 07 13:44:15 test gunicorn[37759]: File "<frozen importlib._bootstrap_external>", line 883, in exec_module
Jul 07 13:44:15 test gunicorn[37759]: File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
Jul 07 13:44:15 test gunicorn[37759]: File "/myapp/app/__init__.py", line 48, in <module>
....
Jul 07 13:44:15 test gunicorn[37759]: File "/myapp/app/math.py", line 24, in <module>
Jul 07 13:44:15 test gunicorn[37759]: pv.start_xvfb()
Jul 07 13:44:15 test gunicorn[37759]: File "/myapp/venv/lib/python3.10/site-packages/pyvista/utilities/xvfb.py", line 47, in start_xvfb
Jul 07 13:44:15 test gunicorn[37759]: raise OSError(XVFB_INSTALL_NOTES)
Jul 07 13:44:15 test gunicorn[37759]: OSError: Please install Xvfb with:
Jul 07 13:44:15 test gunicorn[37759]: Debian
Jul 07 13:44:15 test gunicorn[37759]: $ sudo apt install libgl1-mesa-glx xvfb
Jul 07 13:44:15 test gunicorn[37759]: CentOS / RHL
Jul 07 13:44:15 test gunicorn[37759]: $ sudo yum install libgl1-mesa-glx xvfb
Jul 07 13:44:15 test gunicorn[37759]: [2023-07-07 13:44:15 +0500] [37759] [INFO] Worker exiting (pid: 37759)
Jul 07 13:44:16 test gunicorn[37758]: [2023-07-07 13:44:16 +0500] [37758] [INFO] Shutting down: Master
Jul 07 13:44:16 test gunicorn[37758]: [2023-07-07 13:44:16 +0500] [37758] [INFO] Reason: Worker failed to boot.
Т.е. приложение не видит библиотеку xvfb и я не понимаю, почему. Она есть и прекрасно работает. Но когда тот же пользователь запускает тот же скрипт, но только через systemctl - то библиотека сразу пропадает