Some basic question about the run() command

Questions:

  1. Solved! Is it possible to run an external cmd in background?

run(`sleep 10`)

works, but not

run(`sleep 10 \&`)

  1. In my docu run() is shown as

run(command, args…)
Run a command object, constructed with backticks. Throws an error if anything goes wrong, including the process exiting with a non-zero status.

How to use the comma separated syntax?

cmd = `kill`
run(cmd,12452 5467)

does not work.

  1. Solved! How to pass arguments correct over?

If I like to stop several processes

run(`kill 12452 5467`)

works fine. But if I generate a string of pids and try

string = “12452 5467”
run(`kill $string`)

is not working. Although if I pass Integers over

s1 = 12452
s2 = 5467
run(`kill $s1 $s2`)

it works. So how create an object which contains several integers and is accepted as argument?
Ah, try a tuple

stuple = tuple(12452,5467)

this works.

  1. Solved! If an external cmd fails and exit with error code 1 is

try
run()
catch

the only possibility to react on it? Or how else? Or how can I get back an empty string result?

Thx for any hints. Your help is very welcome as always.

1 Like

Look at the output:

julia> string = "12452 5467"
"12452 5467"

julia> `kill $string`
`kill '12452 5467'`

julia> s1 = 12452;  s2 = 5467;

julia> `kill $s1 $s2`
`kill 12452 5467`

Use success

4 Likes

In Julia 0.7:

run(`sleep 10`, wait=false)

In Julia 0.6 it is spelled spawn(`sleep 10`). You can wait on the result with wait if you want. You can also run commands in asynchronous tasks (green threads) with @async. Julia has good support for asynchronous I/O of all kinds.

3 Likes

Thx for answering!

Over the spawn() function I found ignorestatus() which can be a solution too for question nr 4.

Therefore only question 2. remains open :wink:

Can someone explain the run syntax found in the Docs under

https://docs.julialang.org/en/stable/stdlib/base/#Base.run

with an example showing that syntax. Sorry, but I maybe misunderstand it.

See https://docs.julialang.org/en/stable/manual/running-external-programs/

The older blog post Put This In Your Pipe is still helpful though some of the syntax has changed slightly and Shelling Out Sucks is good to read for the for rationale behind Julia’s approach.