LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-25-2005, 10:42 AM   #1
Erhnam
Member
 
Registered: Jan 2005
Posts: 54

Rep: Reputation: 15
[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`

or

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 root@server01.domain.net \
ps -eaf | grep user01 | awk '{print $2}' | xargs kill
 
Old 01-25-2005, 12:15 PM   #2
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,438

Rep: Reputation: 52
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.

-twantrd
 
Old 01-26-2005, 03:00 AM   #3
Erhnam
Member
 
Registered: Jan 2005
Posts: 54

Original Poster
Rep: Reputation: 15
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.
 
Old 01-26-2005, 01:54 PM   #4
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,438

Rep: Reputation: 52
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.

-twantrd
 
Old 01-27-2005, 03:20 AM   #5
Erhnam
Member
 
Registered: Jan 2005
Posts: 54

Original Poster
Rep: Reputation: 15
[root@apollo root]# ssh -p 7744 -l root server01.domain.net "(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..

Code:
[root@apollo root]# ssh -p 7744 root@server01.domain.net '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]#
 
Old 01-27-2005, 05:58 PM   #6
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,438

Rep: Reputation: 52
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.

-twantrd
 
Old 01-27-2005, 11:27 PM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,507

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
This should work too:
Code:
ssh remotehost "eval kill '\$(ps -ef|grep user1|cut -f 2 -d\" \")'"
 
Old 01-28-2005, 02:28 AM   #8
Erhnam
Member
 
Registered: Jan 2005
Posts: 54

Original Poster
Rep: Reputation: 15
Thanks! This works! Could you explain what this exactly does?

Last edited by Erhnam; 02-01-2005 at 02:43 AM.
 
Old 01-28-2005, 03:27 AM   #9
kees-jan
Member
 
Registered: Sep 2004
Distribution: Debian, Ubuntu, BeatrIX, OpenWRT
Posts: 273

Rep: Reputation: 30
All of this has to do with the order in which things are executed. If you type
Code:
ssh remotehost_ip "(kill -9 `ps -ef | grep user01 | awk '{print $2}'`)"
Then, bash will first execute
Code:
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.

Groetjes,

Kees-Jan
 
Old 01-28-2005, 03:31 AM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,507

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
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.
 
Old 01-31-2005, 08:09 AM   #11
Erhnam
Member
 
Registered: Jan 2005
Posts: 54

Original Poster
Rep: Reputation: 15
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\" \")'"
 
Old 01-31-2005, 09:27 AM   #12
nixcraft
Member
 
Registered: Nov 2004
Location: BIOS
Distribution: RHEL3.0, FreeBSD 5.x, Debian 3.x, Soaris x86 v10
Posts: 379

Rep: Reputation: 30
LOL guys here is simple example to do task

ssh root@server.test.com pkill -9 ľu username
 
Old 01-31-2005, 11:18 AM   #13
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,507

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
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.
 
Old 01-31-2005, 11:56 PM   #14
nixcraft
Member
 
Registered: Nov 2004
Location: BIOS
Distribution: RHEL3.0, FreeBSD 5.x, Debian 3.x, Soaris x86 v10
Posts: 379

Rep: Reputation: 30
You are right!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Killing running processes hitest Suse/Novell 4 09-03-2005 02:22 PM
Killing processes hongman Linux - Newbie 3 02-28-2005 05:27 PM
Killing processes on Port 80 matux Linux - General 3 02-16-2005 09:15 PM
killing processes adriaanbw Linux - Software 3 05-24-2003 10:46 PM
Vm: Killing Processes tbhebe Linux - General 3 03-07-2002 09:51 AM


All times are GMT -5. The time now is 02:27 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration