I'm using Ansible to create a pm2 service on a EC2 / Ubuntu instance. Below is the script. When I run it, PM2 is installed and the service is enabled. When I run pm2 list, I don't the see the service, but I can grep it (ps aux | grep node) and see that it's running. It also seems like a shadow copy of pm2 is running and loading the app, but I can't seem to control it.

- hosts: comm
  sudo: yes
    - npm: name=pm2 global=yes
    - name: configure pm2 to restart on startup
      shell: pm2 startup ubuntu >& /dev/null chdir=~/ executable=/bin/bash
      sudo: yes
      sudo_user: root
    - command: sudo env PATH=$PATH:/usr/bin pm2 startup ubuntu -u ubuntu
      sudo: yes
    - command: /usr/bin/pm2 save
    - command: /usr/bin/pm2 start /home/ubuntu/something/app.js --name something

1 Answer 1


This playbook is not idempotent, the shell task is going to run every time the playbook is run, you need to supply a creates argument to that task.

In the current version of PM2:

var scriptFile = '/etc/init.d/pm2-init.sh',

Also, the use of sudo is redundant, as it is already specified globally above, and the default is to sudo to root, so that's redundant as well.

Additionally, once there's an init script for the service, it would be better to use the service module to manage it.

  • Even after running the startup command, getting the init script, and restarting, I don't see pm2 in the list of services running on the box (from service --status-all). Would be nice to use the service module for sure, can you expand your answer in that area?
    – IanVS
    Jul 22, 2015 at 2:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .