首页 > 解决方案 > 为 aws ubuntu 服务器 node.js 安装设置 bash 脚本失败,但在再次启动时工作

问题描述

我正在尝试使用 bash 脚本在 aws EC-2 服务器上运行我的 Express 服务器。我正在从 test_server.sh 调用其他三个脚本(sleep.sh、server_run 和 install.sh)

测试服务器.sh

#!/bin/bash
# git clone the repo
# cd to the cloned repo directory
# Run the user’s installation steps which will install any necessary dependencies required for the server to run, with sudo permission
chmod +x install.sh
sudo ./install.sh
# 1. Run the user’s server execution steps which will bring up the server
# 2. We’ll be running your server_run.sh as a background process (using &) so that we can run the next set of commands
chmod +x server_run.sh
./server_run.sh &
# 3. Add a sleep timer to sleep.sh depending upon how long you want to sleep so that the server is ready.
chmod +x sleep.sh
./sleep.sh
# Execute the GET /memes endpoint using curl to ensure your DB is in a clean slate
# Should return an empty array.
curl --location --request GET 'http://localhost:8081/memes'
# Execute the POST /memes endpoint using curl
curl --location --request POST 'http://localhost:8081/memes' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "xyz",
"url": "https://images.unsplash.com/photo-1612832022002-66ee448468a4?ixid=MXwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80",
"caption": "This is a meme"
}'
# Execute the GET /memes endpoint using curl
curl --location --request GET 'http://localhost:8081/memes'
# If you have swagger enabled, make sure it is exposed at localhost:8080
# curl --location --request GET 'http://localhost:8080/swagger-ui/'

server_run.sh

    #!/bin/bash
    
    cd server/
    # Setup DB or any other environment variables you want to setup.
    npm install
    npm start

安装.sh

 #!/bin/bash
    sudo apt update
    wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -
    echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list
    sudo apt update
    sudo apt install -y mongodb-org
    sudo systemctl start mongod
    sudo systemctl enable mongod
    # Node
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
    . ~/.nvm/nvm.sh
    nvm install node
    source ~/.bashrc
    node -v
    npm -v

睡眠.sh

#!/bin/bash

# Sleep for 60 seconds
sleep 60

问题是当我第一次运行 test_server.sh 它显示

在此处输入图像描述

但是当我关闭这个终端并从不同的终端运行 test_server.sh 时,它就可以工作了。

标签: node.jslinuxbashshellamazon-ec2

解决方案


推荐阅读