15

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?

Jacob Lyles
  • 9,920
  • 7
  • 32
  • 30
  • You might want to install forever first, and daemonize nodejs; you can see some information here; http://stackoverflow.com/questions/4903570/how-does-one-start-a-node-js-server-as-a-daemon-process – Kevin Willock Jan 05 '14 at 07:44

4 Answers4

29

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.

geerlingguy
  • 4,682
  • 8
  • 56
  • 92
  • Thanks for this solution. But it is good just for running the app once. What if you want to re-deploy it again? Please check my answer below for a small improvement. – Quyen Nguyen Tuan Jun 07 '14 at 08:34
  • @QuyenNguyenTuan it is quite easy, just use `forever restart /path/to/app.js` when `forever_list.stdout.find('/path/to/app.js') != -1` – vr3C Apr 10 '15 at 03:41
  • Nice solution, I have found that these tasks install `forever` without any problems but are unable for run my node script. If i manually run `forever start /home/vargrant/project/socket.js` I am able to start my `socket.js` script, but if i use this as part of an ansible **command** it does not start the script. Any ideas? – andy_roddam Mar 19 '16 at 10:54
  • Solved the problem that i have described here. I had not realized that `forever` would list the programs that were running depending on the user. Basically, I was becoming Root with Ansible and then I was checking with my normal vagrant user and it was therefore not showing that the node scripts were in fact running. – andy_roddam Mar 19 '16 at 22:58
  • Note that there's a bug in the `npm` module on Ansible 2.x (at least, as of this comment) that prevents install with `state=latest`. Use `present` instead; https://github.com/ansible/ansible-modules-extras/issues/137 – geerlingguy Mar 20 '16 at 12:33
4

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
Quyen Nguyen Tuan
  • 1,677
  • 17
  • 23
1

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.

Bruce P
  • 19,995
  • 8
  • 63
  • 73
1

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");
        });
Maoz Zadok
  • 4,871
  • 3
  • 33
  • 43