Jacob Lyles
Jacob Lyles

Reputation: 10400

Ansible hangs when starting node.js server

I would like to start my node.js app in an ansible playbook. Right now, the final directive looks like this:

  - name: start node server
    shell: chdir=${app_path} npm start&

The problem is that ansible never returns from this. How can I make it continue?

Upvotes: 15

Views: 7627

Answers (4)

Maoz Zadok
Maoz Zadok

Reputation: 5920

when you close the shell, the process get the SIGHUP signal (like kill -1). you can catch the signal "SIGHUP" in your app.js file.

process.on('SIGHUP', function() {
            logger.info("SIGHUP signal was interrupted");
        });

Upvotes: 1

geerlingguy
geerlingguy

Reputation: 4802

Forever seems to be the simplest and easiest way of starting and daemonizing Node.js apps. Currently, there's no Ansible module for forever, but you can still use the following plays to install forever and run your app:

- name: "Install forever (to run Node.js app)."
  npm: name=forever global=yes state=present

- name: "Check list of Node.js apps running."
  command: forever list
  register: forever_list
  changed_when: false

- name: "Start example Node.js app."
  command: forever start /path/to/app.js
  when: "forever_list.stdout.find('/path/to/app.js') == -1"

This is completely idempotent, and works great for me. You could program a little forever module for Ansible to do this stuff for you (like the service module), but this works for now.

I have a complete example of how to start a Node.js app with Forever and Ansible on Server Check.in's blog.

Upvotes: 31

Quyen Nguyen Tuan
Quyen Nguyen Tuan

Reputation: 1675

Using Forever is the best solution for running nodejs app in background. The solution of @geerlingguy is great for running the app once, but if you want to re-deploy the app, you must stop the server first, and then start it again:

- name: Get app process id
  shell: "ps aux | grep app.js | grep -v grep | awk '{print $2}'"
  register: process_id

- name: Stop app process
  shell: kill -9 {{ item }}
  with_items: process_id.stdout_lines
  ignore_errors: True  # Ignore error when no process running

- name: Start application
  command: forever start path/to/app.js
  environment:
    NODE_ENV: production  # Use this if you want to deploy the app in production

Upvotes: 4

Bruce P
Bruce P

Reputation: 20739

Try using nohup:

- name: start node server
  shell: chdir=${app_path} nohup npm start &

A better approach, however, might be to try using forever so if the app terminates it'll restart automatically.

Upvotes: 1

Related Questions