Skip to content

Instantly share code, notes, and snippets.

@thesephist
Created July 8, 2025 03:28
Show Gist options
  • Save thesephist/12814552c0945f2c5ca8d98e122aeb5a to your computer and use it in GitHub Desktop.
Save thesephist/12814552c0945f2c5ca8d98e122aeb5a to your computer and use it in GitHub Desktop.
Portobello clears rogue processes from local ports
std := import('std')
str := import('str')
fmt := import('fmt')
cli := import('cli')
Version := '1.0'
Filename := 'portobello.oak'
Cli := with cli.parseArgv() if {
args().1 |> std.default('') |> str.endsWith?(Filename) -> args()
_ -> ['oak', Filename] |> std.append(args() |> std.slice(1))
}
if Cli.opts.version != ? -> {
fmt.printf('Portobello v{{0}}', Version)
exit(0)
}
if Cli.opts.help != ? | Cli.opts.h != ? -> {
std.println('Portobello clears rogue processes from local ports
Usage
portobello [cmd] [port] [options]
Commands
list List processes on a port
clear Clear processes from a port
Options
--[h]elp Show this help message
--[d]ry-run Print all operations, but do not run them
--version Print version information and exit
Examples
List processes listening on 3000
portobello list 3000
Kill all processes listening on 3000
portobello clear 3000
Dry run clearing all processes listening on 3000
portobello clear 3000 -d
')
exit(0)
}
if Cli.verb = ? -> {
std.println('No command provided. Try "-h" to see the manual.')
exit(1)
}
fn listeningPids(port, withPids) {
with exec(
'lsof'
['-i', ':{{0}}' |> fmt.format(port)]
''
) fn(result) {
stdout := result.stdout
lines := stdout |> str.split('\n') |> std.slice(1) |> std.filter(fn(l) len(l) > 0)
values := lines |> std.map(fn(line) {
line |> str.split(' ') |> std.filter(fn(s) s != '')
})
pids := values |> std.map(fn(vals) int(vals.1))
withPids(pids)
}
}
Cli.opts.port := Cli.opts.port |> std.default(Cli.args.0)
if Cli.opts.port = ? -> {
std.println('Missing port.')
exit(1)
}
if Cli.verb {
'list' -> with listeningPids(Cli.opts.port) fn(pids) {
pids |> with std.each() fn(pid) std.println(pid)
}
'clear' -> with listeningPids(Cli.opts.port) fn(pids) if {
Cli.opts.d
Cli.opts.'dry-run' -> pids |> with std.each() fn(pid) {
fmt.printf('kill -9 :{{0}}', pid)
}
_ -> {
pids |> with std.serial(fn(pid, i, next, done) {
with exec('kill', ['-9', string(pid)], '') fn(result) if result.status {
0 -> {
fmt.printf('Killed pid={{0}}', pid)
next()
}
_ -> {
std.println('Error clearing port. Error below:')
std.println(result.stdout)
std.println(result.stderr)
done()
}
}
}) fn {
std.println('done.')
}
}
}
_ -> {
std.println('oops, no command')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment