0

I use this command to kill remotely a process but it does not work.

ssh -t root@g-9 -x "sshpass -p 'ubuntu' ssh -t [email protected] -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -x 'kill -9 `ps aux | grep cassandra | awk '{print $2}'`'"
Warning: Permanently added '10.147.243.178' (ECDSA) to the list of known hosts.
bash: line 0: kill: (12720) - No such process
Connection to 10.147.243.178 closed.

Any idea where could be an error?

рüффп
  • 744
  • 6
  • 13
  • 26
researcher
  • 394
  • 1
  • 4
  • 18
  • Your command substitution is in double-quotes so it runs locally. It doesn't matter it's in single-quotes inside double-quotes because the most outer quotes count. And because you connect in chain, you need to take care so the command substitution is single-quoted also on the intermediate host. I think the command can be fixed with some heavy quoting frenzy. – Kamil Maciorowski Feb 12 '19 at 09:22

2 Answers2

0

You can try:

pkill -f cassandra
0

try replacing your command string with:

kill -9 `ps aux | grep cassandra | grep -v "grep " | awk '{print $2}'`

This will prevent grep from appearing in the results you are trying to parse.

What is actually happening, is that your parsing is picking up your grep process, and attempting to kill it, but it has already completed. When you grep a string that does not exist in ps's output, you will get a response back, but it will be the PID of the grep process, which is of no use to you.

IE (I do not have a process called "thisIsNotAProcess"):

Minty17 ~ $ ps -aux | grep "thisIsNotAProcess"
username    9364  0.0  0.0  11740   936 pts/2    S+   04:38   0:00 grep --colour=auto thisIsNotAProcess

and if you plug it in to your command string:

Minty17 ~ $ kill -9 `ps aux | grep thisIsNotAProcess | awk '{print $2}'`
bash: kill: (9374) - No such process

Check here for more techniques to avoid this pitfall: Excluding grep from process list

Frank Thomas
  • 35,097
  • 3
  • 77
  • 98
  • ssh -t root@g-9 -x "sshpass -p 'ubuntu' ssh -t [email protected] -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -x 'kill -9 `ps aux | grep cassandra | grep -v "grep" | awk '{print $2}'`'" Warning: Permanently added '10.147.243.178' (ECDSA) to the list of known hosts. kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec] Connection to 10.147.243.178 closed. I think that there is a quotes " " problem .. – researcher Jun 22 '15 at 15:23
  • That means that there is no process line in `ps -aux` which matches the string `cassandra`. the grep and awk are not returning any value so you are litterally running `kill -9 ` with no PID, so Kill prints its usage. you can reproduce this yourself with `kill -9 `. This will happen with your script any time the grep cannot find a line. perhaos you have mispelled 'cassandra'. Remember grep is case-sensitive, so "Cassandra" != "cassandra". I was already expecting this, since your prior issue was with detecting the grep process, and grep will always have the highest PID with the target string. – Frank Thomas Jun 22 '15 at 15:43
  • When i use this command kill -9 `ps aux | grep cassandra | grep -v "grep " | awk '{print $2}'` directly on the host, i have no error and it kills cassandra process . i have only a problem when i worl remotely ! Bests – researcher Jun 22 '15 at 17:20
  • if that is the case, then `ps -aux` is not listing the process for you. That may be because the process may be on another "terminal" so try `ps -ef` instead. – Frank Thomas Jun 22 '15 at 18:31