node.js を使用し、/dev/tty ファイルを開いてシリアル ポートから入力を読み取ります。コマンドを送信し、コマンドの結果を読み取ります。すべてのデータを読み取って解析したら、ストリームを閉じたいと思います。 . データマーカーの終わりまでにデータの読み取りが完了したことを知っています。ストリームを閉じると、プログラムが終了しないことがわかりました。
以下は、私が見ているものの例ですが、 /dev/random を使用してゆっくりとデータを生成しています (システムがあまり機能していないと仮定します)。私が見つけたのは、ストリームが閉じられた後にデバイスがデータを生成すると、プロセスが終了することです。
var util = require('util'),
PassThrough = require('stream').PassThrough,
fs = require('fs');
// If the system is not doing enough to fill the entropy pool
// /dev/random will not return much data. Feed the entropy pool with :
// ssh <host> 'cat /dev/urandom' > /dev/urandom
var readStream = fs.createReadStream('/dev/random');
var pt = new PassThrough();
pt.on('data', function (data) {
console.log(data)
console.log('closing');
readStream.close(); //expect the process to terminate immediately
});
readStream.pipe(pt);
更新:1
この問題に戻って、別のサンプルがあります。これは pty を使用するだけで、ノード repl で簡単に再現できます。2 つの端末にログインし、以下の createReadStream の呼び出しでノードを実行していない端末の pty を使用します。
var fs = require('fs');
var rs = fs.createReadStream('/dev/pts/1'); // a pty that is allocated in another terminal by my user
//wait just a second, don't copy and paste everything at once
process.exit(0);
この時点でノードはハングアップし、終了しません。これは10.28にあります。