Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: Execute external commands in strict sequence

Tags:

haskell

If I am in a situation where I need to execute external commands in sequence, what is the best solution?

For instance, I have two commands "make snapshot" and "backup snapshot" The second cannot start till the first one is complete. If I orderly stick those two commands in a do syntax would they be executed one after another or do I have to manually check and make sure the first one is complete?

For the manual completion check, is it enough to use "system" or rawSystem" and examine their exit code?

I don't fully understand the difference between "system" and "runCommand" functions. Can someone clarify this to me. I can only see they return different values: exit code vs process handle. Any other differences?

Would I rather need to use "runCommand" for the above sequence to work? Do I need to call wait on the process handle?

Thanks.

like image 496
r.sendecky Avatar asked Mar 15 '12 03:03

r.sendecky


1 Answers

I understand you are using the System.Process module to run the external commands. This is good.

The module contains both blocking and non-blocking IO actions. The non-blocking ones (like createProcess, runCommand) create a process and return its handle immediately, while it's still running. The blocking ones (like readProcess, system) do not return any handles, but rather return the result of running the process once it terminates.

To ensure that the process has terminated, you need to either use blocking actions, or use waitForProcess, which blocks until the process with the given handle dies.

is it enough to use "system" or rawSystem" and examine their exit code?

Yes.

the difference between "system" and "runCommand" functions

The main difference is system is blocking while runCommand is not.

Would I rather need to use "runCommand" for the above sequence to work?

No, blocking calls should be enough in your case.

Do I need to call wait on the process handle?

Only if you decide to use non-blocking calls.

Example of usage:

import System.Process
main = do
  ExitSuccess <- system "make snapshot"
  ExitSuccess <- system "backup snapshot"
  return ()
like image 181
Rotsor Avatar answered Nov 09 '22 09:11

Rotsor