4

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?

Greg Hornby
  • 43
  • 1
  • 4

2 Answers2

5

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

Jonathan Turpie
  • 1,343
  • 10
  • 16
  • How do we kill this process created by this command ? It seems to respawn into a new process(different PID) in the ApplicationStop script. I've tried `fuser -k 3000/tcp`, `lsof -P | grep ':3000' | awk '{print $2}' | xargs kill -9` every time a new process is created. Thanks in advance. – Ninz Feb 19 '16 at 08:34
  • start.sh should look like `npm start > /dev/null 2> /dev/null < /dev/null & echo $! > node.pid`, basically add `echo $! > node.pid` to the end. Also make stop.sh which will kill that pid `if [ -f node.pid ]; then kill `cat node.pid` fi` – Silverfall05 Oct 01 '16 at 17:21
2

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

Community
  • 1
  • 1
Chris
  • 3,795
  • 3
  • 17
  • 23