Skip to content

Instantly share code, notes, and snippets.

@jroesch
Created November 5, 2012 09:10
Show Gist options
  • Save jroesch/4016194 to your computer and use it in GitHub Desktop.
Save jroesch/4016194 to your computer and use it in GitHub Desktop.
For when Chrome gets cranky and takes all your memory ...
module SleepyTime where
-- Check out System.Proccess to avoid using the tmp file.
import System.Cmd
import System.Exit (ExitCode(..))
-- Find something lighter weight for the filtering.
import Text.Regex.TDFA
main :: IO ()
main = do
exit <- system "ps -eaxo pid,command | grep '/Applications/Google Chrome\\.app.* --type=renderer.*' | grep -o '^ [0-9]*' > /tmp/result.sleepy_time"
contents <- case exit of
ExitFailure _ -> error "Well... your fucked this didn't work at all good luck with kill."
ExitSuccess -> readFile "/tmp/result.sleepy_time"
let pids = map eatWhiteSpace $ filter (=~ "[0-9]+") $ lines contents
exit' <- rawSystem "kill" $ ["-9"] ++ pids
case exit' of
ExitFailure _ -> error "So ... you passed the first round, but this did not work at all."
ExitSuccess -> putStrLn "Chrome went nighty night."
eatWhiteSpace :: String -> String
eatWhiteSpace xs = filter (\x -> x /= ' ' && x /= '\t') xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment