Code Deploy ApplicationStart gets stuck on pending

2020-07-18 09:27发布

问题:

Hi I'm new to using Code Deploy. I am trying to launch a node application. I have setup.sh, start.sh and app.js in my root directory.

This is my appspec.yml file

version: 0.0
os: linux
files:
 - source: /
   destination: /
hooks:
  Install:
    - location: setup.sh
      timeout: 3600
  ApplicationStart:
    - location: start.sh
      timeout: 3600

setup.sh

yum -y install nodejs npm --enablerepo=epel
npm install

start.sh

node /app.js

app.js (just a basic dummy server)

var express = require("express");
var app = express();

app.get("/",function(req,res) {
    res.send("Hello world")
})


var server = app.listen(8080,function() {
    console.log("Listening at " + server.address().address + ": " + server.address().port);
});

The Install step successfully completes, but Code Deploy gets stuck on pending when it does the ApplicationStart step.

I'm pretty sure it's because the app.js program runs continously, so how is CodeDeploy supposed to know that it's working and move on?

回答1:

The command node /app.js does not run in background but in foreground, therefor the start.sh script is never finished.

See this thread for more info about running node in background Node.js as a background service



回答2:

The CodeDeploy agent is waiting for the script it launched to return an exit code and to close stdout and stderr. To start a process in the background and detach it from the host agent so it can run as a daemon, try:

node /app.js > /dev/null 2> /dev/null < /dev/null &

Note: you'll want to modify your program to write to a log file instead of the console, since daemons usually don't have a console to write to (as it is in this version).

See the official docs here: http://docs.aws.amazon.com/codedeploy/latest/userguide/troubleshooting.html#troubleshooting-long-running-processes