How to run a command but not wait for it to return?

Process.run("ls")
puts "I am done"
#=> I am done
Process.exec("ls")
puts "I am done"
#=> exec.cr ...
system "ls"
puts "I am done"
#=> exec.cr ...
#=> I am done

The first or third output is what I want but they both wait for the command to finish.

Better to use Process.new
https://crystal-lang.org/api/master/Process.html#new(command%3AString%2Cargs%3Dnil%2Cenv%3AEnv%3Dnil%2Cclear_env%3ABool%3Dfalse%2Cshell%3ABool%3Dfalse%2Cinput%3AStdio%3DRedirect%3A%3AClose%2Coutput%3AStdio%3DRedirect%3A%3AClose%2Cerror%3AStdio%3DRedirect%3A%3AClose%2Cchdir%3APath|String|Nil%3Dnil)-class-method

or invoke run inside a Fiber. e.g spawn { Process.run("ls") }.

2 Likes