Haskell: Execute external commands in strict sequence

后端 未结 1 461
粉色の甜心
粉色の甜心 2021-01-17 21:32

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 \"b

相关标签:
1条回答
  • 2021-01-17 21:54

    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 ()
    
    0 讨论(0)
提交回复
热议问题