System commands

Hi,

I am want to run a system command (executing a file) from inside a code written in Julia. However, writing

run(`echo Hello`)

throws an error

could not spawn `echo Hello`: no such file or directory (ENOENT)
in run at base\process.jl:650
in #spawn#374 at base\process.jl:511
in setup_stdio at base\process.jl:499
in #375 at base\process.jl:512
in _jl_spawn at base\process.jl:360

What am I doing wrong?

Philippe

1 Like

Are you running Julia on Windows?

In Julia a Cmd is not run through a shell, but is instead exec’d directly.
To run a cmd internal command, such as echo, try:

run(`cmd /c echo Hello`)
3 Likes

Yes, I am running on Windows (and I obviously should have specified that!).

OK, thank you - this helped :grinning:

More questions on the same subject. I am in Windows
I wish to execute a batch file, say

C:\path\web.bat

If I double click on the file in Windows-Explorer, the file executes correctly. If I execute

run(`cmd /c   C:\\path\\web.bat`)

it fails. I believe this is because it is executed from Julia’s home directory, and the batch file does not find some dependencies.

So my question, how do I get Julia to run
a) cd C:\path
b) web.bat

If I just write

run(`cmd /c cd C:\\path\\webapp`)
run(`cmd /c web.bat`)

things fail: 'web.bat' is not recognized as an internal or external command, operable program or batch file. I believe because the “current directory” is lost from on run command ot the next. How can I get run to execute a sequence of commands?

If you want to run a bat file, dont use cmd /c, only specify the path to the file.

(Not sure about Windows-specific concerns to do with cmd, but at least for the path handling part…) The current working directory is inheritied from Julia each time cmd is invoked. Therefore, you need to move Julia’s working directory and then run the command, not change directory inside a command which is going to end. One way to do this without actually changing Julia’s working directory forever is to do it temporarily like

cd("C:\\path\\webapp") do
  run(`cmd /c web.bat`)
end

You could try:

run(`cmd /c cd c:\\path '&&' web.bat`)

Note that special characters should be quoted within Julia commands, so && needs to be quoted.

Greg, Jmert, Chakravala

thank you, all. I have tested your suggestions and they work! :grin:

Philippe