how to get the list of process - node.js

I am playing around with node and just installed it on my machine. Now I want to get a list of processes running on my machine so I can see whether Apache is running, MySQL is started, etc? How can I do that? I just have very basic code in my js file. I don't even know where to begin on this.
Here is my code:
var http = require('http');
http.createServer(function(request, response){
response.writeHead(200);
response.write("Hello world");
console.log('Listenning on port 1339');
response.end();
}).listen(8080);

As far as I know there isn't a module (yet) to do this cross-platform. You can use the child process API to launch tools that will give the data you want. For Windows, just launch the built-in tasklist process.
var exec = require('child_process').exec;
exec('tasklist', function(err, stdout, stderr) {
// stdout is a string containing the output of the command.
// parse it and look for the apache and mysql processes.
});

See ps-node
To get a list of processes in node:
var ps = require('ps-node');
ps.lookup({
command: 'node',
arguments: '--debug',
}, function(err, resultList ) {
if (err) {
throw new Error( err );
}
resultList.forEach(function( process ){
if( process ){
console.log( 'PID: %s, COMMAND: %s, ARGUMENTS: %s', process.pid, process.command, process.arguments );
}
});
});

ps-list is a better node package for the job, it is working on Linux, BSD, and Windows platforms too.
const psList = require('ps-list');
psList().then(data => {
console.log(data);
//=> [{pid: 3213, name: 'node', cmd: 'node test.js', cpu: '0.1'}, ...]
});

You can also use current-processes which lists all the processes.
https://www.npmjs.com/package/current-processes
The result includes name,pid,cpu and memory used by process.
You can also sort the result and limit the number of processes.
The result looks like this:
[ Process {
pid: 31834,
name: 'atom',
cpu: 84,
mem: { private: 19942400, virtual: 2048, usage: 0.4810941724241514 } }]

Solution for unix-like systems:
const child_process = require('child_process');
const displayProcessBy = (pattern) => {
let command = `ps -aux | grep ${pattern}`;
child_process.exec(command, (err, stdout, stdin) => {
if (err) throw err;
console.log(stdout);
});
}
Examples usage and results
displayProcessBy("nodejs");
setivol+ 7912 0.0 0.0 12732 2108 ? S 10:56 0:00 grep nodejs
setivol+ 12427 0.0 0.0 669552 712 pts/3 Tl Dec16 0:00 nodejs
setivol+ 14400 0.0 0.0 669552 644 pts/2 Tl Dec15 0:00 nodejs
setivol+ 14412 0.0 0.0 670576 224 pts/3 Tl Dec16 0:00 nodejs
setivol+ 14567 0.0 0.0 669552 436 pts/3 Tl Dec15 0:00 nodejs
setivol+ 14911 0.0 0.0 669552 0 pts/3 Tl Dec15 0:00 nodejs
setivol+ 15489 0.0 0.0 669552 712 pts/3 Tl Dec16 0:00 nodejs
setivol+ 15659 0.0 0.0 669520 0 pts/3 Tl Dec16 0:00 nodejs --harmony
setivol+ 16469 0.0 0.0 669520 704 pts/3 Tl Dec16 0:00 nodejs --harmony
setivol+ 20514 0.0 0.0 669552 664 pts/2 Tl Dec15 0:00 nodejs
displayProcessBy("python2")
setivol+ 8012 0.0 0.0 4336 712 ? S 10:58 0:00 /bin/sh -c ps -aux | grep python2
setivol+ 8014 0.0 0.0 12728 2240 ? S 10:58 0:00 grep python2
Testing environment
$ uname -a
Linux localhost 3.16.0-4-amd64 #1 SMP Debian 3.16.36-1+deb8u2 (2016-10-19) x86_64 GNU/Linux
$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 8.6 (jessie)
Release: 8.6
Codename: jessie

Seems there isn't any direct methods
but below videos might help.
Monitoring Long Running Processes with Node. (http://www.youtube.com/watch?v=zamGXhjim00)
CPU usage in real time with node.js & socket.io (http://www.youtube.com/watch?v=bPYgx9V6qAk)

Use the command line tools rather than node scripts.
ps -aef | grep node
This would list the visual studio code helpers and .nvm env processes. We can exclude them using
ps -aef | grep node | grep -v "Visual Studio" | grep -v ".nvm"
Additional tip to kill all the listed processes:
killall node

Related

Virtual size in docker is always increasing with puppeteer

I am developing a little program with nodejs and Puppeteer which goal is to generate a certain amount of PDF files. The program is working with the bluebird module in order to achieve concurrency. The problem is that the use of Physical and virtual memory does not stop to increase. The size of all the generated documents is approximately 25GB, but the used memory in the docker container is much bigger:
pdf-generator 64.2GB (virtual 68.4GB)
We generate the PDF with Puppeteer in this way:
async function generatePDF(browser, num) {
const page = await browser.newPage();
try {
const pdfUrl = pdfPathURL(num);
await page.goto(pdfUrl, {
waitUntil: ["domcontentloaded", "networkidle0", "load"],
timeout: 300000,
});
// await page.waitForLoadState({ waitUntil: "domcontentloaded" });
const buffer = await page.pdf({
format: "A4",
printBackground: true,
landscape: true,
preferCSSPageSize: true,
});
await page.close()
return buffer.toString("base64");
} catch (error) {
let messageError = error;
console.log(messageError);
return "";
}
} finally {
await page.close();
}
}
[EDIT]
This is the code that opens the Chromium instance. One per request:
async function generatePDFFile(id) {
let pdfFile;
let errorGeneration = "";
const browser = await launchPuppeteer();
try {
if (!browser) {
errorGeneration = `Error getting Chromium instance`;
}
if (!browser.isConnected()) {
errorGeneration = `Error connecting to Chromium`;
}
pdfFile = await generatePDFPage(browser, id);
console.log(`PDF file generated of id:`, id);
} catch (error) {
errorGeneration = error;
console.log("errorGeneration: ", error);
}
finally {
await browser.close();
}
return { id, pdf: pdfFile, error: errorGeneration };
}
const puppeteerParams = {
headless: true,
args: [
"--disable-gpu",
"--disable-dev-shm-usage",
"--disable-setuid-sandbox",
"--no-sandbox",
"--font-render-hinting=none",
'--single-process',
'--no-zygote'
],
};
The top command in the container
Tasks: 19 total, 1 running, 18 sleeping, 0 stopped, 0 zombie
%Cpu(s): 45.4 us, 11.0 sy, 0.0 ni, 42.5 id, 0.3 wa, 0.0 hi, 0.9 si, 0.0 st
MiB Mem : 15862.5 total, 1418.4 free, 9686.2 used, 4757.9 buff/cache
MiB Swap: 2048.0 total, 1291.0 free, 757.0 used. 4953.4 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
34855 myuser 20 0 1124.8g 222708 149664 S 81.0 1.4 0:02.74 chrome
34810 myuser 20 0 1124.8g 212544 146712 S 77.3 1.3 0:02.61 chrome
34918 myuser 20 0 1124.8g 184764 141052 S 36.7 1.1 0:01.10 chrome
31 myuser 20 0 706628 142100 33080 S 35.7 0.9 2:19.22 node
34968 myuser 20 0 1124.7g 136748 112832 S 9.3 0.8 0:00.28 chrome
35062 myuser 20 0 1124.7g 138452 114036 S 9.0 0.9 0:00.27 chrome
35013 myuser 20 0 1124.8g 137448 113456 S 8.3 0.8 0:00.25 chrome
60 myuser 20 0 965160 103512 33040 S 7.7 0.6 0:22.25 node
35106 myuser 20 0 1124.6g 105352 89208 S 5.0 0.6 0:00.15 chrome
8 myuser 20 0 630596 51892 32908 S 0.7 0.3 0:04.14 node
1 myuser 20 0 2420 524 452 S 0.0 0.0 0:00.05 sh
19 myuser 20 0 707412 57724 35064 S 0.0 0.4 0:02.26 npm start
30 myuser 20 0 2420 580 512 S 0.0 0.0 0:00.00 sh
48 myuser 20 0 705296 53336 34512 S 0.0 0.3 0:01.71 npm run example
59 myuser 20 0 2420 524 456 S 0.0 0.0 0:00.00 sh
24495 myuser 20 0 1124.7g 140500 116068 S 0.0 0.9 0:00.31 chrome
31812 myuser 20 0 4100 3376 2948 S 0.0 0.0 0:00.05 bash
31920 myuser 20 0 7012 3420 2848 R 0.0 0.0 0:00.03 top
34415 myuser 20 0 1124.8g 138368 114276 S 0.0 0.9 0:00.28 chrome
There are 8 chrome processes because I am concurrently doing 8 requests. The memory continues increasing

Linux User NameSpaces

I am experimenting with user namespaces using Go on Linux. The thing that I cannot figure out is that although am setting the uid and gid mappings when creating the namespace it still identifies as the nobody user when I launch the binary using sudo but when I launch it using the normal user everything works fine. For reference please see my code below
...
cmd := exec.Command("/bin/sh")
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.SysProcAttr = &syscall.SysProcAttr{
Cloneflags: syscall.CLONE_NEWUSER,
UidMappings: []syscall.SysProcIDMap{
{
ContainerID: 0,
HostID: 1000,
Size: 1,
},
},
GidMappings: []syscall.SysProcIDMap{
{
ContainerID: 0,
HostID: 1000,
Size: 1,
},
},
}
cmd.Run()
....
...
From the host I can confirm that indeed the user and group mappings were successful. The current pid is 87751
sudo cat /proc/87751/uid_map
0 1000 1
sudo cat /proc/87751/gid_map
0 1000 1
But when I run the binary after building
go build -o user_n
sudo ./user_n
sh-5.0$ whoami
nobody
sh-5.0$ id
uid=65534(nobody) gid=65534(nobody) groups=65534(nobody) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
But when I run the binary using the normal user it works as expected
./user_n
sh-5.0# whoami
root
sh-5.0# id
uid=0(root) gid=0(root) groups=0(root),65534(nobody) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
While running the binary using the normal user is an option I would like to know why running using sudo does not give the expected results. Any pointers will be greatly appreciated.
More info
Fedora 31
Kernel 5.3.11-100.fc29.x86_64
go version go1.14.3 linux/amd64
In the first case, you are running as root user (through sudo) for which there is no mapping specified in the child user namespace. Hence, the resulting "nobody" id.
In the second case, you run the program as user id 1000 for which the mapping says : 1000 becomes root in the child user namespace. Hence, the resulting "root" id.

NodeJs runs again to reach Number of Processes limit in cPanel

I'm using shared hosting cPanel service.
I have ran a NodeJs application on server and everything is ok.
but after server backup runs in every day one NodeJs process add to the running processes and I can see these processes on terminal.
I have killed the processes but they came back after server backup!!!
I have write a code to exit from processes that run when port is busy:
tcpPortUsed.check(port, '127.0.0.1')
.then(function(inUse) {
console.log(`Port ${port} usage: `+inUse);
if (inUse) {
process.exit();
} else {
app.listen(port, () =>
console.log(`server is running on port: ${port}`)
);
}
}, function(err) {
console.error('Error on check:', err.message);
});
but the problem has remained...
this is the terminal after I run ps aux:
user 5435 1.0 0.0 9916 1820 pts/0 S 16:50 0:00 /bin/bash -l
user 5752 0.0 0.0 49844 1684 pts/0 R+ 16:50 0:00 ps aux
user 13658 0.0 0.1 1155556 36060 ? Sl 05:15 0:06 lsnode:/home1/user/node-server/
user 32346 0.0 0.1 1091012 37944 ? Sl Jul04 0:05 lsnode:/home1/user/node-server/

Trouble with VisualEditor in mediawiki

Hi I've installed mediawiki 1.26.2 with the extensión Visual Editor, nodejs and parsoid, the question is that when I start parsoid, it seems every process is working right but the the configuration of parsoid and visualeditor, I can't see any editor in my wiki.
I describe below all my configurations, how I start parsoid, the processes of parsoid involved and the configurations lines in localsettings of media wiki configuration file.
/etc/init.d/parsoid2 start-end script:
#!/bin/bash
#
# chkconfig: 35 90 12
# description: Foo server
#
# Get function from functions library
#. /etc/init.d/functions
# Start the service PARSOID
SCRIPT_PATH="/usr/lib/parsoid/src/bin/server.js"
DAEMON="/usr/bin/node $SCRIPT_PATH"
DAEMON_ARGS=""
start() {
#initlog -c "echo -n Starting PARSOID server: "
ulimit -n 64000
/usr/bin/node /usr/lib/parsoid/src/bin/server.js >> /var/log/parsoid/parsoid.log 2>&1 &
### Create the lock file ###
#touch /var/lock/subsys/parsoid
success $"PARSOID server startup"
echo
}
# Restart the service PARSOID
stop() {
#initlog -c "echo -n Stopping PARSOID server: "
pkill -f server.js
### Now, delete the lock file ###
rm -f /var/lock/subsys/parsoid
echo
}
### main logic ###
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status parsoid_nodejs.sh
;;
restart|reload|condrestart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart|reload|status}"
exit 1
esac
exit 0
processes of parsoid involved after I run /etc/init.d/parsoid2 start
root#vscj016mlinuxserver:~# ps -ef | grep parsoid
root 2244 1 0 08:21 pts/0 00:00:00 /usr/bin/node /usr/lib/parsoid/src/bin/server.js
root 2251 2244 0 08:21 pts/0 00:00:00 /usr/bin/nodejs /usr/lib/parsoid/src/bin/server.js
root 2252 2244 0 08:21 pts/0 00:00:00 /usr/bin/nodejs /usr/lib/parsoid/src/bin/server.js
root 2258 2244 0 08:21 pts/0 00:00:00 /usr/bin/nodejs /usr/lib/parsoid/src/bin/server.js
root 2264 2244 0 08:21 pts/0 00:00:00 /usr/bin/nodejs /usr/lib/parsoid/src/bin/server.js
root 2437 2023 0 08:36 pts/0 00:00:00 grep --color=auto parsoid
root#vscj016mlinuxserver:~#
the Localsetting.js parsoid configuration file:
exports.setup = function(parsoidConfig) {
// Set your own user-agent string
// Otherwise, defaults to "Parsoid/<current-version-defined-in- package.json>"
//parsoidConfig.userAgent = "My-User-Agent-String";
// Configure Parsoid to point to your MediaWiki instance.
parsoidConfig.setMwApi({
// The "prefix" is the name given to this wiki configuration in the
// (deprecated) Parsoid v1 API.
prefix: 'localhost', // optional
// The "domain" is used for communication with Visual Editor
// and RESTBase. It defaults to the hostname portion of
// the `uri` property below, but you can manually set it
// to an arbitrary string.
domain: 'localhost', // optional
// This is the only required parameter:
// the URL of you MediaWiki API endpoint.
uri: 'http://localhost/mediawiki/api.php',
// To specify a proxy (or proxy headers) specific to this prefix
// (which overrides defaultAPIProxyURI). Alternatively, set `proxy`
// to `null` to override and force no proxying when a default proxy
// has been set.
/*
proxy: {
uri: 'http://my.proxy:1234/',
headers: { 'X-Forwarded-Proto': 'https' } // headers are optional
}
*/
});
The configuration for VisualEditor at /var/www/HTML/mediawiki/Localsettings.php:
require_once "$IP/extensions/VisualEditor/VisualEditor.php";
wfLoadExtension ( 'VisualEditor' );
$wgDefaultUserOptions['visualeditor-enable'] = 1;
$wgDefaultUserOptions['minordefault'] = 1;
$wgHiddenPrefs[] = 'visualeditor-enable';
$wgVisualEditorParsoidURL = 'http://localhost:8000';
$wgVirtualRestConfig['modules']['parsoid'] = array('url' => 'http://localhost:8000', 'domain' => 'localhost', 'prefix' => 'localhost');
$wgSessionsInObjectCache = true;
$wgVirtualRestConfig['modules']['parsoid']['forwardCookies'] = true;
Please ensure that your parsoid version match your Visual Editor version, there is a chance that you should use old-way to configure Visual Editor:
$wgVisualEditorParsoidURL = 'http://127.0.0.1:8000';
$wgVisualEditorParsoidPrefix = 'localhost';
If you see the button "Edit source" on your pages only, make sure the Visual Editor is enabled for some name spaces in `LocalSettings.phpExample:
$wgVisualEditorNamespaces = array(NS_MAIN, NS_USER);
Source: https://www.mediawiki.org/wiki/Extension:VisualEditor#Complete_list_of_configuration_options
Check if Visual Editor is enabled in the User Preferences and you see the enabled name spaces as well

Gulp inject: Segmentation Fault

I have manjaro linux and I installed nodejs and npm from official repositories
node version: v6.2.1
npm version: 3.9.5
gulp.task('inject', ['scripts', 'styles'], function () {
var injectStyles = gulp.src([
path.join(conf.paths.tmp, '/serve/app/**/*.css'),
path.join('!' + conf.paths.tmp, '/serve/app/vendor.css')
], { read: false });
var injectScripts = gulp.src([
path.join(conf.paths.src, '/app/**/*.module.js'),
path.join(conf.paths.src, '/app/**/*.js'),
path.join('!' + conf.paths.src, '/app/**/*.spec.js'),
path.join('!' + conf.paths.src, '/app/**/*.mock.js'),
])
.pipe($.angularFilesort()).on('error', conf.errorHandler('AngularFilesort'));
var injectOptions = {
ignorePath: [conf.paths.src, path.join(conf.paths.tmp, '/serve')],
addRootSlash: false
};
return gulp.src(path.join(conf.paths.src, '/*.html'))
.pipe($.inject(injectStyles, injectOptions))
.pipe($.inject(injectScripts, injectOptions))
.pipe(wiredep(_.extend({}, conf.wiredep)))
.pipe(gulp.dest(path.join(conf.paths.tmp, '/serve')));
});
When I run:
$ gulp serve
It display me:
[21:36:37] Using gulpfile /home/jics/Documents/Proyect/gulpfile.js
[21:36:37] Starting 'config'...
[21:36:37] Starting 'styles'...
[21:36:37] Finished 'config' after 236 ms
[21:36:37] Starting 'scripts'...
[21:36:37] gulp-inject 30 files into index.scss.
[21:36:39] Finished 'styles' after 2.36 s
[21:36:40] all files 211.97 kB
[21:36:40] Finished 'scripts' after 2.84 s
[21:36:40] Starting 'inject'...
[21:36:40] gulp-inject 1 files into index.html.
Segmentation fault
My coredumctl display me:
PID: 29909 (gulp)
UID: 1000 (jics)
GID: 1000 (jics)
Signal: 11 (SEGV)
Timestamp: lun 2016-06-13 18:03:49 CLT (3h 37min ago)
Command Line: gulp
Executable: /usr/bin/node
Control Group: /user.slice/user-1000.slice/session-c2.scope
Unit: session-c2.scope
Slice: user-1000.slice
Session: c2
Owner UID: 1000 (jics)
Boot ID: 857cb1e6f9134beb91d172fc85f05f36
Machine ID: 50f47fe8f80b4f92829b61933e8b309e
Hostname: jics-pc
Coredump: /var/lib/systemd/coredump/core.gulp.1000.857cb1e6f9134beb91d172fc85f05f36.29909.1465
Message: Process 29909 (gulp) of user 1000 dumped core.
Stack trace of thread 29909:
#0 0x00002080c7ca188c n/a (n/a)
The only why I can run gulp serve with success is deleting de follow line from de configuration file .pipe($.inject(injectScripts, injectOptions))
Well I did do it with n
$ sudo npm install -g n
$ sudo n stable
install : node-v6.2.1
mkdir : /usr/local/n/versions/node/6.2.1
fetch : https://nodejs.org/dist/v6.2.1/node-v6.2.1-linux-x64.tar.gz
######################################################################## 100,0%
installed : v6.2.1
Finally I do $ gulp serve and run perfectly

Resources