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?

1

4 Answers 4

31

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
@QuyenNguyenTuan it is quite easy, just use forever restart /path/to/app.js when forever_list.stdout.find('/path/to/app.js') != -1
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?
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.
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; github.com/ansible/ansible-modules-extras/issues/137
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 

1 Comment

You can also use forever restart, or forever restartall to save a few steps.
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.

Comments

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"); }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.