为什么使用 sudo 运行 Flask 服务时 shell 脚本(.sh)给出的结果与命令行不同?

为什么使用 sudo 运行 Flask 服务时 shell 脚本(.sh)给出的结果与命令行不同?

我从命令行运行了一系列命令。以下是从命令行运行时的命令和输出:

~/bettermult $ sudo systemctl stop bettermult
+ sudo systemctl stop bettermult
~/bettermult $ sudo systemctl start bettermult
+ sudo systemctl start bettermult
~/bettermult $ sudo systemctl status bettermult
+ sudo systemctl status bettermult
● bettermult.service - uWSGI instance to serve bettermult
     Loaded: loaded (/etc/systemd/system/bettermult.service; disabled; vendor preset: enabled)
     Active: active (running) since Wed 2024-05-08 01:06:53 UTC; 10s ago
   Main PID: 945740 (uwsgi)
      Tasks: 6 (limit: 1132)
     Memory: 26.9M
     CGroup: /system.slice/bettermult.service
             ├─945740 /home/michael/bettermult/my-venv/bin/uwsgi --ini bettermult.ini
             ├─945752 /home/michael/bettermult/my-venv/bin/uwsgi --ini bettermult.ini
             ├─945753 /home/michael/bettermult/my-venv/bin/uwsgi --ini bettermult.ini
             ├─945754 /home/michael/bettermult/my-venv/bin/uwsgi --ini bettermult.ini
             ├─945755 /home/michael/bettermult/my-venv/bin/uwsgi --ini bettermult.ini
             └─945756 /home/michael/bettermult/my-venv/bin/uwsgi --ini bettermult.ini

    May 08 01:06:53 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945740]: *** Operational MODE: preforking ***
    May 08 01:06:53 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945740]: WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x5644d37f4bf0 pid: 945740 (default app)
    May 08 01:06:53 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945740]: *** uWSGI is running in multiple interpreter mode ***
    May 08 01:06:53 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945740]: spawned uWSGI master process (pid: 945740)
    May 08 01:06:53 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945740]: spawned uWSGI worker 1 (pid: 945752, cores: 1)
    May 08 01:06:53 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945740]: spawned uWSGI worker 2 (pid: 945753, cores: 1)
    May 08 01:06:53 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945740]: spawned uWSGI worker 3 (pid: 945754, cores: 1)
    May 08 01:06:53 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945740]: spawned uWSGI worker 4 (pid: 945755, cores: 1)
    May 08 01:06:53 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945740]: spawned uWSGI worker 5 (pid: 945756, cores: 1)
    May 08 01:06:55 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945753]: [pid: 945753|app: 0|req: 1/1] 185.242.226.7 () {36 vars in 509 bytes} [Wed May  8 01:06:55 2024] GET / => generated 25686 by>

这是正确的输出。请注意,它正确运行了所需的五个线程。

我没有每次都输入这些命令(实际上是在命令行上向上箭头,但仍然如此),而是创建了一个文件deploy.sh

~/bettermult $ cat deploy.sh
+ cat deploy.sh
set -x
gzip -dkf deployment.tar.gz
tar -xf deployment.tar
sudo systemctl stop bettermult
sudo systemctl start bettermult
sudo systemctl status bettermult

在这种情况下,我得到不同的输出(无论我在脚本运行时是否必须输入 sudo 密码,或者系统已经有一个有效的 sudo 密码):

~/bettermult $ source deploy.sh
+ source deploy.sh
++ set -x
++ gzip -dkf deployment.tar.gz
++ tar -xf deployment.tar
++ sudo systemctl stop bettermult
++ sudo systemctl start bettermult
++ sudo systemctl status bettermult
● bettermult.service - uWSGI instance to serve bettermult
     Loaded: loaded (/etc/systemd/system/bettermult.service; disabled; vendor preset: enabled)
     Active: active (running) since Wed 2024-05-08 01:05:27 UTC; 68ms ago
   Main PID: 945702 (uwsgi)
      Tasks: 1 (limit: 1132)
     Memory: 216.0K
     CGroup: /system.slice/bettermult.service
             └─945702 [uwsgi]

May 08 01:05:27 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945702]: lock engine: pthread robust mutexes
May 08 01:05:27 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945702]: thunder lock: disabled (you can enable it with --thunder-lock)
May 08 01:05:27 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945702]: uwsgi socket 0 bound to UNIX address bettermult.sock fd 3
May 08 01:05:27 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945702]: Python version: 3.8.10 (default, Nov 22 2023, 10:22:35)  [GCC 9.4.0]
May 08 01:05:27 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945702]: *** Python threads support is disabled. You can enable it with --enable-threads ***
May 08 01:05:27 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945702]: Python main interpreter initialized at 0x562291f91bf0
May 08 01:05:27 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945702]: your server socket listen backlog is limited to 100 connections
May 08 01:05:27 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945702]: your mercy for graceful operations on workers is 60 seconds
May 08 01:05:27 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945702]: mapped 437520 bytes (427 KB) for 5 cores
May 08 01:05:27 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945702]: *** Operational MODE: preforking ***
May 08 01:05:28 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945702]: WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x562291f91bf0 pid: 945702 (default app)
May 08 01:05:28 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945702]: *** uWSGI is running in multiple interpreter mode ***
May 08 01:05:28 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945702]: spawned uWSGI master process (pid: 945702)
May 08 01:05:28 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945702]: spawned uWSGI worker 1 (pid: 945709, cores: 1)
May 08 01:05:28 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945702]: spawned uWSGI worker 2 (pid: 945710, cores: 1)
May 08 01:05:28 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945702]: spawned uWSGI worker 3 (pid: 945711, cores: 1)
May 08 01:05:28 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945702]: spawned uWSGI worker 4 (pid: 945712, cores: 1)
May 08 01:05:28 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[945702]: spawned uWSGI worker 5 (pid: 945713, cores: 1)

命令行输出有几点不同。例如,它显示了 Python 版本。更重要的是,它给出了一堆关于锁、无线程、有限积压和有限仁慈的警告。

我试图理解为什么会有这些差异。我当时想也许它运行的是不同版本的 Python,但它列出的版本与命令行中的默认版本相同:

~/bettermult $ python --version
+ python --version
Python 3.8.10

我的理解是,默认情况下 .sh 脚本以我的身份运行。那么,为什么不同呢?

添加

这是我按照 ubfan1 在评论中的建议运行的 ./deploy.sh:

~/bettermult $ chmod +x ./deploy.sh
~/bettermult $ ./deploy.sh
++ gzip -dkf deployment.tar.gz
++ tar -xf deployment.tar
++ sudo systemctl stop bettermult
++ sudo systemctl start bettermult
++ sudo systemctl status bettermult
● bettermult.service - uWSGI instance to serve bettermult
     Loaded: loaded (/etc/systemd/system/bettermult.service; disabled; vendor preset: enabled)
     Active: active (running) since Wed 2024-05-08 11:43:51 UTC; 105ms ago
   Main PID: 957810 (uwsgi)
      Tasks: 1 (limit: 1132)
     Memory: 5.1M
     CGroup: /system.slice/bettermult.service
             └─957810 /home/michael/bettermult/my-venv/bin/uwsgi --ini bettermult.ini

May 08 11:43:51 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[957810]: lock engine: pthread robust mutexes
May 08 11:43:51 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[957810]: thunder lock: disabled (you can enable it with --thunder-lock)
May 08 11:43:51 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[957810]: uwsgi socket 0 bound to UNIX address bettermult.sock fd 3
May 08 11:43:51 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[957810]: Python version: 3.8.10 (default, Nov 22 2023, 10:22:35)  [GCC 9.4.0]
May 08 11:43:51 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[957810]: *** Python threads support is disabled. You can enable it with --enable-threads ***
May 08 11:43:51 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[957810]: Python main interpreter initialized at 0x560c5795cbf0
May 08 11:43:51 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[957810]: your server socket listen backlog is limited to 100 connections
May 08 11:43:51 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[957810]: your mercy for graceful operations on workers is 60 seconds
May 08 11:43:51 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[957810]: mapped 437520 bytes (427 KB) for 5 cores
May 08 11:43:51 ubuntu-s-1vcpu-1gb-intel-nyc3-01 uwsgi[957810]: *** Operational MODE: preforking ***

答案1

哎呀!

谜团已揭开。

调用sudo systemctl start bettermult会启动服务启动过程,但启动本身是异步的。因此sudo systemctl status bettermult运行时,它会在几毫秒后显示服务器的状态。

事实证明,“Python 线程支持已禁用”的内容总是正在输出,无论是否手动运行。但systemctl status只显示日志的最后 n 行。因此,当我手动执行此操作并且服务器有时间在我发出之前生成所有工作程序时systemctl status,我看到了所有工作程序,但没有看到更高级别的线程错误。在脚本中,工作程序尚未启动,所以我只看到了主服务进程,并看到了那些警告行。我错误的假设是它向systemctl status我展示了启动过程的“结果”,但实际上它总是只显示日志的最后 n 行。

等待足够长的时间以查看完整启动过程的新脚本:

set -x
gzip -dkf deployment.tar.gz
tar -xf deployment.tar
sudo systemctl stop bettermult
sleep 2s
sudo systemctl start bettermult
sleep 2s
sudo systemctl status bettermult
set +x

至于“Python 线程支持已禁用。”警告,这对我来说仍然很困惑。我确实threading在 Flask Python 代码中使用 Python 来触发/处理定期的日常管理任务,而且它似乎运行良好。所以我不确定线程​​支持是如何/在哪里被禁用的。但那是另一个问题。

相关内容