How to use the connect function from pm2
Find comprehensive JavaScript pm2.connect code examples handpicked from public code repositorys.
pm2.connect is a function used to connect to a local or remote PM2 daemon programmatically.
GitHub: ryanlelek/Raneto
35 36 37 38 39 40 41 42 43 44 45
program .command('start') .description('Start Raneto Service') .action(() => { pm2.connect(() => { pm2.start( { script: path.normalize(path.join(__dirname, '..', 'server.js')), exec_mode: 'fork',
+ 3 other calls in file
GitHub: Taschendieb/autodeploy
50 51 52 53 54 55 56 57 58 59
.command(['list', 'ls'], 'list all deployments', (yargs) => {}, async (argv) => { const deployments = await config.listDeployments() console.table(deployments,["name","url","path"]) }) .command(['autostart', 'as'], 'register server to automatically start', (yargs) => {}, async (argv) => { pm2.connect(function (err) { if (err) { console.error(err); process.exit(1); }
+ 9 other calls in file
How does pm2.connect work?
pm2.connect is a function provided by the PM2 module in Node.js that establishes a connection to the PM2 daemon. The pm2 module allows developers to manage and monitor Node.js applications using a process manager called PM2. When pm2.connect is called, it creates a new connection to the PM2 daemon process. This connection is used to send commands to the PM2 daemon, such as starting or stopping a process, restarting a process, or getting status information about running processes. Once the connection is established, the PM2 API is available to use in the code. If a connection error occurs, pm2.connect will throw an error. It's important to handle this error in order to gracefully exit the application or take other appropriate action. It's worth noting that pm2.connect is not necessary if the PM2 daemon is already running and being managed via the command line interface. In that case, the pm2 command can be used directly to manage the PM2 daemon and its managed processes. However, pm2.connect is useful when a developer wants to programmatically manage PM2 from within a Node.js application.
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
const pm2 = require("pm2"); // Connect to the PM2 daemon pm2.connect(function (err) { if (err) { console.error(err); process.exit(2); } // Do something with PM2, like list processes pm2.list(function (err, processes) { if (err) { console.error(err); pm2.disconnect(); // Disconnect from the PM2 daemon process.exit(2); } console.log(processes); // Disconnect from the PM2 daemon pm2.disconnect(function () { console.log("Disconnected from PM2"); }); }); });
This example connects to the PM2 daemon using pm2.connect, lists all running processes using pm2.list, and then disconnects from the daemon using pm2.disconnect.
pm2.restart is the most popular function in pm2 (219 examples)