gpt4 book ai didi

elixir - 如何在 Elixir 中启动操作系统进程

转载 作者:行者123 更新时间:2023-12-04 22:04:39 24 4
gpt4 key购买 nike

在 Elixir 中启动操作系统进程的最佳方式是什么?

我希望能够在开始时向它传递不同的参数,
捕获它的PID然后杀死它。

最佳答案

您可以使用端口来实现这一点:

defmodule Shell do
def exec(exe, args) when is_list(args) do
port = Port.open({:spawn_executable, exe}, [{:args, args}, :stream, :binary, :exit_status, :hide, :use_stdio, :stderr_to_stdout])
handle_output(port)
end

def handle_output(port) do
receive do
{^port, {:data, data}} ->
IO.puts(data)
handle_output(port)
{^port, {:exit_status, status}} ->
status
end
end
end

iex> Shell.exec("/bin/ls", ["-la", "/tmp"])

关于elixir - 如何在 Elixir 中启动操作系统进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25342212/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com