Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store nodejs exec stdout in var

Tags:

linux

node.js

I would like make this function return the result of the exec command in a var. How would I go about doing this?

// System Serial Number
function systemSerialNumber(response) {
  console.log("Request handler 'systemSerialNumber' was called.");
  exec("dmidecode -t 1 | grep 'Serial Number:' | cut -d: -f2 | sed -e 's/^[ \t]*//g'", function (error, stdout, stderr) {
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write(stdout);
    response.end();
  });
}
like image 386
Nathan Rockhold Avatar asked Nov 14 '22 10:11

Nathan Rockhold


1 Answers

From http://nodejs.org/docs/v0.3.1/api/all.html#child_process.exec describes how to get the output of a command asynchronously. But it sounds like you want to get the results synchronously so you can return them, in which case, you're probably more interested in this SO question: node.js execute system command synchronously

like image 69
broofa Avatar answered Dec 21 '22 09:12

broofa