I'm writing a Haskell program whose sole purpose is to execute another (non-Haskell) program, after gathering some info to determine what arguments that program should be executed with.
Replacing a running Haskell process is easy enough using executeFile, but I'm wondering if there's a way to ensure the Haskell program properly cleans up after itself before it's replaced.
For example, Control.Exception.finally is normally useful to ensure cleanup actions are performed even if an exception occurs, but it will have no effect if we exec a whole different process:
module Main (main) where
import Control.Exception (finally)
import System.Posix.Process (executeFile)
main = finally (do putStrLn "opening file descriptors"
putStrLn "writing temporary files"
executeFile "ls" True [] Nothing)
(putStrLn "cleaning up")
The above example will never print "cleaning up".
Is there a recommended way to perform such a maneuver "cleanly"?
You need to put the executeFile after finally, because it completely replaces any code from the Haskell program.
main = do
finally (putStrLn "opening" >> putStrLn "writing") (putStrLn "cleaning up")
executeFile "ls" True [] Nothing
The only exception that can occur from executeFile is a problem with the exec itself, but you have to proceed as if the exec will work and you aren't coming back to the Haskell code; it's a one-way goto, not a subroutine call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With