-   Programming (
-   -   [bash] killing processes using ssh (

Erhnam 01-25-2005 10:42 AM

[bash] killing processes using ssh
I want to manage some servers using ssh, so I wrote a couple of scripts. One of the scripts starts a screen under root and within the screen new processes will be started using one of the user acounts. Except starting, I also want to kill processes using ssh. On the local machine the command is:

kill -9 `ps -eaf | grep user01 | awk '{print $2}' | xargs`


ps -eaf | grep user01 | awk '{print $2}' | xargs kill

Output example when running: ps -eaf | grep user01

root 3710 1 0 12:47 ? 00:00:00 SCREEN -d -m -S server01 su user01 -c run
user01 3712 3711 0 12:47 pts/0 00:00:00 bash -c /home/user01/run
user01 3719 3712 0 12:47 pts/0 00:00:00 /bin/sh ./etc......
user01 3734 3719 3 12:47 pts/0 00:00:01 ./bin/sh ./etc......

As you can see I have multiple processes used by two different kinds of users. Below (and up) are the right commando's for killing the processes. I failed using any of the commands using ssh. I tried to add some quotes or ( ) but without any succes. I also tried pkill, but without any luck.. the screen proces keeps running! This is no good because I also have some scripts listing the screen status. Anyone here can tell me how to solve this problem?

ssh \
ps -eaf | grep user01 | awk '{print $2}' | xargs kill

twantrd 01-25-2005 12:15 PM

You're pretty close to the answer :). Here it is:

kill -9 `ps -ef | grep user01 | awk '{print $2}'`

For the ssh part, add your ssh statement before this.


Erhnam 01-26-2005 03:00 AM

Not working
I'm sorry to say.. This is not working. It seems that kill is trying to kills the processes on the local machine.

twantrd 01-26-2005 01:54 PM

The syntax with ssh should be this:

ssh remotehost_ip "(kill -9 `ps -ef | grep user01 | awk '{print $2}'`)"

Try that and let me know.


Erhnam 01-27-2005 03:20 AM

[root@apollo root]# ssh -p 7744 -l root "(kill -9 `ps -eaf | grep csuser01 | awk '{print $2}'`)"
bash: line 1: kill: (18272) - No such process
[root@apollo root]#

Nope.. Doesn't seems to work..


[root@apollo root]# ssh -p 7744 'netstat -ea | grep csuser01'
tcp        0      0 server01.domain:27015 *:*                    LISTEN      csuser01  38543591
udp        0      0 *:27015                *:*                                csuser01  38543588
udp        0      0 *:27020                *:*                                csuser01  38543589
udp        0      0 *:27005                *:*                                csuser01  38543590
[root@apollo root]#

twantrd 01-27-2005 05:58 PM

Interesting...i thought that would work. Never really killed a remote process before. Anyhow, I came up with a different solution (and I verified that it works). Just make a shell script on server01 to kill the process. Then ssh into server01 and execute that shell script.


jlliagre 01-27-2005 11:27 PM

This should work too:

ssh remotehost "eval kill '\$(ps -ef|grep user1|cut -f 2 -d\" \")'"

Erhnam 01-28-2005 02:28 AM

Thanks! This works! Could you explain what this exactly does?

kees-jan 01-28-2005 03:27 AM

All of this has to do with the order in which things are executed. If you type

ssh remotehost_ip "(kill -9 `ps -ef | grep user01 | awk '{print $2}'`)"
Then, bash will first execute

ps -ef | grep user01 | awk '{print $2}'
on the local machine, and then call ssh with a bunch of local process ids, which will not work.

The notation with $() is the same as putting things between backquotes ``.

The trick is to postpone execution of the backquotes stuff untill logged in at the remote end. To do this, you have three possible alternatives:[list=1][*]Put a backslash before the backquotes. Then the shell on the local machine will remove the backslash, and the remote end will do the actual execution[*]Put a backslash before the dollar sign. Same argument as above.[*]Put the entire thing within single quotes. Doesn't apply here, because the argument to awk is already between single quotes, and you can't nest them. Note that the argument to awk is between single quotes for exactly the same reason. If it weren't then the shell would try to evaluate the $2, and awk would never see it. If you were using cut instead of awk, then putting everything in single quotes would be a viable option.[/list=1]

eval is also a technique to postpone execution of a command, but I guess in this case it is redundant (though it doesn't hurt either).

Note that all of this is my interpretation of the bash manpage, I didn't actually test it.



jlliagre 01-28-2005 03:31 AM

It builds a command line that is to be executed on the remote host, while the previously posted scripts were executing the pid search on the local host.
If you want to understand the details, execute it first after replacing kill by echo, and second after removing the eval command.

Erhnam 01-31-2005 08:09 AM

I'm writing a php script that should perform the action as described above. The only problem is, php hangs on the use quotes.. Is it possible to rewrite the line below without the use of "quotes" ?

ssh remotehost "eval kill '\$(ps -ef|grep user1|cut -f 2 -d\" \")'"

nixcraft 01-31-2005 09:27 AM

LOL guys here is simple example to do task

ssh pkill -9 ľu username

jlliagre 01-31-2005 11:18 AM

nixcraft, you're right pkill is a far simpler answer to the question.
Just one comment, I wouldn't use "-9" which is bad behaviour.
-9 is only to be used on processes that won't die after being sent "regular" signals that allows them to shutdown properly.

nixcraft 01-31-2005 11:56 PM

You are right!

All times are GMT -5. The time now is 08:21 AM.