The alias cannot work properly as this statement is executed during alias creation and not during run time.
lsof -n -i:8080 | grep LISTEN | awk '{print $2}'
So your alias will only kill this process that opens the port 8080 at the time when you create the alias and not when you are using the alias. The process number created for the alias will be static unless you re-create your alias create a statement each time before running it. So you test this by doing:
alias test1="echo $(lsof -n -i:8080 | grep LISTEN | awk '{print $2}')"
The test1 alias will always echo the same process number even if you manually kill the process and restart another process for port 8080. So the only way to really do this is to a bash script to do this instead of using aliases.
Also, note that the step also will not work if subprocesses are returned for lsof
in Ubuntu, but I suppose this is not the case in OSX, but just be aware of this. For multiple processes, you will need to use a loop to kill each one but ignoring any error as killing the main process could kill any subprocesses as well.
Edited: You can do this with a one-liner in bash, not as an alias. This will work even if multiple process numbers are returned.
for p in `sudo lsof -n -i:8080 | grep LISTEN | awk '{print $2}'`; do sudo kill -9 $p; done
PS: You can check all your aliases by entering alias
at the command prompt. It's a quick way to verify what is actually being created.