尝试在“AfterInstall”事件上运行脚本时,我收到以下错误信息:
我在阅读了这篇文章之后,添加了
我发现使用命令
LifecycleEvent - AfterInstall
Script - deploy/install_dependencies.sh
[stdout]Install dependencies. /
[stderr]/usr/bin/env: node: No such file or directory
这很奇怪,因为:
which node
的结果是:~/.nvm/versions/node/v15.2.0/bin/node
which npm
的结果是:~/.nvm/versions/node/v15.2.0/bin/npm
appspec.yml不是由我设置的,但我认为它很简单明了:
version: 0.0
os: linux
files:
- source: /
destination: /home/ec2-user/deploy/
permissions:
- object: /
pattern: "**"
owner: ec2-user
hooks:
AfterInstall:
- location: deploy/install_dependencies.sh
- location: deploy/build.sh
- location: deploy/install_prd_dependencies.sh
- location: deploy/copy_overwrite.sh
# - location: deploy/migrations.sh
timeout: 300
runas: ec2-user
ApplicationStart:
- location: deploy/start_server.sh
timeout: 300
runas: ec2-user
我在阅读了这篇文章之后,添加了
runas: ec2-user
和 permissions
键及其值。文章链接为:Start Node Application in AfterInstall Hook。虽然那篇文章和我的问题不完全相同,但我认为这样做可以解决我的问题,但事实证明并没有。我发现使用命令
which nvm
无法检测到是否已安装 nvm
。[ec2-user@my-ip ~]$ which nvm
/usr/bin/which: no nvm in (/home/ec2-user/.nvm/versions/node/v15.2.0/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ec2-user/.local/bin:/home/ec2-user/bin)
但是,只输入nvm
,我可以顺利地接收到nvm
的CLI帮助命令。
我检查了ec2-user
的.bashrc
文件,以查看是否正确导出了nvm(如这个答案所建议的):
# User specific aliases and functions
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
所有脚本都使用npm/node二进制文件的绝对路径,例如:
install_dependencies.sh
#!/bin/bash
echo "Install dependencies. $(pwd)"
cd /home/ec2-user/deploy/
/home/ec2-user/.nvm/versions/node/v15.2.0/bin/npm install
start_server.sh
#!/bin/bash
echo "Restarting servers..."
/home/ec2-user/.nvm/versions/node/v15.2.0/bin/pm2 restart 0
即使为 ec2-user
设置了 runas
,codedpeloy 是否仍然使用 root
用户来发出这些命令?我不明白发生了什么事,以前它是可以运行的。