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 +vxand edit question add output generated.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo