NodeJS에서 콘솔명령 실행하고 그 리턴값을 파일로 저장하기 > 개발자팁

개발자팁

개발과 관련된 유용한 정보를 공유하세요.
질문은 QA에서 해주시기 바랍니다.

NodeJS에서 콘솔명령 실행하고 그 리턴값을 파일로 저장하기 정보

node.js NodeJS에서 콘솔명령 실행하고 그 리턴값을 파일로 저장하기

본문


const { exec } = require('child_process');
const fs = require('fs');
const cmd = 'your-command'; // 실행할 cmd 명령어
const outputFile = 'output.txt'; // 저장할 파일명
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.error(`Error executing command: ${error}`);
return;
}
const outputData = stdout || stderr; // 출력 데이터 선택 (stdout 또는 stderr)

fs.appendFile(outputFile, outputData, 'utf8', function(err) {
if (err) {
console.error(`Error writing file: ${err}`);
return;
}

console.log(`Output saved to ${outputFile}`);
});
});

추천
0
  • 복사

댓글 0개

© SIRSOFT
현재 페이지 제일 처음으로