Sysvinit

通過 init.d 腳本啟動 UWSGI 以退出程式碼 1 退出並顯示“失敗”,但程序執行正常

  • January 5, 2022

在這被標記為重複之前,我閱讀了這個問題;但是添加logfile2並沒有改變任何東西。


我目前正在創建一個使用 UWSGI 執行 Python 堆棧的 docker 容器,為此我使用預設的 Debian 安裝的 uwsgi 作為init.d腳本,並使用pip.

但是,每當我啟動服務時,它都會輸出一個錯誤,指出腳本無法啟動,但是當我嘗試向伺服器發出請求 ( localhost:5000) 或嘗試查看程序是否正在執行ps aux | grep -i uwsgi時,伺服器執行得很好。

我的意思的例子:

root@f16b3268a956:~# /etc/init.d/uwsgi start
[FAIL] Starting app server(s): uwsgi -> ! failed!

但是,如果我查看正在執行的程序

root@f16b3268a956:~# ps aux | grep -i uwsgi
user       984  0.1  2.8 235284 57876 ?        S    14:15   0:00 /usr/local/bin/uwsgi --ini /usr/share/uwsgi/conf/default.ini --ini /etc/uwsgi/apps-enabled/portal.ini --daemonize /var/log/uwsgi/app/portal.log
user      1009  0.0  2.7 253204 57040 ?        S    14:15   0:00 /usr/local/bin/uwsgi --ini /usr/share/uwsgi/conf/default.ini --ini /etc/uwsgi/apps-enabled/portal.ini --daemonize /var/log/uwsgi/app/portal.log
root      1146  0.0  0.0  12860   976 pts/0    S+   14:24   0:00 grep -i uwsgi

我的 uwsgi 配置的內容:

[uwsgi]
http-socket = localhost:5000
http-websockets = true
chmod-socket=775
chdir = /home/user/
master = true
binary-path = /usr/local/bin/uwsgi
virtualenv = /home/user/portal
module = portal.server:app
uid = user
gid = user
processes = 1
gevent = 1000
logfile2 = /tmp/test.pid
logto = /var/log/uwsgi/app/portal.log

查看日誌文件也沒有顯示任何奇怪的東西:

root@f16b3268a956:~# cat /var/log/uwsgi/app/portal.log
Thu Oct 25 14:29:48 2018 - *** Starting uWSGI 2.0.17.1 (64bit) on [Thu Oct 25 14:29:48 2018] ***
Thu Oct 25 14:29:48 2018 - compiled with version: 6.3.0 20170516 on 25 October 2018 13:26:09
Thu Oct 25 14:29:48 2018 - os: Linux-4.9.87-linuxkit-aufs #1 SMP Wed Mar 14 15:12:16 UTC 2018
Thu Oct 25 14:29:48 2018 - nodename: f16b3268a956
Thu Oct 25 14:29:48 2018 - machine: x86_64
Thu Oct 25 14:29:48 2018 - clock source: unix
Thu Oct 25 14:29:48 2018 - pcre jit disabled
Thu Oct 25 14:29:48 2018 - detected number of CPU cores: 4
Thu Oct 25 14:29:48 2018 - current working directory: /
Thu Oct 25 14:29:48 2018 - writing pidfile to /run/uwsgi/app/portal/pid
Thu Oct 25 14:29:48 2018 - detected binary path: /usr/local/bin/uwsgi
Thu Oct 25 14:29:48 2018 - setgid() to 1000
Thu Oct 25 14:29:48 2018 - setuid() to 1000
Thu Oct 25 14:29:48 2018 - chdir() to /home/user/
Thu Oct 25 14:29:48 2018 - writing pidfile to /tmp/test.pid
Thu Oct 25 14:29:48 2018 - your memory page size is 4096 bytes
Thu Oct 25 14:29:48 2018 - detected max file descriptor number: 1048576
Thu Oct 25 14:29:48 2018 - - async cores set to 1000 - fd table size: 1048576
Thu Oct 25 14:29:48 2018 - lock engine: pthread robust mutexes
Thu Oct 25 14:29:48 2018 - thunder lock: disabled (you can enable it with --thunder-lock)
Thu Oct 25 14:29:48 2018 - uwsgi socket 0 bound to UNIX address /run/uwsgi/app/portal/socket fd 3
Thu Oct 25 14:29:48 2018 - uwsgi socket 1 bound to TCP address localhost:5000 fd 5
Thu Oct 25 14:29:48 2018 - Python version: 3.6.6 (default, Sep  5 2018, 03:40:52)  [GCC 6.3.0 20170516]
Thu Oct 25 14:29:48 2018 - Set PythonHome to /home/user/portal
Thu Oct 25 14:29:48 2018 - Python main interpreter initialized at 0x563332cd13a0
Thu Oct 25 14:29:48 2018 - python threads support enabled
Thu Oct 25 14:29:48 2018 - your server socket listen backlog is limited to 100 connections
Thu Oct 25 14:29:48 2018 - your mercy for graceful operations on workers is 60 seconds
Thu Oct 25 14:29:48 2018 - mapped 21036928 bytes (20543 KB) for 1000 cores
Thu Oct 25 14:29:48 2018 - *** Operational MODE: async ***
Thu Oct 25 14:29:49 2018 - WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x563332cd13a0 pid: 1814 (default app)
Thu Oct 25 14:29:49 2018 - *** uWSGI is running in multiple interpreter mode ***
Thu Oct 25 14:29:49 2018 - spawned uWSGI master process (pid: 1814)
Thu Oct 25 14:29:49 2018 - spawned uWSGI worker 1 (pid: 1839, cores: 1000)
Thu Oct 25 14:29:49 2018 - *** running gevent loop engine [addr:0x5633314d0fd0] ***

有誰知道如何解決這個問題?

我最終通過基於以下 docker 鏡像解決了這個問題,該鏡像使用 supervisord 而不是 init.d 程序(沒有 systemctl)。

您需要傳遞--cap-add SYS_PTRACEdocker run,否則start-stop-daemon將無法檢測到守護程序正在執行並且初始化腳本將失敗。

引用自:https://unix.stackexchange.com/questions/477760