Running programs in the background from Mathematica

Is there a way to make the background process in Mathematica so that the external command executes but does not delay other evaluations?

startTime = AbsoluteTime[]; Run["sleep 2" (*, a magic option here perhaps? *)]; AbsoluteTime[] - startTime (* Returns 2.016960 on my system *) 

I want a version of / option for Run that does not delay evaluation, so the last line in the above code will return something close to zero.

(The above will run on * nix and OSX, there seems to be no universal / portable way to "sleep 2" in DOS / Windows shell: Sleeping in a batch file . Although, perhaps the easiest way to get the sleep command is to install the MS resource set - see this answer to Sleep in a batch file or comments below.)


ALSASound now, the reason I want to do this is because my ALSASound command, listed in No sound in Mathematica under linux , will not stop fireworks . But it will also be a good improvement to ALSASound and a useful thing to know in general.


Edit

The two solutions below rely on using the shell to create a new / child process using start cmd on Windows / DOS and cmd& on * nix. Is there a platform independent and / or mathematica-based solution? (Points will be awarded for creating a team that checks the OS using $OperatingSystem !)

+4
source share
3 answers

As Simon points out, Switch is the right tool for procedural conditions and is more appropriate than Piecewise . Here's the solution using Switch :

 BackgroundProcess[proc_String] := Switch[$OperatingSystem, "MacOSX" | "Unix", Run["(" <> proc <> ")&"], "Windows", Run["start /b " <> proc], _, $Failed] 

You can write a wrapper function to handle several operating systems.

 BackgroundProcess[proc_String] := Module[{ command = Piecewise[{ {proc <> " &", $OperatingSystem == "MacOSX" || "Unix"}, {"start /b " <> proc, $OperatingSystem == "Windows"} }] }, Run[command]] 

/b after start starts the process in the background without creating a new command window, and the output is printed to stdout. Then you can use this to include error messages, some input sanitation (for example, not allowing rm , etc.), if you want it, and everything should be installed.

A predefined function from Mathematica (if one exists) is likely to run along these lines. Background processes are inherently OS-dependent, so there is no real “platform-independent” way to do this. Any implementation that claims to be like that (like the os module in python) is basically a set of rules defined for every possible OS that is known to exist, so you really don't need to worry about the finer details .

+4
source

On Windows (I realized what you are trying to do?)

 startTime = AbsoluteTime[]; Run["start notepad"]; AbsoluteTime[] - startTime (* -> 0.1406250 *) 

Edit

For reasons of mobility, I do not think that Mma should accept the specific functions of the underlying OS. The presence of standard input and output and shell commands called from within the program is enough. As soon as you get control of the OS shell of your choice, do whatever you want (call start or send & , for example) or whatever your OS allows.

+1
source

Although I don’t know the way Mathematica does this, there is a very simple BASH / shell solution: just create a child process with & " .

Then

 startTime = AbsoluteTime[]; Run["(sleep 20)&"]; AbsoluteTime[] - startTime (* Returns approx 0.01 *) 

A similar solution works with my ALSASound team, and I updated my answer .

0
source

All Articles