Reputation: 583
I wrote a little server in node js, that I start as service when booting
My /etc/init.d/lightserver looks like this:
# Actions
case "$1" in
start)
# START
/usr/bin/lightserver
;;
stop)
# STOP
;;
restart)
# RESTART
;;
esac
exit 0
I want to be able to stop the server properly with the command "/etc/init.d/lightserver stop". Without using of kill or killall.
How to implement this function into my server?
var i = 0;
var connect = require('connect');
var sleep = require('sleep');
var exec = require('child_process').exec;
var j = 1;
var server = connect().
use(function(req, res) {
i++;
res.write('Hello World! ' + i);
res.end();
let code = 83028 + j;
j = 1 - j;
exec("./433Utils/RPi_utils/codesend " + code, function(error, stdout, stderr) {
console.log(stdout);
});
}).listen(64084);
console.log("Server has started and is listening to http://localhost:64084/");
Upvotes: 2
Views: 414
Reputation: 2846
I think you are looking for something like this?
process.on('SIGTERM', () => {
// some other cleanup code
server.close();
});
What this does is intercept the kill command and close the server gracefully instead of just suddenly stopping the process, without warning.
To end the process gracefully you can just use the kill <PID>
command, since SIGTERM
is the default value. But if you want to be sure the sigterm is used you can always choose to use kill -SIGTERM <PID>
, which will send a SIGTERM
The default signal for kill is TERM. Use -l or -L to list available signals. Particularly useful signals include HUP, INT, KILL, STOP, CONT, and 0. Alternate signals may be specified in three ways: -9 -SIGKILL -KILL. Negative PID values may be used to choose whole process groups; see the PGID column in ps command output. A PID of -1 is special; it indicates all processes except the kill process itself and init.
http://linuxcommand.org/lc3_man_pages/kill1.html
Upvotes: 1