I was wondering if it was possible to get process metadata using pm2 inside my node application.
Yes, you can get any information from pm2 inside your app. below will return all running process list. For further details, you can check pm2-api
var pm2 = require('pm2');
app.use('/all_process_list', function(req,res){
pm2.connect(function(err) {
if (err) {
console.error(err);
process.exit(2);
}
pm2.list(function(err, processDescriptionList) {
console.log(processDescriptionList)
res.json ({process_list:processDescriptionList})
pm2.disconnect(); // Disconnects from PM2
});
});
});
Related
Okay, so I'm in the process of creating a Minecraft panel. I have a windows service, and that will call my express server. The problem is, that when the computer tries to start the process, it goes on infinitely because the server will run for eternity. I need this to be changed so that when it runs it will start up the web server, make sure it's running, and then finish the task and forget the webserver exists, but leave it running. How would this be possible? Many thanks :)
Update: After a little bit of though I'm going to refine my question to this: How can I start a process and not wait for it to finish in C#/nodejs(either work)
Edit: Lol i am refreshing this page like every two microseconds.... Its 4 AM for me so yeah :D brain - broke
Answer: So I figured out how to fix it. I used pm2 which can start a process and not wait for it to finish. We have a start.js and a stop.js which can start it. If you want to build something like it here is the code I used.
//Start.js
const pm2 = require('pm2');
pm2.connect((err) => {
if (err) {
console.log(err);
}
});
pm2.start({
name: 'webserver',
script: "webserver.js",
}, (err) => {
if (err) {
console.log(err)
} else {
console.log('Webserver started');
pm2.disconnect();
process.exit(0)
}
})
//Stop.js
const pm2 = require('pm2');
//Stop the webserver
pm2.stop('webserver', (err) => {
if (err) {
console.log(err);
} else {
console.log('Webserver stopped');
pm2.disconnect();
process.exit(0)
}
});
I have an express application running and I am using PM2 to keep it alive and restart when there is any error in the system. PM2 logs the error and restarts which is perfectly fine.
But I need to notify the user who submitted this with the error message. Is there any way or event or trigger which is activated when the restart happens or when the error is being written to the log file so that I can try to capture that and notify the actual user?
I implemented as per suggestion below,
var pm2 = require('pm2');
pm2.connect(function(err) {
if (err) {
console.error(err);
process.exit(2);
}
console.log("connected to pm2")
pm2.launchBus(function(err, bus) {
bus.on('log:err', function(e) {
// Send emails
console.log("error in pm2 send email");
});
});
});
I can see "connected to pm2" printed in output.log but I can't see "error in pm2 send email" printed anywhere. But I see an error log that I am triggering to force the error scenario in error.log.
1) You can use alert system from Keymetrics
2) You can connect to pm2 bus and catch errors:
// alerts.js
var pm2 = require('pm2')
pm2.launchBus(function(err, bus) {
bus.on('log:err', function(e) {
// Send emails
});
});
I want to log the process id of a fork when I log an action, but I am not sure how to access the variable where it's held.
I couldn't find if the process id is held inside a global variable and also identify which one is running when the express routes are being executed, so I am in a kind of a bind here.
router.get('/', auth, async (req, res) => {
try {
const user = await User.findById(req.user.id).select('-password');
res.json(user);
customLogger.log(`action A performed by process ${}`);
} catch(err) {
console.error(err.message);
res.status(500).send('Server error');
}
});
I just want to get something like: "action A performed by process J85T".
It is there in the process object: process.pid.
I have a NodeJs application running in the following directory
First Application's Path '/users/user1/projects/sampleProject' which is running at 3000 port.
Second Application's Path '/users/user1/demoProjects/demo1' which is going to run at 5000 port on triggering the router function from first application.
The second NodeJs application is not yet started(It will run at port 5000). It need to run independently on hitting a router function in the first NodeJs Application which is running on port 3000 ie(http://localhost:3000/server/startServer). I'm new to NodeJs child processes, Kindly correct me if i'm wrong. And suggest me a right way to do it. Thanks
Start another node application using node.js?
I have tried it like below
// First NodeJs application
import { exec } from "child_process";
router.get('/startServer', async (req, res, next) => {
console.log("Initiated request")
let startServerInstance = 'cd "/users/user1/demoProjects/demo1" && npm run dev'; // path for the second NodeJs application
console.log("Server instance path => " + startServerInstance)
try {
// exec from child process, Spawns a shell then executes the command within that shell
let child = exec(startServerInstance, function (err, stdout, stderr) {
if (err) throw err;
else {
console.log("result ")
res.json({
status: 'success'
});
}
});
} catch (error) {
res.json({
status: 'error',
message: error
});
}
});
The above code executes the command and triggered the second application to run in background but it doesn't return anything. Either error or success result.
You need to use stout and stderror to check other server logs. Also your code is not correct. If you use if without {} it will not go to else statement. That is why you don't see 'result' text in console.
import {
exec
} from "child_process";
router.get('/startServer', async (req, res, next) => {
console.log("Initiated request")
let startServerInstance = 'cd "/users/user1/demoProjects/demo1" && npm run dev'; // path for the second NodeJs application
console.log("Server instance path => " + startServerInstance)
try {
// exec from child process, Spawns a shell then executes the command within that shell
let child = exec(startServerInstance, function(err) {
if (err) throw err;
console.log("Server started");
});
child.stdout.on('data', (data) => {
// this is new server output
console.log(data.toString());
});
child.stderr.on('data', (data) => {
// this is new server error output
console.log(data.toString());
});
res.json({
status: 'success'
});
} catch (error) {
res.json({
status: 'error',
message: error
});
}
});
Child process callback is only called once the process terminates. If the process keeps running, callback is not triggered.
Explained here - https://nodejs.org/docs/latest-v10.x/api/child_process.html#child_process_child_process_exec_command_options_callback
I have an express application running and I am using PM2 to keep it alive and restart when there is any error in the system. PM2 logs the error and restarts which is perfectly fine.
But I need to notify the user who submitted this with the error message. Is there any way or event or trigger which is activated when the restart happens or when the error is being written to the log file so that I can try to capture that and notify the actual user?
I implemented as per suggestion below,
var pm2 = require('pm2');
pm2.connect(function(err) {
if (err) {
console.error(err);
process.exit(2);
}
console.log("connected to pm2")
pm2.launchBus(function(err, bus) {
bus.on('log:err', function(e) {
// Send emails
console.log("error in pm2 send email");
});
});
});
I can see "connected to pm2" printed in output.log but I can't see "error in pm2 send email" printed anywhere. But I see an error log that I am triggering to force the error scenario in error.log.
1) You can use alert system from Keymetrics
2) You can connect to pm2 bus and catch errors:
// alerts.js
var pm2 = require('pm2')
pm2.launchBus(function(err, bus) {
bus.on('log:err', function(e) {
// Send emails
});
});