[英]How to run executable file in Ubuntu as a command or service (with Node.js child_process.spawn())?
First, similar questions, no answers: 首先,类似的问题,没有答案:
Node.js child_process.spawn() fail to run executable file Node.js child_process.spawn()无法运行可执行文件
node.js child_process.spawn ENOENT error - only under supervisord node.js child_process.spawn ENOENT错误-仅在监督下
I have an executable file with .linux
extension. 我有一个扩展名为.linux
的可执行文件。 It is http server. 它是http服务器。
service.linux service.linux
I can run it like this: 我可以这样运行:
$ ./service.linux
2018/01/11 18:32:56 listening on port 8080
But since it is not a command, I cannot start it as a spawned process: 但是由于它不是命令,所以无法将其作为生成的进程启动:
let cp = spawn('service.linux', [], { cwd: __dirname });
The errors I get is: 我得到的错误是:
service.linux: command not found
ERROR: Error: spawn service.linux ENOENT
How can I run it as a command? 如何将其作为命令运行? Or should I use some other command to run it, like: 还是我应该使用其他命令来运行它,例如:
$ start service.linux
UPD: UPD:
$ file service.linux
service.linux: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
UPD: UPD:
It needs absolute path: 它需要绝对路径:
const path = require('path');
let cp = spawn(path.resolve(__dirname, `service.linux`), [], { cwd: __dirname });
Try using exec
and also write ./
before the name of the binary: 尝试使用exec
并在二进制名称前也写./
:
const { exec } = require("child_process");
exec("./service.linux", (err, data) => {
if (err) return console.log(err);
console.log(data);
});
Assuming the file is in the same directory as the script. 假设文件与脚本位于同一目录中。
The error ENOENT
means "Error No Entry" so it basically doesn't find the command. 错误ENOENT
表示“没有错误输入”,因此基本上找不到命令。
That's why we specify "./"
. 这就是为什么我们指定"./"
的原因。 That way it will handle it as a path. 这样,它将把它当作一条路径来处理。
这是路径问题,节点无法找到service.linux文件,使用绝对路径,问题将得到解决
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.