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 root@10.147.243.178 -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?

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
  • 37,476