Reputation:
I'm working at raspberry pi and node version is "node-v0.10.28-linux-arm-pi". I'm have run the below code.
'use strict' var util = require(‘util’); var config = require(‘./config.js’); var fs = require(‘fs’); var pidInfo = util.format('process id:%d’, process.pid); util.log(pidInfo); process.title = pidInfo; process.on('exit’, function () { var path = '/home/pi/test/message_1.txt’; fs.writeFileSync(path, new Date().toString()); }); process.on('SIGTERM’, function () { var path = '/home/pi/test/message.txt’; fs.writeFileSync(path, new Date().toString()); process.exit(0); }); //var exec = require(‘child_process’).exec; //exec(util.format('sudo shutdown -h %s’, '18:25'), function (err) { //if (err) //console.log(err); //}); process.stdin.resume();
When I call the linux command "kill process id" to kill the process,event SIGTERM and exit is triggered. But when it run code "exec(util.format('sudo shutdown -h......" Raspberry pi will shutdown directly and I'm unable to capture event SIGTERM and exit. I don't know why. May could you to help me please? Thank you.
Upvotes: 0
Views: 812
Reputation: 1
I assume you start the script engine in a Terminal.
During shutdown, the controlling Terminal is closed and a SIGHUP and a SIGCONT are sent to the script process. The default action for SIGCONT just continues execution. However, the default action for SIGHUP terminates the process before the scripted SIGTERM handler can be executed.
After implementing a SIGHUP handler or setting the SIGHUP handler to SIG_IGN the SIGTERM handler should just execute fine during shutdown.
This example script demonstrates the reception of all 3 signals (SIGHUP, SIGCONT and SIGTERM).
'use strict'
var fs = require('fs');
var path = '/home/pi/test/message.txt';
fs.writeFileSync(path, new Date().toString() + ': Start\n');
process.on('exit', function () {
fs.appendFileSync(path, new Date().toString() + ': exit\n');
});
process.on('SIGTERM', function () {
fs.appendFileSync(path, new Date().toString() + ': SIGTERM\n');
// delay exit() for academic purposes only
// without delay SIGHUP and SIGCONT can be missed, which
// would be OK for the application
setTimeout( function () {
process.exit(0);
}, 5000);
});
// Imlementing SIGHUP handler prevents process termination
// by default SIGHUP handler
process.on('SIGHUP', function () {
fs.appendFileSync(path, new Date().toString() + ': SIGHUP\n');
});
// SIGCONT handler for academic purposes only
// The default handler SIGCONT would not do any harm
process.on('SIGCONT', function () {
fs.appendFileSync(path, new Date().toString() + ': SIGCONT\n');
});
var exec = require('child_process').exec;
exec('sudo shutdown -r');
process.stdin.resume();
Example output from file /home/pi/test/message.txt:
Mon May 02 2016 07:43:52 GMT+0000 (UTC): Start
Mon May 02 2016 07:44:52 GMT+0000 (UTC): SIGTERM
Mon May 02 2016 07:44:52 GMT+0000 (UTC): SIGCONT
Mon May 02 2016 07:44:52 GMT+0000 (UTC): SIGHUP
Mon May 02 2016 07:44:52 GMT+0000 (UTC): SIGHUP
Mon May 02 2016 07:44:57 GMT+0000 (UTC): exit
For further information on this subject see the section Orphaned process groups on http://www.win.tue.nl/~aeb/linux/lk/lk-10.html#ss10.2.
Upvotes: 0
Reputation: 25918
That's because systemd
sends SIGKILL
immediately after SIGTERM
(see shutdown.c
log_info("Sending SIGTERM to remaining processes...");
broadcast_signal(SIGTERM, true, true);
log_info("Sending SIGKILL to remaining processes...");
broadcast_signal(SIGKILL, true, false);
You may also want to read How much time before SIGKILL
Upvotes: 0