Erlang 的操作系统进程管理

发布于 2024-11-03 06:28:09 字数 241 浏览 1 评论 0 原文

从 Erlang 进行操作系统进程管理的最佳方法是什么?我需要能够生成子操作系统进程,写入子进程的标准输入,从标准输出和标准错误读取,发送终止信号,并获得退出代码的通知。我不认为这对于 erts 中的内容是可能的。

我了解 erlexec,但我不知道它是否稳定或是否有人实际使用它在愤怒中。还有其他选择吗,或者这是最好的选择?

What's the best way to do OS process management from Erlang? I need to be able to spawn child OS processes, write to the child's stdin, read from both stdout and stderr, send kill signals, and get notified of exit codes. I don't see that this is possible with what's in erts.

I know about erlexec, but I don't know if it's stable or if anyone is actually using it in anger. Are there any other options, or is that the best?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撑一把青伞 2024-11-10 06:28:09

Port 程序是实现此目的的最佳方法。端口程序是安全的,不会损害 Erlang VM,如果它们崩溃或行为不当,可以像任何其他 Erlang 进程一样被杀死并重新启动。

端口驱动程序是不同的,如果它们行为不当可能会导致整个 Erlang VM 崩溃。

Port programs are the best way to do this. Port programs are safe and won't harm the Erlang VM, and if they crash or mis-behave can be killed and restarted like any other Erlang process.

Port Drivers are different and if they mis-behave can bring down the entire Erlang VM.

留蓝 2024-11-10 06:28:09

在 Erlang 中,管理操作系统进程的标准方法是端口

如果选项 exit_status 添加到 open_port

1> P = open_port({spawn, "/bin/ls unknown_file_name"}, [exit_status]).
#Port<0.486>
/bin/ls: cannot access unknown_file_name: No such file or directory
2> flush().
Shell got {#Port<0.486>,{exit_status,2}}
ok

可以通过向端口发送消息或通过port_command 函数和 stdout 内容将作为消息发送。 (另请注意 open_port{line, L}{packet, N} 选项):

1> P = open_port({spawn, "/bin/cat"}, [stream, binary]).
#Port<0.486>
2> true = port_command(P, <<"data">>).
true
3> flush().
Shell got {#Port<0.486>,{data,<<"data">>}}
ok
4> true = port_close(P).
true

Stderr 也可以重定向到 stdout:

1> P = open_port({spawn, "/bin/ls unknown_file_name"}, [stderr_to_stdout, binary]).
#Port<0.486>
2> flush().
Shell got {#Port<0.486>,
    {data,<<"/bin/ls: cannot access unknown_file_name: No such file or directory\n">>}}
ok

但是您无法使用端口发送终止信号,但如果您通过向端口发送消息或调用 port_close 外部进程可以通过 SIGPIPE 信号退出。

In Erlang standard way to manage OS processes is ports.

Exit status of spawned OS process will be sent as a message if option exit_status added to open_port:

1> P = open_port({spawn, "/bin/ls unknown_file_name"}, [exit_status]).
#Port<0.486>
/bin/ls: cannot access unknown_file_name: No such file or directory
2> flush().
Shell got {#Port<0.486>,{exit_status,2}}
ok

Data can be written to stdin of spawned OS process by sending a message to port or by port_command function and stdout content will be sent as a message. (Note also {line, L} and {packet, N} options of open_port):

1> P = open_port({spawn, "/bin/cat"}, [stream, binary]).
#Port<0.486>
2> true = port_command(P, <<"data">>).
true
3> flush().
Shell got {#Port<0.486>,{data,<<"data">>}}
ok
4> true = port_close(P).
true

Stderr also can be redirected to stdout:

1> P = open_port({spawn, "/bin/ls unknown_file_name"}, [stderr_to_stdout, binary]).
#Port<0.486>
2> flush().
Shell got {#Port<0.486>,
    {data,<<"/bin/ls: cannot access unknown_file_name: No such file or directory\n">>}}
ok

However you can't send kill signals with ports but if you close port by sending a message to port or by calling port_close external process can exit by SIGPIPE signal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文