Pm2: Non zero exit code when no process found

Created on 8 Sep 2015  路  11Comments  路  Source: Unitech/pm2

When there are no running processes, pm2 stop all and pm2 delete all logs a "No process found" warning, and returns with exit code 1.

Though there might be a good reason to do this, I think it's a bit weird in the sense that the operation technically completes successfully (even though it technically doesn't do anything).

To elaborate a bit more on our specific use case: We run a deployment script that stops and deletes all pm2 processes first before deploying a newer version of the application. So basically when there are no processes running in the first place, the script fails. I'm currently just ignoring the exit code but that's obviously not really clean.

Would it make sense to change the behaviour? Maybe have flags to achieve this? Or maybe my reasoning is just wrong completely and I should use a different approach?

stale

Most helpful comment

My opinion here is that because there is no 'error', it should offer exit code 0.

All 11 comments

You could use something like pm2 jlist|jq 'length' to check if there are processes to delete.

jq is very powerful : https://stedolan.github.io/jq/

@Lapixx
Had you found out this solution? I encounter the same problem with you.

@linxiaowu66 Unfortunately we ended up just keep ignoring the result for this particular command. Didn't have the time to try out soyuka's idea (would definitely work though) at the time.

@Lapixx
Hi, thanks for your helping, I finally try the pm2 kill command, and maybe looks like woking properly.

I think we should change the exit code here, WDYT @Unitech ?

https://github.com/Unitech/pm2/blob/master/test/bash/right-exit-code.sh
Can't know what is the best practice for exit code, there are pros and cons of changing this exit codes

Hmm, yes on one hand, when issuing pm2 stop all or pm2 delete all, if pm2 doesn't find anything to kill it's an error and the exit code 1 is appropriate. On the other hand, if we have no way to check if there are processes to delete (something like pm2 count or pm2 ls --raw | wc -l), there is no appropriate workaround for this case.

I'm +1 for keeping the exit code to 1 but we may add a count feature then, WDYT?

If I use the forever package, and the command forever stopall wont existing when there is no process when exec thenpm` command in the script

I guess it all comes down to whether you want to use the exit code to indicate that something specific has happened, or that a desired state has been reached (in a more "declarative" style than "imperative").

A matter of personal preference probably. I'm just a huge fan of idempotent operations (this problem came up when mixing pm2 with Ansible) nowadays.

However you decide to go with this, I would reccomend trying to keep this consistent accross other commands too.
I'm not 100% about this but I vaguely remember some commands defaulting to a no-op if there is nothing to do (stopping the same process multiple times?)

My opinion here is that because there is no 'error', it should offer exit code 0.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ldarren picture ldarren  路  3Comments

jubairsaidi picture jubairsaidi  路  3Comments

rajendar38 picture rajendar38  路  3Comments

getvega picture getvega  路  3Comments

webchaz picture webchaz  路  3Comments