osx - Passing a port number to lsof in a shell function -
i wrote alias in bash profile me kill rogue rails server processes don't cleanly close. alias works well.
alias kill3000="lsof -i tcp: 3000 -t | xargs kill -9 | echo 'killed processes on port 3000'"
i wanted make more general purpose, frameworks work on other ports. tried make similar function, pass port number variable, error. function wrote looks this...
function killproc (){ "lsof -i tcp:$1 -t | xargs kill -9 | echo 'killed processes on port $1'" }
however, when run "killproc 3000", following error:
lsof: unacceptable port specification in: -i tcp:
i'm struggling understand problem, appreciate help.
maybe double-quotes involved.
give try this:
function killproc (){ lsof -i tcp:"$1" -t | xargs kill -9 lsof -i tcp:"$1" -t 2>/dev/null >/dev/null || printf "killed processes on port %s\n" "$1" }
the message printed if there no more process found listening on port pspecified.
if troubles, follow @shellter instruction. run test set -vx ; killproc 300 ; set +vx
and edit question add output generated.