Accessing logs of a node js code running as a server - node.js

There is a node js code running on a linux server as a process.
I have the process id of the process using
ps -aef | grep node
which gives
amit 20897 1 0 Sep26 ? 03:07:06 node energyMonitor/newBroker.js
I want to access the stdout,stderr of this process, to view the what console.log() statements are priniting. I tried with
tail -f /proc/20897/fd/1
but of no use, can someone help me with this?
Thanks

Related

How to get specific node PID in Node-Red?

I am using Node-Red V2.2.2. I would like to restart an specific node of the flow after an error is triggered in it.
I have managed to restart the full flow getting node-red process id. After modifying: settings.js in my .node-red folder:
functionGlobalContext: {
// os:require('os'),
'pid': process.pid
},
I am able to get general process pid from a function node:
var General_pid = context.global.pid
And kill and restart the global process from an Exec node sending General_pid in msg.payload :
Being comando.sh:
#!/bin/bash
taskkill //PID $1 //F
sleep 4
node-red
But i am unable to do this with specific nodes inside the node-red flow.
Almost all info i have searched relied on Status node to get node specific pid,
but in my case, this is the Status node structure (no PID in there):
I have also tried to get PID based on status.source.id using:
RED.nodes.getNode(id);
But RED.nodes is undefined (altough RED is defined, but it only shows functions on print)
Any idea on how to be able to get the node PID to kill it and restart it? I could do it from an Exec node, but if there is an easier way even better.
You don't.
Nodes are not separate processes that can be restarted independently of Node-RED. (While some nodes may fork a new process, e.g. a python script, Node-RED has no access to this and it is all handled inside the node in question)
You have 2 choices:
You can trigger a restart of the deployed flow by making a HTTP call to the /flows Admin API with the header set to reload. Assuming the node with the failure is well written then it should restart cleanly.
Restart all of Node-RED as you are already

Node too slow if ran from terminal

I've noticed that for some reason when I try to run a node script in terminal it's much slower than same statement in PHP no matter what.
test.js:
console.log("test");
duration # ubuntu:
aherne#aherne-NUC8i7INH:~$ time node test.js
real 0m0,317s
user 0m0,303s
sys 0m0,016s
test.php:
echo "test\n";
duration # ubuntu:
aherne#aherne-NUC8i7INH:~$ time php test.php
real 0m0,096s
user 0m0,021s
sys 0m0,028s
Am I doing something wrong or node is that slow? DISCLAIMER: I'm a beginner in node (obviously)
What are you trying to benchmark here? The startup times of each executable?
Obviously node is far more complex and has a higher startup time.
It is a very bad test since this is not what happens when php or node processes a user request - as the process will already be running.
Reason was ancient node version (10.19.0). After upgrading to v16.14.0 everything worked instantly:
aherne#aherne-NUC8i7INH:~$ time node test.js
real 0m0,057s
user 0m0,045s
sys 0m0,013s

child_process is not executing on ec2 nodejs server

I have node server which is on an ec2 instance running Ubuntu.
I maintain code on Gitlab and push updates there.
The manual updating process is to simply pull the changes and restart the server which I want to automate and for that I am using Gitlab webhook and created a simple endpoint which executes:
childProcess.exec('git pull && bash deploy.sh',{ cwd: '/home/ubuntu/someXyzFolder' }, function(err, stdout, stderr){
if (err) {
return res.status(500).send(err);
}
res.status(200).send("OK");
});
So childProcess is not able to execute these commands as its sending 500 status with this error:
{"killed":false,"code":1,"signal":null,"cmd":"git pull && bash deploy.sh"}
several people have suffered from this problem and solved it by creating a swap due to low memory
I ran free command on my server which replied back
ubuntu#ip-xxx-xxx-xxx-xxx:/var/somefolder/somefolder$ free
total used free shared buff/cache available
Mem: 1007532 404276 381512 772 221744 452944
Swap: 0 0 0
According to this, I should be having enough ram right?
I am not sure whether Linux oom is killing it or something else.
Do let me know your thoughts on it.
-Thanks
EDIT: here is my deploy.sh
git pull
npm install
gulp build
sudo killall forever
sudo killall node
rm /removeTheSymlinkOfTheCurrentBuildFolder
ln -s /makeaNewFolderWithCurrentTimeStamp/`ls -ltr /some | tail -n 1 | awk {' print $9 '}` /andMakeItCurrent
sudo forever /currentBuildFolder/server.js &
So, I finally fixed the issue.
The ssh was generated with when I logged in as the user ubuntu and then stored in Gitlab.
I was opening my 2nd server ( which would kill and restart my main server ) using sudo which was creating the issue.
Now everything seems to be working fine.

Stopping nodejs process start by npm?

I am in the process of creating some scripts to deploy my node.js based application, via continuous integration and I am having trouble seeing the right way to stop the node process.
I start the application via a start-dev.sh script:
#!/bin/sh
scripts_dir=`dirname $0`
cd "${scripts_dir}/"..
npm start &
echo $! > app.pid
And then I was hoping to stop it via:
#!/bin/sh
scripts_dir=`dirname $0`
cd "${scripts_dir}/"..
echo killing pid `cat app.pid`
kill -9 `cat app.pid`
The issue I am finding is that npm is no longer running at this point, so the pid isn't useful to stop the process tree. The only workaround I can think of at this point is to skip npm completely for launch and simply call node directly?
Can anyone suggest an appropriate way to deal with this? Is foregoing npm for launching a good approach, in this context?
Forever can do the process management stuff for you.
forever start app.js
forever stop app.js
Try to avoid relying on npm start outside of development, it just adds an additional layer between you and node.
just use supervisor example conf is like
[program:long_script]
command=/usr/bin/node SOURCE_FOLDER/EXECUTABLE_JAVASCRIPT_FILE.js
autostart=true
autorestart=true
stderr_logfile=/var/log/long.err.log
stdout_logfile=/var/log/long.out.log
where
SOURCE_FOLDER is the folder for your project
EXECUTABLE_JAVASCRIPT_FILE the file to be run
you can check the post here

How can I get source code of nodejs from running app

I have accidentally delete source code of nodejs application, but this application is running, so how can I get source code back from running app?
I hope source code has been cached in some directory.
I was able to recover the full file by attaching the debugger (as TGrif suggested).
To actually recover the code:
Use setBreakpoint('app.js', 10), where 10 is a line of the code you know will be ran over again in the running process
Say pause, then next until it's paused on the script you want to recover.
Finally, say list(5000), where 5000 is an arbitrarily long number of lines to list.
You will now have your full script printed out, albeit with line numbers at the front, but you can use a site like this to remove them.
Hope this helps anyone who encounters this unique issue in the future, as this took me a couple hours to figure out.
There is maybe a way to retrieve some of your source code with the Nodejs debugger.
Assuming Linux OS, you need to get the process id of your application:
$ ps -e | grep node
Next you entering your app in debug mode with something like that:
$ kill -s USR1 PID
where PID is the pid of your node app.
Then your start the debug console:
$ node debug -p PID
If you have an app console, you'll see:
Starting debugger agent.
Debugger listening on port 5858
In your debug console you should see a debug prompt and you can get available commands with:
debug> help
I am able to show some of the running app source with the list command:
debug> list(NUMBER_OF_LINE)
where NUMBER_OF_LINE is the number of source code line you want to display.
I'm not sure this is a one shot try for you or not because my source code was not deleted.
Hope you can get some results.

Resources