Search code examples
pythonnode.jspm2

How to perform a pm2 start/stop/status of node js from Python script?


Let's say I have a Python script that will use the subprocess library to execute the some basic pm2 commands (for nodejs code) as shown below:

import subprocess
subprocess.call(['pm2', 'status'])

The output then comes in a tabular format with all the services that were started/running/errored on the standard output.

How do I get only the relevant data that I want?

For example: If I do the below:

subprocess.call(['pm2', 'status', 'someService'])

I should only get the output for someService

Is there a library in python that will allow me to do this? Or can I hit the pm2 using curl commands from within my Python script?


Solution

  • After some messing around I have found the answer. A simple pm2 jlist retrieves the output in JSON.

    subprocess.call(['pm2', 'jlist'])