Для завершения процесса сигнал отправляется в него. Для большинства этих сигналов процесс может поймать такой сигнал, сделать некоторый материал (высвободите сетевые средства, распечатайте до свидания), и (большую часть времени) завершите.
То, что я описал, является путем к процессу, который будет уничтожен корректно, или обычно. Процесс может также завершиться, если он закончил свое задание или использование "x" кнопки на программе GUI. Но так как это - связанный сайт о Unix, я предполагаю иначе.
Отметьте, два сигнала не могут быть пойманы процессом, SIGKILL и SIGSTOP. Завершение SIGKILL, рассматривают не нормальное завершение.
Нормальное завершениеkillall -SIGHUP firefox
killall -SIGINT firefox
Не корректное завершениеkillall -SIGKILL firefox
Команда икры возвращает читаемый поток, таким образом, необходимо будет передать это по каналу к перезаписываемому потоку, чтобы сделать что-то полезное.
// the filed module makes simplifies working with the filesystem
var filed = require('filed')
var path = require('path')
var spawn = require('spawn')
var outputPath = path.join(__dirname, 'out.txt')
// filed is smart enough to create a writable stream that we can pipe to
var writableStream = filed(outputPath)
var cmd = path.join(__dirname, 'my_script.bash')
var args = [] // you can option pass arguments to your spawned process
var child = spawn(cmd, args)
// child.stdout and child.stderr are both streams so they will emit data events
// streams can be piped to other streams
child.stdout.pipe(writableStream)
child.stderr.pipe(writableStream)
child.on('error', function (err) {
console.log('an error occurred')
console.dir(err)
})
// code will be the exit code of your spawned process. 0 on success, a positive integer on error
child.on('close', function (code) {
if (code !== 0) {
console.dir('spawned process exited with error code', code)
return
}
console.dir('spawned process completed correctly at wrote to file at path', outputPath)
})
Необходимо будет установить зарегистрированный модуль для выполнения примера выше
npm install filed
process.stdout и process.stderr являются оба перезаписываемыми потоками, таким образом, можно передать вывод по каналу порожденной команды непосредственно к консоли также
var path = require('path')
var spawn = require('spawn')
var cmd = path.join(__dirname, 'my_script.bash')
var args = [] // you can option pass arguments to your spawned process
var child = spawn(cmd, args)
// child is a stream so it will emit events
child.stderr.pipe(process.stderr)
child.stdout.pipe(process.stderr)
child.on('error', function (err) {
console.log('an error occurred')
console.dir(err)
})
// code will be the exit code of your spawned process. 0 on success, a positive integer on error
child.on('close', function (code) {
if (code !== 0) {
console.dir('spawned process exited with error code', code)
return
}
console.dir('spawned process completed correctly at wrote to file at path', outputPath)
})