如何使用shell脚本文件启动fastapi,react,节点服务器

发布于 2025-02-07 23:57:17 字数 487 浏览 1 评论 0 原文

我需要一个一个一个一个命令来启动我的项目,而不是我尝试将命令放在shell脚本文件

server.sh

#!/bin/bash
sudo systemctl start elasticsearch.service
sudo systemctl start kibana.service
cd fastapi
uvicorn main:app --reload --port 8000
cd ..
cd reactjs
npm i 
npm start
cd ..
cd node
npm i 
npm run dev

上,这些是我将其放在 .sh 文件,现在问题是在 UVICORN主:App -Reload -port 8000 此命令SH文件未能执行其余命令。

如何使用 .sh 文件或 yaml 文件解决此问题

I need to run many commands one by one to start my project instead of that i tried to put commands on shell script file

server.sh

#!/bin/bash
sudo systemctl start elasticsearch.service
sudo systemctl start kibana.service
cd fastapi
uvicorn main:app --reload --port 8000
cd ..
cd reactjs
npm i 
npm start
cd ..
cd node
npm i 
npm run dev

These are commands I put it in a .sh file, now problem is after uvicorn main:app --reload --port 8000 this command sh files failed to execute rest of the commands.

how to resolve this using .sh file or yaml file

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅此而已 2025-02-14 23:57:17

您必须在代码中的三个主要脚本中运行:

UVICORN MAIN:APP -RELOAD -PORT 8000& amp;

npm start&

npm Run dev dev &

& 在命令之后使用该命令在后台运行,因此该脚本不会在第一个命令(Avicorn)中停止,并且将随着代码的遵循。

并且由于这些命令将在终端中生成输出(如果您从中运行它们)可能会混淆,因此我建议将输出重定向到您在后台运行的每个命令的文件。

您的代码可能就是这样:

#!/bin/bash

sudo systemctl start elasticsearch.service
sudo systemctl start kibana.service
cd fastapi
uvicorn main:app --reload --port 8000 > uvicorn.log &
cd ../reactjs
npm i
npm start > npmstart.log &
cd ../node
npm i 
npm run dev > npmdev.log &

为了杀死这些过程,您应该使用 kill 命令。
杀人有几种选择,您可以访问 linux上的杀死信号有用。
或者,如果您想使用GUI,则系统监视器可能会起作用,但是您必须知道您想杀死的PID是什么。

当一个过程启动时,将其保存在 $!其当前PID(进程ID)中,因此您可以使用下一个语句显示PID:

echo $!

以及当您想要时要杀死该过程,请使用:

kill -9 the_pid_shown_with_echo

因此您的代码可能是这样(但在这种情况下是不够的):

 #!/bin/bash

sudo systemctl start elasticsearch.service
sudo systemctl start kibana.service
cd fastapi
uvicorn main:app --reload --port 8000 > uvicorn.log &
echo "Uvicorn ID: $!" | tee uvicornpid.txt
cd ../reactjs
npm i
npm start > npmstart.log &
echo "npm start ID: $!" | tee npmstart.txt
cd ../node
npm i 
npm run dev > npmdev.log &
echo "npm run dev ID: $!" | tee npmrundev.txt

带有 tee> tee> tee 命令的语句,例如回声“ Uvicorn ID:$!” | Tee Uvicornpid.txt 用于在终端显示文本,并将输出重定向到文件。因此,您可以稍后检查这些文件以检查PID的内容,

但是正如我所说,在这种情况下,这还不够,因为除非节点使用各种过程,否则您使用 $! /代码>这将杀死该过程(也许还有其他过程)。但是,在该端口中侦听的过程将保持运行,当您再次运行应用程序时,这将崩溃,因为端口正在使用(除非您在另一个端口中运行该应用程序,否则我不建议这样做)。

您可以使用多个命令来获取应杀死的PID。
第一种方法是使用命令:

pgrep node

这将返回与 node word

ps -efl | grep -e“(节点| pid)”

这将返回带有几列的输出,您将看到与 node 匹配的所有过程的pid单词和另一个可能有用的信息。

其他有用的命令可能对您来说更好的是:

lsof -I:4000

这将返回在4000端口中运行的过程(您将获得PID,该过程的名称和更多信息)

FUSER 4000/TCP

这仅将返回 4000/TCP 以及该端口中运行的过程的PID。

因此,一旦使用其中一种方法获得PID,就应按照我以前解释的 kill 命令杀死该过程。

You must run in background the three main scripts in your code:

uvicorn main:app --reload --port 8000 &

npm start &

npm run dev &

That & is used after a command to run this one in background, so the script will not stop in the first command (avicorn) and it will follow with the code.

And because of those commands will generate an output in the terminal (in the case you are running them from it) that output can be confused, so I would recommend redirect the output to a file for every command you run in background.

Your code could be like this:

#!/bin/bash

sudo systemctl start elasticsearch.service
sudo systemctl start kibana.service
cd fastapi
uvicorn main:app --reload --port 8000 > uvicorn.log &
cd ../reactjs
npm i
npm start > npmstart.log &
cd ../node
npm i 
npm run dev > npmdev.log &

For killing those process you should use the kill command.
There are several options for killing, you can visit kill signals on Linux to understand how it works.
Or if you want to use a GUI, the system monitor might work, but you must know what PID is what you want to kill.

When a process starts this saves in $! its current PID (process ID), so you can use the next statement to show the PID:

echo $!

And when you want to kill the process, use:

kill -9 the_pid_shown_with_echo

So your code could be like this (but it's not enough in this case):

 #!/bin/bash

sudo systemctl start elasticsearch.service
sudo systemctl start kibana.service
cd fastapi
uvicorn main:app --reload --port 8000 > uvicorn.log &
echo "Uvicorn ID: $!" | tee uvicornpid.txt
cd ../reactjs
npm i
npm start > npmstart.log &
echo "npm start ID: $!" | tee npmstart.txt
cd ../node
npm i 
npm run dev > npmdev.log &
echo "npm run dev ID: $!" | tee npmrundev.txt

The statements with tee command like echo "Uvicorn ID: $!" | tee uvicornpid.txt are used for showing the text in the terminal and also redirect the output to a file. So you can check those files later for checking the PID's

But as I said, in this case this is not enough because unless with node this runs various process and if you kill the process by using the PID you got with $! this will kill that process (and maybe other one). But the process which is listening in that port, will stay running and when you run the app again, this will crash because the port is in use (unless you run the app in another port, but I would not recommend it).

You can use several commands for getting the PID you should kill.
The first way is using commands like:

pgrep node

This will return all PID which match with node word

ps -efl | grep -E "(node|PID)"

This will return an output with several columns and you will can see the PID of all process which match with node word and another information that might be useful.

Other useful commands the might be better for you are these:

lsof -i :4000

This will return the process running in the 4000 port (you will get the PID, the name of the process and more information)

fuser 4000/tcp

This only will return 4000/tcp and the PID of the process running in that port.

So, once you get the PID with one of those methods, you should kill the process with the kill command as I explained before.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文