How to capture the output of a system command?

473 views
Skip to first unread message

J Luis

unread,
Sep 11, 2015, 7:56:43 PM9/11/15
to julia-users
Ok, I've spend about an hour around "run" "open", "run(pipeline(..." but no way.
In Matlab I would do

    [status, cmdout] = system(cmd);

but in Julia the most a can reach is to run the command

com = "C:/programs/GraphicsMagick/gm.exe compare -density 200 ...

   run(`com')

but I need the result of that execution.
How to?

Thanks




Simon Kornblith

unread,
Sep 11, 2015, 8:17:57 PM9/11/15
to julia-users
readall(`cat test`) or similar

J Luis

unread,
Sep 11, 2015, 9:00:25 PM9/11/15
to julia-users
Thanks. It turned out my main error was that I was build the Cmd object by first creating the command as a string and after wrapping it in back ticks.

But I continue in troubles. Right, I can run the command, a GraphicsMagic image comparison command, but when that comparison say the two images are different gm.exe returns an error code != 0 and Julia interprets it a command error. But it isn't, it only means the two images are different. As a consequence the julia function where this happens aborts (an example in REPL bellow).

I tried with a try catch but not even that prevented the function abortion. How can I get out of this one?

julia> readall(cm)
C:/programs/GraphicsMagick/gm.exe compare: image difference exceeds limit (0.33549 > 0.001).
ERROR: failed process: Process(`C:/programs/GraphicsMagick/gm.exe compare -density 200 -maximum-error 0.001 -highlight-color magenta -highlight-style assign -metric rmse -file V:/example_02.png C:/progs_cygw/GMTdev/gmt5/branches/5.2.0/doc/examples/ex02/example_02.ps V:/example_02.ps`, ProcessExited(1)) [1]
 in pipeline_error at process.jl:548

J Luis

unread,
Sep 11, 2015, 9:17:17 PM9/11/15
to julia-users
Found a ignorestatus function. Better, but not yet good enough. It still prints a message even if using a suppression ';'

julia> readall(ignorestatus(cm));
C:/programs/GraphicsMagick/gm.exe compare: image difference exceeds limit (0.336711 > 0.001).

Miguel Bazdresch

unread,
Sep 12, 2015, 10:35:39 AM9/12/15
to julia...@googlegroups.com
What could be happening is that `gm.exe` is printing that message to STDERR, which is not captured by `readall()`. In theory, the new pipeline infrastructure should let you capture STDERR, but I haven't had the time to figure out exactly how. Maybe somebody who knows will chime in.

-- mb

J Luis

unread,
Sep 12, 2015, 10:47:06 AM9/12/15
to julia-users
Right, that's indeed what is (very sadly) happening. It turned out that the stderr output is the part that I really need in this case.
The only working solution I found was to do

    run(pipeline(ignorestatus(cmd), stdout=DevNull, stderr="errs.txt"))
    t = readall("errs.txt");

but it's annoying having to write a disk file and read its contents.
 
Had some hope on this one, but no way too

julia> readall(run(pipeline(ignorestatus(cm), STDOUT, STDOUT)))
Image Difference (RootMeanSquaredError):
           Normalized    Absolute
          ============  ==========
     Red: 0.3709618690    24311.0
   Green: 0.3611391197    23667.3
    Blue: 0.2638682470    17292.6
   Total: 0.3354900017    21986.3

C:/programs/GraphicsMagick/gm.exe compare: image difference exceeds limit (0.33549 > 0.001).
ERROR: MethodError: `readall` has no method matching readall(::Void)

and this one completely hangs the REPL giviing me no choice but killing the shell window

    rd,wr=redirect_stderr();

    run(pipeline(ignorestatus(cmd), stdout=DevNull, stderr=rd))

now if try to read from 'rd'

    readall(rd)    # ---> IceAge

Miguel Bazdresch

unread,
Sep 12, 2015, 10:51:40 AM9/12/15
to julia...@googlegroups.com
This is a change to `open` that would allow capturing STDERR the way you want. This change won't make it into v0.4, but the code in there could point you towards a solution. I hope to have some time soon to dive into this (I need to implement the same thing in Gaston).

-- mb

J Luis

unread,
Sep 12, 2015, 11:04:23 AM9/12/15
to julia-users
Thanks, I'll keep an eye on that PR.
One other thing that is also missing is the possibility of returning the command status like Matlab does. In my workaround above I force it to be ignored (otherwise it may abort the function execution) but we than have no info of its value. Right, one can do a

    success(cm)

but this implies running the command twice ... and `cmd` may do a heavy job
Reply all
Reply to author
Forward
0 new messages