gunicorn 服务后如何 运行 shell 命令?这是 docker enterypoint.sh 文件

How to run shell command after gunicorn service? this is for docker enterypoint.sh file

下面是我的dockerenterypoint.sh文件代码

#!/bin/bash
set -e

python3 test1.py

gunicorn -b 0.0.0.0:8000 "app:app" --workers=1 --threads=10 --timeout=3600

node /home/test2.js

我想在 gunicorn 服务启动后 运行 test2.js nodejs 应用程序,因为 test2.js 需要连接 localhost:8000。请帮我解决这个问题

默认情况下,下一行仅在上一行之后执行,但命令可能在端口处于活动状态之前结束,因此您可以使用 while 来检查

#!/bin/bash
set -e

python3 test1.py

gunicorn -b 0.0.0.0:8000 "app:app" --workers=1 --threads=10 --timeout=3600
#
check=1
#
while [ $check -eq 1 ]
do
  echo "Testing"
  test=$(netstat -nlt | grep "0.0.0.0:8000" &> /dev/null)
  check=$?
  sleep 2
done

node /home/test2.js

试试这个:

#!/bin/bash
set -e

python3 test1.py

# wait 10 seconds, then run test2.js
{ sleep 10; node /home/test2.js; } &

gunicorn -b 0.0.0.0:8000 "app:app" --workers=1 --threads=10 --timeout=3600

认为理想的解决方案是使用 docker-compose 部署 2 个不同的容器,一个用于 gunicorn,另一个用于 test2.js nodejs 应用程序。

但是可以 运行 test2.js nodejs 脚本在启动 gunicorn 服务后使用下面的代码通过在 gunicorn 行末尾插入“&”

#!/bin/bash
set -e

python3 test1.py

gunicorn -b 0.0.0.0:8000 "app:app" --workers=1 --threads=10 --timeout=3600 &

node /home/test2.js