s

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

[code]

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}`);
});
});

[/code]

|
댓글을 작성하시려면 로그인이 필요합니다.

개발자팁

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

+
분류 제목 글쓴이 날짜 조회
PHP 2년 전 조회 2,389
node.js 2년 전 조회 1,057
PHP 2년 전 조회 3,480
node.js 2년 전 조회 1,063
정규표현식 2년 전 조회 1,018
node.js 2년 전 조회 1,243
PHP 2년 전 조회 1,548
node.js 2년 전 조회 1,490
PHP 2년 전 조회 1,751
node.js 2년 전 조회 1,341
node.js 2년 전 조회 1,510
PHP 2년 전 조회 2,949
PHP 2년 전 조회 1,733
JavaScript 2년 전 조회 1,112
node.js 2년 전 조회 960
node.js 2년 전 조회 1,147
MySQL 2년 전 조회 1,188
MySQL 2년 전 조회 1,067
JavaScript 2년 전 조회 1,300
웹서버 2년 전 조회 4,548
PHP 2년 전 조회 1,337
PHP 2년 전 조회 1,764
기타 2년 전 조회 1,142
jQuery 2년 전 조회 2,536
2년 전 조회 940
2년 전 조회 1,555
2년 전 조회 1,413
PHP 2년 전 조회 1,295
MySQL 2년 전 조회 1,078
PHP 2년 전 조회 1,166