Open Source Uses About
BaseCode

Programmatic use of PM2

by Brian Faust – 2 minute read

If you’ve ever had the need to programmatically work with PM2 you’ve probably realised within minutes that you are in the midst of Callback Hell, which is no play you ever want to be at. What if we could have a more sane and reliable way of working with PM2 from inside our application?

Foreman

Foreman is a npm package developed in TypeScript which which makes use of execa to talk to the PM2 daemon that is running on your system. It’s goal is to provide consistency by using the system-wide PM2 daemon and a great developer experience by abstracting away all all the nitty gritty work that is needed to talk to PM2 and process its responses.

Initialising Foreman

import { Foreman } from "@typeskrift/foreman";

const foreman: Foreman = new Foreman();

Listing all processes

import { ProcessDescription } from "@typeskrift/foreman";

const processes: ProcessDescription[] | undefined = foreman.list();

Show details about a specific process

import { ProcessDescription } from "@typeskrift/foreman";

const process: ProcessDescription | undefined = foreman.describe(
    "process-name-or-id"
);

Start a new process

const { failed } = foreman.start(
    { script: resolve(__dirname, "app.js") },
    { name: "process-name" }
);

Stop the given process

const { failed } = foreman.stop("stub");

Restart the given process

const { failed } = foreman.restart("stub");

Reload the given process

const { failed } = foreman.reload("stub");

Reset the given process

const { failed } = foreman.reset("stub");

Delete the given process

const { failed } = foreman.delete("stub");

Empty all log files

const { failed } = foreman.flush();

Reload all logs

const { failed } = foreman.reloadLogs();

Ensure pm2 daemon has been launched

const { failed } = foreman.ping();

Update in-memory pm2

const { failed } = foreman.update();

Show the status for the given process

import { ProcessState } from "@typeskrift/foreman";

foreman.status("process-name-or-id");

Determine if the given process is online

foreman.isOnline("process-name-or-id");

Determine if the given process is stopped

foreman.isStopped("process-name-or-id");

Determine if the given process is stopping

foreman.isStopping("process-name-or-id");

Determine if the given process is waiting

foreman.isWaiting("process-name-or-id");

Determine if the given process is launching

foreman.isLaunching("process-name-or-id");

Determine if the given process has errored

foreman.isErrored("process-name-or-id");

Determine if the given process is one-launch

foreman.isOneLaunch("process-name-or-id");

Determine if the given process has entered an unknown state

foreman.isUnknown("process-name-or-id");

Determine if the given process exists

foreman.has("process-name-or-id");

Determine if the given process does not exist

foreman.missing("process-name-or-id");

Conclusion

Programmatically working with PM2 doesn’t have to be a pesky task that ends you in Callback Hell as long as you get creative and avoid the built-in tools for programmatic use. PM2 is a great tool to manage your processes and almost all of its CLI outputs are easy to parse.


More about typescript, pm2 & process

npm vs. yarn vs. pnpm