Production process manager for Node.JS applications with a built-in load balancer.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Core process lifecycle operations for managing Node.js applications with PM2. Provides starting, stopping, restarting, reloading, and deleting managed processes with support for multiple execution modes, clustering, and advanced configuration options.
Establish and manage connections to the PM2 daemon process.
/**
* Connect to PM2 daemon or launch a new one
* @param callback - Called when connection is established or fails
*/
function connect(callback: (err: Error) => void): void;
/**
* Connect to PM2 daemon with daemon mode control
* @param noDaemonMode - If true, PM2 will not run as daemon and will die when script exits
* @param callback - Called when connection is established or fails
*/
function connect(noDaemonMode: boolean, callback: (err: Error) => void): void;
/**
* Disconnect from PM2 daemon
*/
function disconnect(): void;Usage Examples:
const pm2 = require('pm2');
// Standard connection (daemon mode)
pm2.connect((err) => {
if (err) {
console.error('Failed to connect:', err);
process.exit(2);
}
console.log('Connected to PM2');
// Perform operations...
pm2.disconnect();
});
// No daemon mode (PM2 dies when script exits)
pm2.connect(true, (err) => {
if (err) throw err;
// PM2 will not persist after this script ends
});Start new processes with comprehensive configuration options.
/**
* Start a process from options object
* @param options - Process configuration options
* @param callback - Called with error and process information
*/
function start(options: StartOptions, callback: (err: Error, proc: Proc) => void): void;
/**
* Start a process from script path
* @param script - Path to script file
* @param callback - Called with error and process information
*/
function start(script: string, callback: (err: Error, proc: Proc) => void): void;
/**
* Start a process with script and options
* @param script - Path to script file
* @param options - Process configuration options
* @param callback - Called with error and process information
*/
function start(script: string, options: StartOptions, callback: (err: Error, proc: Proc) => void): void;
/**
* Start a process from JSON configuration file
* @param jsonConfigFile - Path to JSON configuration file
* @param callback - Called with error and process information
*/
function start(jsonConfigFile: string, callback: (err: Error, proc: Proc) => void): void;Usage Examples:
// Start with options object
pm2.start({
script: 'app.js',
name: 'my-app',
instances: 'max',
exec_mode: 'cluster',
env: {
NODE_ENV: 'production',
PORT: '3000'
},
watch: true,
ignore_watch: ['node_modules', 'logs'],
max_memory_restart: '1G',
log_date_format: 'YYYY-MM-DD HH:mm:ss Z'
}, (err, proc) => {
if (err) throw err;
console.log('Process started:', proc[0].pm2_env.name);
});
// Start from script path only
pm2.start('server.js', (err, proc) => {
if (err) throw err;
console.log('Server started');
});
// Start with script and options
pm2.start('worker.js', {
name: 'background-worker',
instances: 2,
cwd: '/app/workers'
}, (err, proc) => {
if (err) throw err;
console.log('Worker processes started');
});
// Start from ecosystem file
pm2.start('ecosystem.config.js', (err, proc) => {
if (err) throw err;
console.log('Ecosystem loaded');
});Stop running processes while keeping them in the PM2 process list.
/**
* Stop a process but leave metadata in PM2 list
* @param process - Process name, ID, or "all" for all processes
* @param callback - Called when process is stopped
*/
function stop(process: string | number, callback: (err: Error, proc: Proc) => void): void;Usage Examples:
// Stop by name
pm2.stop('my-app', (err, proc) => {
if (err) throw err;
console.log('Process stopped');
});
// Stop by ID
pm2.stop(0, (err, proc) => {
if (err) throw err;
console.log('Process 0 stopped');
});
// Stop all processes
pm2.stop('all', (err, proc) => {
if (err) throw err;
console.log('All processes stopped');
});Restart processes with optional configuration updates.
/**
* Stop and restart a process
* @param process - Process name, ID, or "all" for all processes
* @param callback - Called when process is restarted
*/
function restart(process: string | number, callback: (err: Error, proc: Proc) => void): void;Usage Examples:
// Restart specific process
pm2.restart('my-app', (err, proc) => {
if (err) throw err;
console.log('Process restarted');
});
// Restart all processes
pm2.restart('all', (err, proc) => {
if (err) throw err;
console.log('All processes restarted');
});Perform zero-downtime rolling restarts for cluster mode applications.
/**
* Zero-downtime rolling restart (cluster mode only)
* @param process - Process name, ID, or "all" for all processes
* @param callback - Called when reload is complete
*/
function reload(process: string | number, callback: (err: Error, proc: Proc) => void): void;
/**
* Zero-downtime rolling restart with options
* @param process - Process name, ID, or "all" for all processes
* @param options - Reload configuration options
* @param callback - Called when reload is complete
*/
function reload(process: string | number, options: ReloadOptions, callback: (err: Error, proc: Proc) => void): void;
interface ReloadOptions {
/** Update environment variables from process.env before reloading */
updateEnv?: boolean;
}Usage Examples:
// Standard reload
pm2.reload('cluster-app', (err, proc) => {
if (err) throw err;
console.log('Zero-downtime reload completed');
});
// Reload with environment update
pm2.reload('cluster-app', { updateEnv: true }, (err, proc) => {
if (err) throw err;
console.log('Reloaded with updated environment');
});
// Reload all cluster processes
pm2.reload('all', (err, proc) => {
if (err) throw err;
console.log('All cluster processes reloaded');
});Remove processes from PM2 management completely.
/**
* Stop process and remove from PM2 list
* @param process - Process name, ID, or "all" for all processes
* @param callback - Called when process is deleted
*/
function delete(process: string | number, callback: (err: Error, proc: Proc) => void): void;Usage Examples:
// Delete specific process
pm2.delete('old-app', (err, proc) => {
if (err) throw err;
console.log('Process deleted from PM2');
});
// Delete by ID
pm2.delete(3, (err, proc) => {
if (err) throw err;
console.log('Process 3 deleted');
});
// Delete all processes
pm2.delete('all', (err, proc) => {
if (err) throw err;
console.log('All processes deleted');
});Save and restore process configurations across system restarts.
/**
* Save current process list to dump file
* @param callback - Called when dump is complete
*/
function dump(callback: (err: Error, result: any) => void): void;
/**
* Restore processes from saved dump file
* @param callback - Called when resurrection is complete
*/
function resurrect(callback: (err: Error, result: any) => void): void;Usage Examples:
// Save current process list
pm2.dump((err, result) => {
if (err) throw err;
console.log('Process list saved to dump file');
});
// Restore saved processes
pm2.resurrect((err, result) => {
if (err) throw err;
console.log('Processes restored from dump');
});
// Typical workflow for persistence
pm2.connect((err) => {
if (err) throw err;
// Start your applications
pm2.start('app.js', { name: 'my-app' }, (err) => {
if (err) throw err;
// Save the configuration
pm2.dump((err) => {
if (err) throw err;
console.log('Configuration saved');
pm2.disconnect();
});
});
});
// On system restart or in startup script
pm2.connect((err) => {
if (err) throw err;
// Restore all saved processes
pm2.resurrect((err) => {
if (err) throw err;
console.log('All processes restored');
pm2.disconnect();
});
});System-level operations for daemon management and startup integration.
/**
* Kill PM2 daemon (stops all processes)
* @param callback - Called when daemon is killed
*/
function killDaemon(callback: (err: Error, processDescription: ProcessDescription) => void): void;
/**
* Setup system startup script
* @param platform - Target platform for startup script
* @param callback - Called when startup is configured
*/
function startup(platform: Platform, callback: (err: Error, result: any) => void): void;
type Platform = 'ubuntu' | 'centos' | 'redhat' | 'gentoo' | 'systemd' | 'darwin' | 'amazon';Usage Examples:
// Kill PM2 daemon
pm2.killDaemon((err, result) => {
if (err) throw err;
console.log('PM2 daemon killed');
});
// Setup startup script
pm2.startup('systemd', (err, result) => {
if (err) throw err;
console.log('Startup script configured:', result);
});interface StartOptions {
/** Process name for PM2 identification */
name?: string;
/** Script file path to execute */
script?: string;
/** Arguments passed to the script */
args?: string | string[];
/** Arguments passed to the interpreter */
interpreter_args?: string | string[];
/** Working directory for the process */
cwd?: string;
/** Environment variables */
env?: { [key: string]: string };
/** Number of instances (cluster mode) */
instances?: number;
/** Execution mode: 'fork' or 'cluster' */
exec_mode?: string;
/** Enable file watching for auto-restart */
watch?: boolean | string[];
/** Patterns to ignore when watching */
ignore_watch?: string[];
/** Merge cluster instance logs */
merge_logs?: boolean;
/** Stdout log file path */
output?: string;
/** Stderr log file path */
error?: string;
/** Log timestamp format */
log_date_format?: string;
/** PID file path */
pid?: string;
/** Auto restart on crash */
autorestart?: boolean;
/** Maximum restart attempts */
max_restarts?: number;
/** Memory restart threshold */
max_memory_restart?: number | string;
/** Minimum uptime before considering stable */
min_uptime?: number;
/** SIGKILL timeout in milliseconds */
kill_timeout?: number;
/** Delay between restarts */
restart_delay?: number;
/** Script interpreter */
interpreter?: string;
/** Wait for ready signal from process */
wait_ready?: boolean;
/** Force start even if already running */
force?: boolean;
/** Process namespace */
namespace?: string;
}
interface Proc {
name?: string;
pm_id?: number;
pid?: number;
status?: string;
restart_time?: number;
created_at?: number;
pm_uptime?: number;
unstable_restarts?: number;
}Install with Tessl CLI
npx tessl i tessl/npm-pm2