Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用nodejs执行shell命令_Node.js - Fatal编程技术网

Node.js 使用nodejs执行shell命令

Node.js 使用nodejs执行shell命令,node.js,Node.js,我得到了这个代码片段 function run_cmd(cmd, args, cb ) { console.log('[run_cmd] cmd = ' + cmd); var spawn = require('child_process').spawn; var child = spawn(cmd, args); var resp = ''; child.stderr.on('data', function(data) { console.log('err = ' + da

我得到了这个代码片段

function run_cmd(cmd, args, cb ) {
  console.log('[run_cmd] cmd = ' + cmd);
  var spawn = require('child_process').spawn;
  var child = spawn(cmd, args);
  var resp = '';
  child.stderr.on('data', function(data) { console.log('err = ' + data) });
  child.stdout.on('data', function (buffer) { resp += buffer.toString() });
  child.stdout.on('close', function(code) {
    console.log('[run_cmd] exit code = ' + code + ', resp = ' + resp);
    cb (resp);
  });
}
当我执行像ls这样的简单命令时,它工作得很好

但是当我试图执行这个命令时,它没有得到execute

 libreoffice --headless --convert-to pdf /home/username/input_file.rtf --outdir ~/tmp
由此

run_cmd( "libreoffice", ["--headless", "--convert-to pdf", file_path, "--outdir ~/tmp"], function(text) { console.log (text) });
我得到了退出代码=false,回答为空。我如何调试这个

命令未执行,输出文件未创建


当我手动粘贴到终端时,该命令工作正常。

此问题是由路径中的瓷砖引起的,请参见。您是否可以尝试手动展开波浪线并重新运行函数?

如果发生错误,您应该侦听stderr。@VuDang,因此我在'data'上添加了'child.stderr.on',functiondata{console.log'err='+data};`还是什么都没有。没有任何错误,我也算出来了。回到这里,看到了你的答案+是否有node.js函数来扩展shell变量或tilda?
run_cmd( "libreoffice", ["--headless", "--convert-to pdf", file_path, "--outdir ~/tmp"], function(text) { console.log (text) });