Attach to current process on nodejs over ubuntu server terminal - node.js

I have a process running over a ubuntu server. I only have access on terminal.
I made this script:
index.js
let i = 0;
setInterval( () => {
i++;
console.log(`try ${i}`);
}, 1000);
I run with: node index.js &
Now I open a new terminal and I want to see the result on console.log.
How can I do it?
New edit:
The principal idea is send a console.log in a terminal and, recovery this console.log in another terminal. This is the goal. Recovery the console log in another terminal. How can I do it?

Of top of my head, I'd suggest going for a combination of https://www.npmjs.com/package/commander and some sort of IPC - redis-based,*mq or plain https://www.npmjs.com/package/node-ipc
If you don't have wide variety of commands you'd need to send to that process, you can also get away using signals, for example: SIGUSR1, SIGUSR2.

Related

Nodejs HTTP.request different timeouts on different systems

I have 2 systems using Nodejs 16.19.0 - one on my local Windows 11 and another in a GKE pod running a Ubuntu 20 container.
In both systems, I open an interactive Nodejs prompt and run the following, pointing to some IP address that I know will NOT reply and cause a timeout on the client side:
$ node # open interactive nodejs prompt and run the following at once:
console.log(new Date());
req=http.request('http://x.x.x.x');
req.on('error', ()=>{}); // just to cleanup the output
req.on('close', () => { console.log('close ' + new Date()); });
I print the start and end dates to see how long it takes the client to close with the timeout.
In my local system, it takes ~20 seconds. In the other system, it takes 130 seconds. I didn't configure anything special in my Node. Why is there a difference?
Also, the consequence of this, is that when I also add a "timeout" handler for, say, 30 seconds, it will NOT fire on Windows and WILL fire in the pod...

How can I stop an Electron app from restarting itself after using app.relaunch?

I was playing around with the app.relaunch API that will restart my app when closing it.
I used it like so (vastly reduced example, so hopefully it works. Don't want to try running this again):
app.on("ready", () => {
const win = new BrowserWindow();
});
app.on("window-all-closed", () => {
app.relaunch();
app.quit();
});
This is a bit stupid, but now, I am stuck in a relaunch loop.
I tried force-closing all 5 Electron.exe processes that are running on my machine via Task Manager, but it keeps restarting.
How can I end this process?
I guess this was a bit obvious in retrospect, but I just removed the app.relaunch() statement, rebuilt the app, and then stopped it. Now it launches the new version which doesn't have the relaunch code in it.
You can also delete your main.js file if you're not packaged. If you are packaged, you could just delete your exe, so it can't find it upon relaunching.

Service Fabric node.js guest application express.js server EADDRINUSE

Not sure if this is a service fabric issue, or issue with node.js.
Basically this is my problem. I deploy the node.js application, it works fine. I redeploy the node application it fails to work, with the server returning EADDRINUSE. When I run netstat -an the port isn't in use. It's as if node is still running somewhere, some how, but not appearing in tasklist etc..
Anyone got any ideas?
Not entirely sure, but I believe this is because the server I was using (express.js), or rather node, was not shutting down and closing existing connections causing windows to think the ports are still in use. At least, that's how it seems.
I can not find it "officially" documented, but from this (quoted below) it reads SF sends SIGINT to the application to attempt to end it before killing it.
The following code appears to fix my issue:
var app = express();
var server = app.listen(17500);
if (process.platform === "win32") {
var rl = require("readline").createInterface({
input: process.stdin,
output: process.stdout
});
rl.on("SIGINT", function () {
process.emit("SIGINT");
}
}
process.on("SIGINT", function() {
server.close(function () {
process.exit(0);
});
});
For Linux nodes, I suppose you'd want to listen for "SIGTERM" as well.
I would like to know if there's any sort of remediation for this though, in the previously mentioned scenario the VMSS was completely unusable -- I could not deploy, nor run, a node web server. How does one restart the cluster without destroying it and recreating it? I now realise you can't just restart VMSS instances willy-nilly because service fabric completely breaks if you do that, apparently irrevocably
Rajeet Nair [RajeetN#MSFT]
Service Fabric also sends a Ctrl-C to service processes and waits for service to terminate. If the service doesn't terminate for 3 minutes, the process is killed.

how to change node.js variables from terminal

I'm trying to run a nodeJS service that contains some parameters, then once it is running, stores a lot of session variables.
My script shouldn't be restarted because of this, all these working variables should remain the same.
Is it possible to change some global variables inside the running nodejs script while it is running? I was hoping the node would actually be a console where I can run JS in it but it appear it is not, it only outputs the console.log() method and errors, there's no input.
I know I could create a method inside my script to change these variables, but there are many, and I dont want to create a function that can handle them all because this would be very insecure.
Is there something I've missed about the node console inside terminal?
In fine, the script will be running with 'forever'
What do you mean no input?
node.js can read from the terminal like any other technology.
You can look at: https://nodejs.org/api/readline.html
They have a simple example:
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("What do you think of Node.js? ", function(answer) {
// TODO: Log the answer in a database
console.log("Thank you for your valuable feedback:", answer);
rl.close();
});
I wrote a command line installer for our app using this.

Viewing node console log remotely

I have been building my first node app. In testing on my mac, I was able to view the console log output using terminal.
I'm now moving the app to a server but I still want to get a live dump of the console log. Yes, I can get this by SSH'ing into the server - start the app then watch the output. But, say my SSH connection to the server gets disconnected. After re-connecting to the server, how do I go about viewing the terminal output of that process?
One solution I came across was http://console.re - this looks ideal, however it comes with warnings not to use in a production environment. Coupled with the fact that it's public, I'm hesitant to use it.
Does anyone know of an alternative solution similar to console.re?
Thanks
You could try using a custom function that writes the output to a log file, as well as printing it on screen.
Something like this: (note that this won't accept multiple arguments)
var fs = require('fs');
module.exports = function(text) {
fs.writeFile('console.log', text, {
flag: 'a' // append
}, function(){}); // ignore the response
console.log(text);
};
Perhaps screen, tmux, or similar software might work for you.

Resources