Created
November 5, 2019 12:52
-
-
Save gughog/9fb1cb4e09ee2fa1db4400b7cc82dd8f to your computer and use it in GitHub Desktop.
Executando comandos bash (Linux) com Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { exec } = require('child_process'); // Responsavel por executar os comandos | |
const fs = require('fs'); | |
const cm = { update: 'sudo apt update -y', upgrade: 'sudo apt upgrade -y' }; | |
// Criando um timestamp: | |
const dd = new Date(); | |
const timestamp = dd.toLocaleString('pt-BR', {hour12: false}).replace(' ', '_'); | |
// Função para escrever num arquivo: | |
const writeToLog = (filename, content) => { | |
fs.writeFile(filename, content, (err) => { | |
if (err) console.log(err) | |
else console.log(`File was written as ${filename}!`) | |
}) | |
} | |
// Execução em si: | |
exec(cm.update, (err, stdout, stderr) => { | |
if (err) { | |
console.log(err); | |
} else { | |
writeToLog(`${timestamp}.txt`, stdout) // Cria um arquivo com o nome de um timestamp.txt | |
console.log('STDOUT:', stdout ); // Loga os pacotes atualizados | |
console.log('STDERR:', stderr ); // Loga quaisquer erros | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment