Created
December 27, 2010 14:14
-
-
Save LiosK/756160 to your computer and use it in GitHub Desktop.
runfor -- Runs a command for a given time, kills it after the time is out.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ghc -W -O -threaded --make runfor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
ghc -W -O -threaded --make runfor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Runs a command for a given time, kills it after the time is out. | |
-- Licensed under the MIT License: Copyright (c) 2010-2011 LiosK. | |
import Control.Concurrent | |
import Control.Monad | |
import System | |
import System.Process | |
main = do | |
argv <- getArgs | |
when ("--help" `elem` take 1 argv) $ showUsage >> exitWith ExitSuccess | |
when (length argv < 2) $ do | |
putStr "Error: Specify required arguments: PROG and DURATION.\n" | |
showUsage >> exitFailure | |
let (n:prog:args) = argv | |
sec <- readIO n `catch` (\_ -> do | |
putStr "Error: Specify a proper DURATION.\n" | |
showUsage >> exitFailure) | |
(_, _, _, ph) <- createProcess $ proc prog args -- run target process | |
tk <- forkIO $ timekeeper sec ph -- run timekeeper thread | |
ec <- waitForProcess ph -- wait until the process ends or is killed by tk | |
killThread tk -- killThread does nothing when the thread is already dead | |
exitWith ec | |
timekeeper sec ph = do | |
threadDelay (sec * 1000000) | |
terminateProcess ph | |
showUsage = do | |
prog <- getProgName | |
putStr $ "Usage: " ++ prog ++ " DURATION PROG [ARG]...\n" | |
putStr $ "Execute a PROGram for DURATION seconds.\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment