11

I'm trying to use AWS CodeDeploy to deploy my application. Everything seems to be working fine but I'm getting the following error.

[stderr]/opt/codedeploy-agent/deployment-root/f1ea67bd-97bo-08q1-b3g4-7b14becf91bf/d-WJL0QLF9H/deployment-archive/scripts/start_server.sh: line 3: pm2: command not found

Below is my start_server.sh file.

#!/bin/bash
pm2 start ~/server.js -i 0 --name "admin" &

I have tried using SSH to connect to my server as user ubuntu and running that bash file and it works perfectly with no errors. So I know that PM2 is installed and working correctly on that user.

Below is also my appspec.yml file.

version: 0.0
os: linux
files:
  - source: /
    destination: /home/ubuntu
hooks:
  ApplicationStart:
    - location: scripts/start_server.sh
      timeout: 300
      runas: ubuntu
  ApplicationStop:
    - location: scripts/stop_server.sh
      timeout: 300
      runas: ubuntu

Also not sure if this will help but here is my stop_server.sh file.

#!/bin/bash
npm install pm2 -g
pm2 stop admin || true
pm2 delete admin || true

Any ideas?

Charlie Fish
  • 18,491
  • 19
  • 86
  • 179

5 Answers5

5

Perform the below steps:

  1. which node
  2. sudo ln -s /home/ubuntu/.nvm/versions/node/v12.13.1/bin/node

(output of above step) /usr/bin/node

  1. which pm2
  2. sudo ln -s /home/ubuntu/.nvm/versions/node/v12.13.1/bin/pm2

(output of above step) /usr/bin/pm2

in start_server.sh and stop_server.sh use it as below (run start.sh as ubuntu):

sudo /usr/bin/pm2 status

Hope this will help you!!

Santosh
  • 3,477
  • 5
  • 37
  • 75
Harsh Patel
  • 446
  • 5
  • 8
1

All of the lifecycle events happens in the order if they have scripts to run:

  1. ApplicationStop
  2. DownloadBundle (reserved for CodeDeploy)
  3. BeforeInstall
  4. Install (reserved for CodeDeploy)
  5. AfterInstall
  6. ApplicationStart
  7. ValidateService

If your deployment has reached to ApplicationStart step, which means your ApplicationStop lifecycle event is already succeeded. Can you make sure the "pm2 stop admin" is succeeded(means pm2 is installed).

binbinlu
  • 416
  • 2
  • 5
1

Usually in cases like that is to use full path to pm2.

#!/bin/bash
/usr/local/bin/pm2 start ~/server.js -i 0 --name "admin" &
SneakyMummin
  • 683
  • 1
  • 11
  • 30
1

I just ran into this problem again.

I was able to solve it by ensuring that the following code is running at the beginning of all of my CodeDeploy script files.

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
Charlie Fish
  • 18,491
  • 19
  • 86
  • 179
  • 1
    Is this equivalent to calling `source ~/.bashrc` since those lines are already in `~/.bashrc`? `source ~/.bashrc` didn't work for some reason – Abhilash Kishore Apr 24 '18 at 20:11
  • 1
    @Abhilashk I feel like it should be equivalent, but I also believe I tried that and it wasn't working for some reason. Maybe someone else will have more insight, but I'm not 100% sure. – Charlie Fish Apr 24 '18 at 20:57
0

If you run

npm install pm2 -g

at ApplicationStop step, then it won't be run until the second time you deploy as ApplicationStop is run on the previous deployment archive bundle.

Aleks Andreev
  • 7,016
  • 8
  • 29
  • 37
X97
  • 106
  • 5