0

I'm trying to run this (script.exs):

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

Spotify opens, but "done." never shows up on the screen. I also tried:

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

My script only halts when I close the spotify window. Is it possible to run commands without waiting for it to end?

Augusto Dias
  • 169
  • 1
  • 9

1 Answers1

2

One should not spawn system tasks in a blind hope they would work properly. If the system task crashes, some actions should be taken in the calling OTP process, otherwise sooner or later it’ll crash in production and nobody would know what had happened and why.

There are many possible scenarios, I would go with Task.start_link/1 (assuming the calling process traps exits,) or with Task.async/1 accompanied by an explicit Task.await/1 somewhere in the supervision tree.

If despite everything explained above you don’t care about robustness, use Kernel.spawn/1 like below

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