Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

pm2 fails to return correctly in ansible #88

Closed
jpmerritt opened this Issue · 16 comments
@jpmerritt

When pm2 commands are run through ansible they operate correctly but fail to return in a way that ansible recognises causing ansible to be stuck at that location waiting for a correct exit.

For example a command like
$ pm2 start pm2-start.json
will run the script but hang afterwards. If you kill that script in a different terminal session the still running script in the other session will finally continue. You can also work around the problem by attaching >& /dev/null to the end of the script so the ansible command might end up looking like this.

shell: pm2 start pm2-start.json >& /dev/null chdir=~/ executable=/bin/bash

@Unitech
Owner

This is strange, I do the classical process.exit(SUCCESS_EXIT); (SUCCESS_EXIT = 1) when the CLI is exited

@arabold

I'm invoking pm2 through SSH on my CI server (via Atlassian Bamboo) and are stuck in a similar situation. My call to pm2 is pm2 start -x /var/www/my-app/app.js --name my-app.

@jonhatalla

i'm also invoking pm2 via ssh
ssh hostname "cd folder; pm2 kill; git pull; npm install; pm2 start index.js"

this won't return from the ssh command - but if i run it locally it returns with an exit code of 0 ($?)

@soyuka soyuka added the bug label
@mehulved

I've run into this bug too. Ansible hangs when it reaches pm2 start even though the command has been executed on the server.

@rlidwka
Collaborator

isn't it an Ansible bug?

@dmourati

It seems more a factor of the text output from pm2.

@cmwelsh

I think this is related to the bug described in #235. I used Ansible's async fire and forget to start pm2 before running any other commands.

- name: Ensure pm2 is running
  command: "pm2 ping"
  async: 30
  poll: 0

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

- name: Start application
  command: "chdir={{appPath}} pm2 start app.js --name my-website"
  when: "pm2_list.stdout.find('my-website') == -1"

- name: Reload application
  command: "chdir={{appPath}} pm2 reload my-website"
@dmourati
  - name: Setup pm2 init
    copy: src=pm2-init.sh dest=/etc/init.d/ owner=root group=root mode=0755
  - name: Service pm2 setup
    service: name=pm2-init.sh enabled=yes
  - name: app.coffee start
    command: chdir=/data/repos/myrepo pm2 start app.coffee
    sudo_user: nobody
    async: 1
    poll: 0
  - name: app.coffee dump 
    command: chdir=/data/repos/myrepo pm2 dump
    sudo_user: nobody
    async: 1
    poll: 0
  - name: app.coffee stop
    command: chdir=/data/repos/myrepo pm2 stop app.coffee
    sudo_user: nobody
    async: 1
    poll: 0

@soyuka
Collaborator

Could it be related to #338 or #281?

@kompot

Same is true for Salt stack. First time I encounter bug of this kind with Salt.
Fixed that by this workaround

start pm2 daemon:
  cmd.run:
    - name: pm2 ping >/dev/null 2>&1
@ryanlelek

Same here with Ansible. Hangs on "start", even when wrapped in a Bash script that exits. Tried "pm2 kill" beforehand to try and restart the whole process, but no luck.

@luckychild305

usually "start" hangs when the process is already running or when you try to start it from a different directory that is messing up your lib imports. This is what I use successfully:

1) Don't kill but delete process instead. Will give you ugly message if no process is running so set ignore_errors.

- name: delete existing pm2 processes if running
  command: pm2 delete all
  ignore_errors: True

2) Start process by defining the full path to file and switch into the relative directory (had this issue for my node.js application, where require didn't work correctly without changing dir first).

- name: start pm2 process
  command: pm2 --run-as-user user start /home/user/full/path/to/file/file.js --name processname
    chdir=/home/full/path/to/file/
  sudo: yes
  sudo_user: user
@luckychild305

to install PM2 I use:

- name: install pm2 globally
  npm: name=pm2 state=present global=yes
  sudo: yes
  sudo_user: root

and

- name: configure pm2 to restart on startup
  shell: pm2 startup ubuntu >& /dev/null chdir=~/ executable=/bin/bash
  sudo: yes
  sudo_user: root
@soyuka soyuka referenced this issue from a commit
@soyuka soyuka Mention #88 tip 0054ac6
@soyuka
Collaborator

:thumbsup:

Closing for now, feel free to re-open.

@soyuka soyuka closed this
@ryanlelek

Thank you for the tips @luckychild305

I'll give it a go :)

@sjwoodr

I have the same problem with running any "pm2" command via SaltStack. After struggling with it for some time, I finally found a workaround:

init-pm2:
    cmd.run:
      - name: env PATH=$PATH:/opt/node/node-v0.12.4-linux-x64/bin pm2 startup linux -u someuser
      - require:
        - cmd: install-npm-pm2
      - user: root

Even though the salt-minion process runs as root, without that explicit "- user: root" in there, the command will hang. This can be a highstate or even a cmd.run for the specific state that executes pm2 commands. Any pm2 command would hang: ping, startup, show, etc... I theorize that having the -user option in the state causes salt to spawn a new shell and that shell is sufficiently different than the one running the command by default. (shrug)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.