1

I know a process is running on a port 5000 on a remote server. How can I call that in one-liner cmd ?

I can ssh to that server and do kill $(lsof -i:5000 -t). It works.

From my local terminal if i do

ssh user@ip.ip.ip.ip "kill $(lsof -i:5000 -t)"

It gives error like

kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

Sarath
  • 9,030
  • 11
  • 51
  • 84
  • 2
    See: [Difference between single and double quotes in bash](http://stackoverflow.com/q/6697753/3776858) – Cyrus Jan 03 '18 at 13:25

2 Answers2

4

This is caused because your $(lsof -i:5000 -t) command is interpreted locally, and not remotely. Just use simple quotes to prevent local expansion. Alternatively, you can also escape the parts you do not want locally expanded.

ssh user@ip.ip.ip.ip 'kill $(lsof -i:5000 -t)'
# or
ssh user@ip.ip.ip.ip "kill \$(lsof -i:5000 -t)"

Basically, with ssh :

  • Double quotes and regular variables (i.e : $VAR) : local expansion
  • Single quotes and escaped variables (i.e : \$VAR) : remote expansion
Aserre
  • 4,916
  • 5
  • 33
  • 56
-1

ssh to the system and do execute the command

 lsof -a +L1 | awk '$7 > 1073741824 && NR >1 {print $2}' | sort -nu | xargs kill

it will also cleared/released other blocked memory as well

Robert
  • 7,394
  • 40
  • 45
  • 64