如何在命令结束前从 Elixir System.cmd/2 中分离进程?

How can I detach process from Elixir System.cmd/2 before command ends?

我正在尝试 运行 这个 (script.exs):

System.cmd("zsh", ["-c" "com.spotify.Client"])
IO.puts("done.")

Spotify 打开,但 "done." 从未出现在屏幕上。我也试过:

System.cmd("zsh", ["-c" "nohup com.spotify.Client &"])
IO.puts("done.")

我的脚本只在我关闭 spotify 时停止 window。是否可以 运行 命令而不等待它结束?

不应盲目地希望它们能够正常工作来生成系统任务。如果系统任务崩溃,应该在调用 OTP 进程中采取一些措施,否则迟早会在生产中崩溃,没有人知道发生了什么以及为什么。

有很多可能的场景,我会选择 Task.start_link/1 (assuming the calling process traps exits,) or with Task.async/1 accompanied by an explicit Task.await/1 监督树中的某个地方。

如果尽管上面解释了所有内容,但您不关心稳健性,请像下面那样使用 Kernel.spawn/1

pid = spawn(System, :cmd, ~w|zsh -c com.spotify.Client|)
# Process.monitor(pid) # yet give it a chance to handle errors
IO.puts("done.")