Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send command to running node process, get back data from inside the app

Tags:

node.js

I start a node.js app per commandline in linux. I see the app running, e.g. by entering "top". Is there a way to send some command to the running app (maybe to the pid?) and get back info from inside it (maybe listen for some input and return requested info)?

like image 630
Hans Avatar asked Dec 27 '22 00:12

Hans


2 Answers

Use repl module. There are examples in the doco doing exactly what you need: run JS in the context of your application and return output.

like image 200
Andrey Sidorov Avatar answered May 13 '23 11:05

Andrey Sidorov


One simple solution is to use process signals. You can define a handler for a signal in your program to output some data to the console (or write to a file or to a database, if your application is running as a service not attached to a terminal you can see):

process.on('SIGUSR1', function() {
  console.log('hello. you called?');
});

and then send a signal to it from your shell:

kill --signal USR1 <pid of node app.js>

This will invoke the signal handler you have defined in your node.js application.

like image 26
Munim Avatar answered May 13 '23 13:05

Munim