View nodejs running scripts - node.js

Im using nightwatch and cucumberjs to make automated tests. After the execution I send a mail report of the status of the tests, but sometimes, when the tests completes it get stuck and doesnt send the mail report. I believe there is something inside my tests that doesn't close correctly but I don't know how I can see what is still running.
How can I display a list of the current processes that are still executing?
EDIT: Sorry for the confusion, by processes, I mean inside my nodejs program, not windows process

Linux:
Open termial.
Run the top command.
Windows:
Open task manager.
View running processes.

For Linux -
ps -ef | grep node
This will match all the processes with keyword node in it.
You can replace node with appropriate process name.
For Windows -
tasklist | FIND "node"

Related

NodeJS: How to read stdout and stderr of a child process spawned by an explicitly spawned process?

I've recently been working on a on-demand build server. The build server is a NodeJS/Express REST API, which essentially wraps the Angular CLI and associated build tooling for completing a custom on-demand Angular application.
Everything is working as expected end to end, but I'd like to be able to get more granular with the status reporting, as Angular builds can be quite time consuming when factoring in 2 very large parts of the process
The two longest running parts of the process:
the npm install (generally automatically kicked off via the ng-new schematic from the default #angular/schematics collection)
the actual ng build command.
2 is easy to address, as I am spawning that process (ng build --prod) via child_process.spawn() directly.
1 has proven a bit more complicated, as the long running npm install process is actually kicked off internally to the default Angular ng-new schematic/command. So, if my thinking is correct, this is essentially a explicitly spawned child process (my spawned ng new) which is internally spawning npm install.
One work around that I've come up with is to pass in the --skip-install arg to ng new, which will prevent the internal npm install process from being kicked off by the Schematic. By doing this, I can then manually kick off npm install via child_process.spawn() and directly observe the stdout and stderr streams.
I'm curious if anyone knows of a way to spy on the stderr and stdout streams from the 'npm install' that's kicked off inside of my explicitly spawned ng new command?
Thanks!
If you're running on Linux you can use strace to spy on the ouput of another process.
strace -p7835 -e trace= -e write=3
See this answer for more details.
You can invoke strace from node of course, using spawn. To find the pid of the npm process (which is actually a node process btw), you would need to get the process tree using the ps command. There is already a node module that does this: ps-tree, which also appears to be cross platform.
For alternatives of strace on Windows, check this discussion. I would go for Process Monitor from Sysinternals.

Jenkins won't kill node.exe?

I've got a Jenkins job set up to run a node.js server in the background, perform some tests on it (through a batch script, using Nightwatch), and then kill off the node server using the TaskKill batch command. Here's the command line script I have for the build:
START /B node ../app.js
runtests.bat
taskkill /F /IM node.exe
The build runs and passes, but it never seems to kill node. At the end of the console output I get:
Process leaked file descriptors. See https://jenkins.io/redirect/troubleshooting/process-leaked-file-descriptors for more information
And I can see the node.exe process still running in my Task Manager.
If I run the same commands in my own command prompt it works fine, and kills node. It's just that Jenkins doesn't seem to execute that last command at all.
Any ideas? Am I maybe taking the wrong approach altogether?
Well I managed to get it working by installing the Hudson Post Build Task plugin, and just killing node in a post-build command. Still not sure why it wasn't working before though.

node.js attach REPL to script

How do I start a node.js script and still be able to execute commands into the terminal ? I am looking for a node.js REPL that is also there for my custom script, so that I can inspect/log the state of my program for instance.
This is something similar to this JVM question, but for node.js.
I have tried node -i server.js without results. Do I need to have custom code in my script or is it feasible without that ? I saw this post, but it requires custom code, which I'd like to avoid.
Also, bonus points for reattaching a node script launched by an init script (I can see it in the process list : node -i server.js).
You can start a repl loop from within your program
http://nodejs.org/api/repl.html
Does the other way round work for you?
Start the REPL and then load the script and then execute your commands. Use load to load your script.
Inside REPL, try
.load server.js

How to set up a bash script to run in the background anytime the linux ubuntu server is running

I have written up a simple bash script that will copy the newest image from my ip camera into a directory, rename the file and delete the old file. The script loops every 10 seconds.
I want to have this script start running in the background and run continuously all the time that the server is up.
I understand the part about adding a & to the end of the command will cause it to run in the background.
Is init.d the best place to execute this?
I am running ubuntu server.
This sort of thing is normally done by service scripts, which you would find under /etc/init.d. Depending on the version, that might be a "System V init script", or one of the systemd scripts.
A simple service script of the sort you are asking about would start automatically (based on comments in the script's header that tell what run-levels it would use), create a file under /var/run telling what process-id the script uses (to allow killing it), and run the copying in a loop, calling sleep 10 to space the timing as indicated.
A typical service script should implement "start", "stop", "restart" and "status". Not all do, but there is rarely a good reason to not do this.
On my (Debian) system, there is a README file in the directory which is a good introduction to the topic. There are several tutorials available for the topic. Here are a few:
Linux: How to write a System V init script to start, stop, and restart my own application or service
Writing a Linux Startup Script
Manage System Startup and Boot Processes on Linux with Upstart

How can I run a node.js server

I'm new to node and have many things unclear.
Like, for php, I just need a index.php file on server's root dir and it can work by itself.
However, for a node.js file, we need to "node" command it in terminal right?
So what if we close that terminal? How can I keep it running to accept my requests?
You are correct in saying that the 'node' command will start a node process with whatever script you supply to it.
As far as keeping it running, there are several ways to do it. There are plenty of CLI libraries that will help you. For example, this one is called Forever
If you are using linux, you can simply run the node process as a background task:
node server.js &
To run node without terminal, you might want to check out one of these modules depending on your platform:
node-mac
node-windows
node-linux

Resources