Pm2: How do I call `pm2` to check if a process with given name is running?

Created on 27 Feb 2014  路  21Comments  路  Source: Unitech/pm2

I have a simple postdeploy script that gets called whenever I deploy to a server.

In that script I want to start a process if it is not started and if it is already started, then just reload it. How would I script that in bash using the pm2 command?

Open for PR Enhancement

Most helpful comment

For me the problem was that any of the above solutions I tried gave a non-zero exit code, which would stop Jenkins' Post Build task and mark it as failed. The pm2 silent mode has a return code of 1 (even though it doesn't show errors in the output) (pm2 delete -s appName). Also, redirecting the output using > /dev/null still produced a non-zero exit code.

So what I did was this:

pm2 delete -s app || :
pm2 start ./index.js --name=app

The || : part executes if the pm2 delete sets a non-zero return code (which happens when try to delete an app that does not exist yet). The : is a null operator that returns 0 success exit code. So whatever happens, the pm2 start line is executed.

All 21 comments

You can do the following :
ps -ef | grep "pm2: $NAME" | grep -v grep

But I do agree, it should be added somehow in pm2.

I would suggest the following:

pm2 pid <name> which if:

  1. Process is running: prints pid to stdout and exits with 0.
  2. Process is not running: prints nothing to stdout and exits with 1.

How about add --force option to reload command?

pm2 reload --force

To check if a process is running?

I like the pid command.

Original requirement was start or reload, not to check pid. :)

Oh title isn't saying this ^^.
It shouldn't be so hard to patch: https://github.com/Unitech/pm2/blob/master/lib/CLI.js#L666.

Thoughts @Unitech @rlidwka?

Doing a pm2 describe <pm_id|name> ?

@Unitech the output is not captured.

$ TEST=`pm2 describe test`
[PM2][WARN] test doesn't exist
$ echo $TEST

$ 

So how can we use that in a script?

--silent option can suppress error, so it can be use to force execute some commands, such like:

pm2 stop --silent some-name
pm2 delete --silent some-name

That doesn't help. I'm trying to detect whether a node is started from pm2, forever, or some other manager.

I figured out that pm2 shows warnings on stderr, so the bash fix would be to redirect that to stdout:

$ TEST=`pm2 describe test 2>&1`

You can just use the exit code from pm2 describe..

#!/bin/bash
pm2 describe appname > /dev/null
RUNNING=$?

if [ "${RUNNING}" -ne 0 ]; then
  pm2 start ./deploy/development.yml
else
  pm2 restart appname
fi;

Thanks! :+1:

For me the problem was that any of the above solutions I tried gave a non-zero exit code, which would stop Jenkins' Post Build task and mark it as failed. The pm2 silent mode has a return code of 1 (even though it doesn't show errors in the output) (pm2 delete -s appName). Also, redirecting the output using > /dev/null still produced a non-zero exit code.

So what I did was this:

pm2 delete -s app || :
pm2 start ./index.js --name=app

The || : part executes if the pm2 delete sets a non-zero return code (which happens when try to delete an app that does not exist yet). The : is a null operator that returns 0 success exit code. So whatever happens, the pm2 start line is executed.

I came across this issue today using Codeship. I ended up doing this:

pm2 start <appname> --update-env

If the app was already running, it just restarted it.

The problem with the last two options is that I cannot _observe_ if pm2 is running this particular script.

I don't just want to delete the app if it's already running or start the app if it's not running. I want to know _which_ manager is running the app and execute the correct portion of the script. Otherwise I would start the app on all managers.

E.g. if running forever, restart forever. If running pm2, restart pm2. Report back to deploy script which manager was managing the script.

@Redsandro I'm running @petarjs's solution in jenkins and I just had to add #!/bin/bash string at the beginning of Execute shell build step.

Note I'm using a plugin that allows me to run shell scripts as a post-build action

Other then that, works like a charm.

if [ "$(pm2 id appname) "= "[]" ]; then
pm2 start appname
else
pm2 reload appname
fi

How to get pm2 status using python (programatically). I meant how we can detect, is pm2 running or not using python?

Thanks for https://github.com/Unitech/pm2/issues/325#issuecomment-281580956
In my .gitlab-ci.yml file I had to use quotes:
$ pm2 del --silent my-app || ':'

325

For me the problem was that any of the above solutions I tried gave a non-zero exit code, which would stop Jenkins' Post Build task and mark it as failed. The pm2 silent mode has a return code of 1 (even though it doesn't show errors in the output) (pm2 delete -s appName). Also, redirecting the output using > /dev/null still produced a non-zero exit code.

So what I did was this:

pm2 delete -s app || :
pm2 start ./index.js --name=app

The || : part executes if the pm2 delete sets a non-zero return code (which happens when try to delete an app that does not exist yet). The : is a null operator that returns 0 success exit code. So whatever happens, the pm2 start line is executed.

**mike drop*

Was this page helpful?
0 / 5 - 0 ratings

Related issues

psparago picture psparago  路  3Comments

xzycn picture xzycn  路  3Comments

webchaz picture webchaz  路  3Comments

lefam picture lefam  路  3Comments

rangercyh picture rangercyh  路  4Comments