永遠支持退出信號定製與--killSignal選項:
--killSignal Support exit signal customization (default is SIGKILL),
used for restarting script gracefully e.g. --killSignal=SIGTERM
以上是用於指示永遠與殺信號停止腳本永遠已經開始。要根據腳本退出的方式選擇性地停止運行腳本,您需要使用forever-monitor。
首先,你的腳本需要發送一個特定的信號,當你想永遠地而不是重新啓動它。下面是我們的script.js:
setTimeout(function() {
console.log('hello');
//process.kill(process.pid, 'SIGKILL'); // this will cause forever to restart the script.
setTimeout(function() {
process.kill(process.pid, 'SIGTERM'); // this will cause forever to stop the script.
}, 1000);
}, 2000);
然後,我們需要一個額外的腳本永遠顯示器(姑且稱之爲腳本monitor.js):
var forever = require('forever-monitor');
var child = new (forever.Monitor)('script.js', {
max: 10,
silent: false,
args: []
});
child.on('restart', function() {
console.error('Forever restarting script for ' + child.times + ' time');
});
child.on('exit:code', function(code) {
console.error('Forever detected script exited with code ' + code);
if (143 === code) child.stop(); // don't restart the script on SIGTERM
});
child.start();
現在可以運行的腳本。 js通過運行:node script-monitor.js
爲方便起見,node.js中的信號事件here is a list。
我讀過這個,但我不知道如何使用它。當我'process.exit()',我可以傳入一個整數。我無法通過SIGTERM或SIGKILL。另外,killSignal實際上做了什麼?如果我將它設置爲SIGTERM,將會停止發生什麼樣的默認行爲?什麼新行爲出現? – Shawn 2015-03-13 16:10:04
我想最好的解決方案取決於你什麼時候不想永遠重啓過程。例如,如果你只是想在你故意「處理」時重新啓動腳本。然後你可以指定'--killSignal = SIGTERM'如果你需要更多的自定義的情況,那麼你可能需要考慮永久監視器,並用永遠監視器編寫一個節點腳本,情況下重新啓動/不重新啓動,然後運行您的節點腳本。 – 2015-03-13 16:20:57
我編輯了我的問題,使其更清晰。我已經嘗試過使用'--killSignal = SIGTERM'和'process.exit()',但腳本仍然會重新啓動... – Shawn 2015-03-13 16:38:48