[HOWTO] Call a process and getting it's info?

I want to call, for example the process ls -a, and from that, I want it’s total time, user time, system time, cpu time, RAM used etc., like the time commands available in most shells, such as bash, zsh and so on.
How can I do this?

run(`time ls -a`) will deliver the ls output and the time information to standard output. It’s calling the /usr/bin/time command on linux rather than using a shell builtin or a julia emulation.

a = read(`time ls -a`, String) will deliver the ls output as a String, but the time information - which is printed to stderr - still goes to julia’s standard output.

The pipeline function allows pipes and redirections, but I don’t see any way to do the equivalent of time ls -a 2>&1 which would append the time information after the directory listing.

a = read(pipeline(`time ls -a`; stderr="time.out"), String) will deliver the output of ls as a String and redirect the time command output into a file, then t = read("time.out", String) will collect the output of the time command as a string.

The man page for /usr/bin/time has a wealth of formatting options.


On ZSH which time says it’s a shell reserved keyword, with my simple which that I wrote for my IRB, returns nil (Ruby’s nothing).

Furthermore, file /usr/bin/time says: “/usr/bin/time: cannot open `/usr/bin/time’ (No such file or directory)”

Yes time is a shell builtin, but it’s also a program. It appears that it’s not installed on your system, but you can probably install it, for example with apt install time on Debian or Ubuntu.

1 Like

Well, as @sudete suggests, you could install the time command.

Or you could use the zsh builtin function itself.

a = read(pipeline(`zsh -c 'time ls -a'`; stderr="time.out"), String)
t = read("time.out", String)

Or you could modify julia/base/process.jl, the library that implements run(...), to incorporate a time(...) function into Julia.