Reputation: 395
I am trying to control a server using nodeJS. I can correctly stop and start the server using nodeJS but sometimes, the server could fail to start because of some unexpected errors. These errors will be outputted to console. Based on these errors I would like to perform various things. To get these errors, I used the spawn function like so:
spawn('./startEngine.sh &> test.txt',[] , options);
But I seem to be getting a ENOENT error
Error: spawn ./startEngine.sh &> test.txt ENOENT
at exports._errnoException (util.js:1026:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
at onErrorNT (internal/child_process.js:359:16)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
I have also tried to include the 'output to file' option as args for the spawn function but when I do this, it seems to ignore it completely and outputs to console anyways.
spawn('./startEngine.sh',['&>', 'test.txt'] , options);
Should I be doing something differently, if not then are there any other ways of getting the output of a process into a text file? thank you in advance!
Upvotes: 0
Views: 43
Reputation: 1270
You could listen to stdout and stderr and write the file yourself...
const child = spawn('./startEngine.sh',[] , options);
const ws = fs.createWriteStream('test.txt');
child.stdout.on('data', data => {
ws.write(data);
});
child.stderr.on('data', data => {
ws.write(`stderr: ${data}`);
});
child.on('close', code => {
ws.write(`process exited with code ${code}`);
ws.end();
});
child.on('error', err => {
ws.write('error encountered', err.message);
console.dir(err);
});
Upvotes: 1