mirror of
https://github.com/srid/nixos-config.git
synced 2026-02-04 03:53:35 +08:00
fuckport reborn
This commit is contained in:
parent
a5e6c1ba75
commit
56d96bea90
1 changed files with 37 additions and 17 deletions
|
|
@ -1,19 +1,39 @@
|
|||
{ writeShellApplication, jc, jq, lsof, ... }:
|
||||
# https://x.com/sridca/status/1861875950897578113
|
||||
{ writers, haskellPackages, coreutils, jc, lsof, ... }:
|
||||
|
||||
writeShellApplication {
|
||||
name = "fuckport";
|
||||
runtimeInputs = [ jc jq lsof ];
|
||||
meta.description = ''
|
||||
Kill the process with the port open
|
||||
writers.writeHaskellBin "fuckport"
|
||||
{
|
||||
libraries = with haskellPackages; [
|
||||
shh
|
||||
aeson
|
||||
];
|
||||
} ''
|
||||
{-# LANGUAGE TemplateHaskell #-}
|
||||
{-# LANGUAGE DerivingStrategies #-}
|
||||
{-# LANGUAGE DeriveAnyClass #-}
|
||||
import Shh
|
||||
import System.Environment
|
||||
import Data.Aeson
|
||||
import Data.Maybe
|
||||
import GHC.Generics
|
||||
import Control.Monad
|
||||
|
||||
Used only to kill stale ghc.
|
||||
'';
|
||||
# FIXME: This doesn't work when lsof returns *multiple* processes.
|
||||
text = ''
|
||||
lsof -i :"$1"
|
||||
THEPID=$(lsof -i :"$1" | jc --lsof 2> /dev/null | jq '.[].pid')
|
||||
echo "KILL $THEPID ?"
|
||||
read -r
|
||||
kill "$THEPID"
|
||||
'';
|
||||
}
|
||||
loadFromBins ["${lsof}", "${coreutils}", "${jc}"]
|
||||
|
||||
data LsofRow = LsofRow
|
||||
{ command :: String
|
||||
, pid :: Int
|
||||
, user :: String
|
||||
}
|
||||
deriving stock (Generic, Show)
|
||||
deriving anyclass (FromJSON)
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
port <- Prelude.head <$> getArgs
|
||||
s <- lsof "-i" (":" <> port) |> jc "--lsof" |> capture
|
||||
let v = fromJust $ decode @[LsofRow] s
|
||||
forM_ v $ \r -> do
|
||||
putStrLn $ "Killing " <> show (pid r) <> " (" <> command r <> ")"
|
||||
kill $ pid r
|
||||
''
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue