Why does the connection break when I kill a process on remote host?

Why does the connection break when I kill a process on remote host?

  • I login to the remote host (debian) by ssh and execute a command like this ssh user@remote_host "ps -ef | grep process_name | grep -v grep | awk {'print $2'} | xargs kill -9' then the connection breaks. I ping the remote host and can't receive any response, like the network isn't connected. But when I restart the remote host (power off and power on), everything is ok. I promise that the process killed is only the program written by me and it's father process is "init" process (if the process run in fg and is killed that everything is ok). Is there any one who knows why it happened?

  • Answer:

    ps -ef | grep process_name | grep -v grep gives not only the pid of what you want to kill, but also other information such as the uid, command of the process which may kill something unexpectly. More unfortunately, its ppid (parent pid, for you is 1) is also showed, then you know what will happen. You may try ssh user@remote_host "pkill process_name" or ssh user@remote_host "ps -eo pid,cmd | grep process_name | grep -v grep | cut -d' ' -f2 | xargs kill -9" Or you can first get its output: ssh user@remote_host "ps -ef | grep process_name | grep -v grep" and then filter the pid yourself.

yanshuyuan at Server Fault Visit the source

Was this solution helpful to you?

Other answers

I wonder that the command you are showing us even ran without errors. As mentioned in the other answer, you are passing too many things to xargs/kill which they treat as garbage. Use something like this to extract only the PID an kill it ps -ef | grep process_name | grep -v grep | awk '{print $3}' | xargs kill -9

Pavan Manjunath

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.