Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Apr 28 '12 at 5:27

This question came from our site for professional and enthusiast programmers.

    
Wrong site. It is an admin issue, not a programming issue. –  glglgl Apr 27 '12 at 8: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? –  wroniasty Apr 27 '12 at 8:49
    
@glglgl it is kind of related to shell programming :) –  wroniasty Apr 27 '12 at 8:49

2 Answers 2

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
share|improve this answer
    
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 –  yanshuyuan Apr 27 '12 at 11:32
    
What is your program doing? Can you paste your program here if small or in pastebin? –  Pavan Manjunath 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 –  yanshuyuan Apr 27 '12 at 15:05

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.

share|improve this answer
    
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. –  yanshuyuan 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. –  Dot Cink 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? –  yanshuyuan 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 ? –  yanshuyuan 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. –  Dot Cink Apr 27 '12 at 12:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.