我正在 Amazon Linux AMI 上為我的 AWS EC2 運行以下用戶資料腳本。我想運行一個簡單的 socketio 服務器,但是每當我停止并啟動實體時,腳本就不會運行。當我通過 Connect 門戶進行 SSH 并運行這些命令時,它可以作業。此外,從我的運行實體創建 AMI 時,Git 存在,但從該 AMI 生成的實體上不存在 Node。
我按照 AWS 檔案安裝節點,它暗示它應該與 AMI 一起使用。
#!/bin/bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
. ~/.nvm/nvm.sh
nvm install node
sudo yum install git -y
git clone https://github.com/justincervantes/socketio-server-demo.git
cd socketio-server-demo
npm i
node index.ts
提前感謝您的建議。
uj5u.com熱心網友回復:
用戶資料 shell 腳本必須以#!字符和要讀取腳本的解釋器的路徑開頭(通常為/bin/bash)。
因此,對于此腳本的作業,在開頭添加以下內容:
#!/bin/bash
您還需要獲取 NVM 檔案以確保變數在當前 shell 中可用
要作業,您的腳本將如下所示:
#!/bin/bash
apt-get -y update
cat > /tmp/subscript.sh << EOF
# START UBUNTU USERSPACE
echo "Setting up NodeJS Environment"
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
echo 'export NVM_DIR="/home/ubuntu/.nvm"' >> /home/ubuntu/.bashrc
echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm' >> /home/ubuntu/.bashrc
# Dot source the files to ensure that variables are available within the current shell
. /home/ubuntu/.nvm/nvm.sh
. /home/ubuntu/.profile
. /home/ubuntu/.bashrc
# Install NVM, NPM, Node.JS & Grunt
nvm install node
sudo yum install git -y
git clone https://github.com/justincervantes/socketio-server-demo.git
cd socketio-server-demo
node index.ts
EOF
chown ubuntu:ubuntu /tmp/subscript.sh && chmod a x /tmp/subscript.sh
sleep 1; su - ubuntu -c "/tmp/subscript.sh"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/437041.html
