简体   繁体   English

如何在Ubuntu中作为命令或服务运行可执行文件(使用Node.js child_process.spawn())?

[英]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.

相关问题 外壳程序命令child_process.spawn(command,[args],[options])node.js - shell command to child_process.spawn(command, [args], [options]) node.js Node.js:捕获`child_process.spawn`的STDOUT - Node.js: Capture STDOUT of `child_process.spawn` node.js child_process.spawn no stdout除非'inherit' - node.js child_process.spawn no stdout unless 'inherit' 如何在node.js中清除child_process.spawn()上的子进程 - How to clean child processes on child_process.spawn() in node.js 如何在Windows上正确使用node.js child_process.spawn()重定向? - How to correctly use node.js child_process.spawn() redirection on Windows? 如何确保node.js child_process.spawn将错误消息写入控制台 - How to ensure node.js child_process.spawn will write error messages to the console 如何通过Node.js中的child_process.spawn()将长字符串传递给/ dev / stdin? - How do you pipe a long string to /dev/stdin via child_process.spawn() in Node.js? 如何通过child_process.spawn()执行本地安装的Node.js应用程序? - How to execute locally installed Node.js application by child_process.spawn()? 如何在Node.js中通过child_process.spawn()将长字符串传递给? - How do i pipe a long string to via child_process.spawn() in Node.js? node.js - 如何调整 child_process.spawn() 的标准输出缓冲区大小(又名 highWaterMark)? - node.js - How to adjust the the stdout buffer size (aka highWaterMark) of child_process.spawn()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM