Search code examples
bashshellcommand-linegrepkill-process

How kill process ID remotely Bash


On linux device, the following process ID runs:

enter image description here

I'm trying to kill the process ids related to ads2 (shown in the image above) remotely (through a bash script that runs on another device). So I tried:

ssh nvidia@"id-address" "kill pgrep ads2"

where pgrep returns the process IDs related to ads2. When i run the script it prompts me to enter the password and then nothing happen, i mean the processes are not terminated.

However, i can't figure out where the error is.

Thanks in advance


Solution

  • kill expects a number (or list of numbers) following it. pgrep ads2 is just words!

    For bash to replace the words pgrep ads2 with the result of running that command to produce kill 15951 15995 you can use backticks.

    i.e. : kill `pgrep ads2` will first run pgrep ads and then kill (result of pgrep ads2)

    However, as you are performing this over ssh, your computer would run the backticks before the remote. I.e. pgrep ads would be run on your local machine, and kill on the remote, which wouldn't work. So you must escape the backticks like so:

    ssh nvidia@"id-address" "kill \`pgrep ads2\`"