0

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?

  • Wrong site. It is an admin issue, not a programming issue. – glglgl Apr 27 '12 at 08:47
  • 2
    what does `ps -ef | grep process_name | grep -v grep | xargs` return on the remote host? Are you sure it outputs valid arguments for `kill -9`? –  Apr 27 '12 at 08:49
  • @glglgl it is kind of related to shell programming :) –  Apr 27 '12 at 08:49

2 Answers2

1

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.

Dot Cink
  • 111
  • 3
  • I'm sorry, I execute "ssh user@remote_host "ps -ef | grep process_name | grep -v grep | awk {'print $2'} | xargs kill -9", I make a mistake in my question. –  Apr 27 '12 at 10:34
  • So does `ssh user@remote_host "ps -ef | grep process_name | grep -v grep | awk {'print $2'}"` just show your process's pid? If so, I can not help you if you would not give more details. –  Apr 27 '12 at 10:54
  • I just try again, i only execute ssh user@remote_host "ps -ef | grep process_name | grep -v grep | awk {'print $2'}" on local, and I find the remote host return the information which is the same with "ps -ef | grep process_name | grep -v grep", and then I try to execute "ps -ef | grep process_name | grep -v grep | awk {'print $2'}" on remote host, it's only show the PID, do you know why the same command return differnt info? –  Apr 27 '12 at 11:40
  • ER1:~# ssh root@2001:da8:215:3566::1 "ps -ef | grep iperf | grep -v grep | awk {'print $2'}" and the remote host return "root 18776 1 0 03:37 ? 00:00:00 iperf -u -V -s -t 10000 -p 9999", why awk don't work ? –  Apr 27 '12 at 11:48
  • Yeah, I just found it. Just add a "\" before "$". It seems that when you pass a command to `ssh`, it just redirect it to `bash -c` to execute. You can try `ssh user@remote_host "echo $0"`, for me, it shows `/bin/bash`. –  Apr 27 '12 at 12:22
  • thank you. But I found the problem still exists. Just know, I try to ssh root@2001:da8:215:3566::1, and I input the ps -ef | grep process_name and then I copy the pid, and execute "kill -9 PID", then it happened again, I think it's not the init process killed problem. It confuses me –  Apr 27 '12 at 12:30
  • Now you may give some info about your program :) –  Apr 27 '12 at 12:38
  • I found that I could login the remote host by ipv4, but not ipv6. I use pstree and found there are some child processes, I think that the child process maybe do some thing about ipv6. When I use kill -9, then the child processes are all killed, then the ipv6 crash. Now I use kill -2 to kill it, it works well. Thank you for your help. –  Apr 27 '12 at 13:13
1

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
  • I make a mistake, I forget to input awk {'print $2'} in my description. I try to ssh the remote host, and ps -ef to get the pid, and kill -9 pid myself, network crash still happen –  Apr 27 '12 at 11:32
  • What is your program doing? Can you paste your program here if small or in pastebin? –  Apr 27 '12 at 12:20
  • Yeah, I found it, the program make some child processes, and the child process do something about ipv6-table, when I kill -9 the parent process, the child processes will be killed too, so the ipv6-table make some mistake, then the ipv6 connection break, thank you for your help –  Apr 27 '12 at 15:05