Asyncronously execute shell script from node.js app
const exec = require('child_process').exec
var yourExecCmd = 'ls -ltr'
// Function to exec shell cmd with callback
function execWithCallback() {
this.execCommand = function(cmd, callback) {
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`)
return
}
console.log('stdout:', stdout)
console.log('stderr:', stderr)
callback(stdout)
})
}
}
var execWithCallback = new execWithCallback()
execWithCallback.execCommand(yourExecCmd, function (result) {
console.log('result')
console.log(result)
}) |