Julia test process exit status

Dear all,
I am trying to run shell commands via julia so to move from bash to julia altogether. However, I need to check the exit status of the commands launched. I tried the tips given in this post but did not work. For instance, how can I get the exit status of a simple ls command?

julia> cmd = `ls ./`
julia> run(cmd)
readSelection.jl  doSomething.jl  rslt.txt
Process(`ls ./`, ProcessExited(0))

but:

julia> err = Pipe()
julia> out = Pipe()
julia> proc = spawn(pipeline(ignorestatus(cmd),stdout=out,stderr=err))
ERROR: UndefVarError: spawn not defined
Stacktrace:
 [1] top-level scope at none:0

thus the rest of the code was not executed:

julia> close(err.in)
julia> close(out.in)
julia> println("stdout: ",String(read(out)))
julia> println("stderr: ",String(read(err)))
julia> println("status: ",proc.exitcode)

Why is spawn not defined? Is it possible to get the ProcessExited(0) string generated by run?
Thanks

spawn(::Cmd) was deprecated in Julia 0.7. If you run the code in Julia 0.7 you get the following warning:

Warning: `spawn(cmds::AbstractCmd)` is deprecated,
         use `run(cmds, wait=false)` instead.

so you should use run(::Cmd; wait=false) as an replacement.

1 Like

Thank you, but how do I capture the exit status?
I tried with

proc=run(cmd, stdout=out,stderr=err; wait=false)
ERROR: MethodError: no method matching run(::Cmd; stdout=Pipe(RawFD(0xffffffff) init => RawFD(0xffffffff) init, 0 bytes waiting), stderr=Pipe(RawFD(0xffffffff) init => RawFD(0xffffffff) init, 0 bytes waiting), wait=false)
Closest candidates are:
  run(::Base.AbstractCmd, ::Any...; wait) at process.jl:661 got unsupported keyword arguments "stdout", "stderr"
Stacktrace:
 [1] kwerr(::NamedTuple{(:stdout, :stderr, :wait),Tuple{Pipe,Pipe,Bool}}, ::Function, ::Cmd) at ./error.jl:97
 [2] (::getfield(Base, Symbol("#kw##run")))(::NamedTuple{(:stdout, :stderr, :wait),Tuple{Pipe,Pipe,Bool}}, ::typeof(run), ::Cmd) at ./none:0
 [3] top-level scope at none:0
julia> proc = run(`ls`; wait=false)
Process(`ls`, ProcessRunning)

julia> proc.exitcode
0

If you just want to make sure the exit code is 0 you can use success.

3 Likes

Thank you!