-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdlooplog.hs
27 lines (24 loc) · 901 Bytes
/
cmdlooplog.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
-- cmdlooplog: loops input command until it returns an ExitSuccess
import Control.Concurrent
import Data.Time
import System.Environment
import System.Exit
import System.Locale
import System.Process
getTimeStamp = fmap (formatTime defaultTimeLocale "%Y%m%d%H%M%S") getCurrentTime
main = do
(e,a) <- fmap (splitAt 1) getArgs
if null e
then do putStrLn "cmdlooplog: no input command"
exitFailure
else loop (concat e) a
loop exec args = do
putStrLn $ "running command: " ++ exec ++ " " ++ show args
timestamp <- getTimeStamp
(ecode,stdout,stderr) <- readProcessWithExitCode exec args ""
writeFile (exec ++ "-stdout-" ++ timestamp ++ ".log") stdout
writeFile (exec ++ "-stderr-" ++ timestamp ++ ".log") stderr
case ecode of
ExitSuccess -> putStrLn "done!"
_ -> do threadDelay 1000000
loop exec args