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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-25-2005, 10:42 AM
|
#1
|
|
Member
Registered: Jan 2005
Posts: 54
Rep:
|
[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
|
|
|
|
01-25-2005, 12:15 PM
|
#2
|
|
Senior Member
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,430
Rep:
|
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
|
|
|
|
01-26-2005, 03:00 AM
|
#3
|
|
Member
Registered: Jan 2005
Posts: 54
Original Poster
Rep:
|
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.
|
|
|
|
01-26-2005, 01:54 PM
|
#4
|
|
Senior Member
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,430
Rep:
|
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
|
|
|
|
01-27-2005, 03:20 AM
|
#5
|
|
Member
Registered: Jan 2005
Posts: 54
Original Poster
Rep:
|
[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]#
|
|
|
|
01-27-2005, 05:58 PM
|
#6
|
|
Senior Member
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,430
Rep:
|
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
|
|
|
|
01-27-2005, 11:27 PM
|
#7
|
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OL
Posts: 9,311
|
This should work too:
Code:
ssh remotehost "eval kill '\$(ps -ef|grep user1|cut -f 2 -d\" \")'"
|
|
|
|
01-28-2005, 02:28 AM
|
#8
|
|
Member
Registered: Jan 2005
Posts: 54
Original Poster
Rep:
|
Thanks! This works! Could you explain what this exactly does?
Last edited by Erhnam; 02-01-2005 at 02:43 AM.
|
|
|
|
01-28-2005, 03:27 AM
|
#9
|
|
Member
Registered: Sep 2004
Distribution: Debian, Ubuntu, BeatrIX, OpenWRT
Posts: 273
Rep:
|
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
|
|
|
|
01-28-2005, 03:31 AM
|
#10
|
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OL
Posts: 9,311
|
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.
|
|
|
|
01-31-2005, 08:09 AM
|
#11
|
|
Member
Registered: Jan 2005
Posts: 54
Original Poster
Rep:
|
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\" \")'"
|
|
|
|
01-31-2005, 09:27 AM
|
#12
|
|
Member
Registered: Nov 2004
Location: BIOS
Distribution: RHEL3.0, FreeBSD 5.x, Debian 3.x, Soaris x86 v10
Posts: 379
Rep:
|
LOL guys here is simple example to do task
ssh root@server.test.com pkill -9 –u username
|
|
|
|
01-31-2005, 11:18 AM
|
#13
|
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OL
Posts: 9,311
|
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.
|
|
|
|
01-31-2005, 11:56 PM
|
#14
|
|
Member
Registered: Nov 2004
Location: BIOS
Distribution: RHEL3.0, FreeBSD 5.x, Debian 3.x, Soaris x86 v10
Posts: 379
Rep:
|
You are right!
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 02:59 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|