LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 06-12-2008, 01:16 PM   #1
ddenton
Member
 
Registered: May 2007
Posts: 114

Rep: Reputation: 15
Killing processes remotely using SSH


Hello list. I have a script that remotely deploys code then kills a process so it can re-init using the new code. Here's the command I'm trying to get working from the script:

ssh deployer@192.168.0.104 'ERPMPROC=`ps -ef | grep top | awk '{ print $2}' | head -1` && kill $ERPMPROC'

In this case, just an example, it finds "top" and kills it. This command works locally (not using the ssh login part...), but when ssh'ing in from a remote machine, I get this error:

awk '{ print $2}' | head -1` && echo $ERPMPROC'

awk: cmd. line:2: (END OF FILE)
awk: cmd. line:2: syntax error

I'm sure the whole thing is failing because it only seems the first and second single quotes and ends the command at the second, instead of the fourth. I haven't had any luck in escaping the first set of single quotes.

Any help is appreciated...
 
Old 06-12-2008, 02:38 PM   #2
ramram29
Member
 
Registered: Jul 2003
Location: Miami, Florida, USA
Distribution: Debian
Posts: 848
Blog Entries: 1

Rep: Reputation: 47
Don't use a variable. I don't think you can set variables remotely. Try this:

ssh deployer@192.168.0.104 kill $(ps -ef | grep top | awk '{ print $2}' | head -1)
 
Old 06-12-2008, 03:30 PM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Your grep command might catch more than you want, such as an argument containing "desktop". If you want to kill the top command you could:
Code:
kill $(ps -ef | awk '/ top$/{print $2}')
or use "killall top".

Last edited by jschiwal; 06-14-2008 at 06:47 AM.
 
Old 06-13-2008, 08:56 AM   #4
ddenton
Member
 
Registered: May 2007
Posts: 114

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ramram29 View Post
Don't use a variable. I don't think you can set variables remotely. Try this:

ssh deployer@192.168.0.104 kill $(ps -ef | grep top | awk '{ print $2}' | head -1)
Thanks for the response. I gave your suggestion a shot, and the command seems to be looking locally instead of remotely. It fails with:
Code:
bash: line 0: kill: (8029) - No such process
Looking at the pid sequence on the local and remote machines, it looks like a pid from the local machine.

I tried using backticks instead of $() and this string simply getting the PID works:

Code:
ssh deployer@192.168.0.104 ``ps -ef | grep top | awk '{ print $2}' | head -1``
However when I try adding the kill command (sudo kill, since I'm not connecting as root) I get the following:

Code:
ssh deployer@192.168.0.104 sudo kill ``ps -ef | grep top | awk '{ print $2}' | head -1``
Password:********

kill: can't find process "ps"
kill: can't find process "-ef"
I think I found a very hurky way of doing this, but it isn't pretty. I tried running the remote grep for the PID and writing it to a file, hoping then I could use command sub to write the PID into a variable. Unfortunately, it writes the file locally, not on the remote machine. So I tried scp'ing the file to the remote machine, then use command sub to put the PID into a variable and use that variable to run kill:

Code:
ssh deployer@192.168.0.104 ``ps -ef | grep top | awk '{ print $2}' | head -1`` > process.pidfile
scp process.pidfile deployer@192.168.0.104:
ssh deployer@192.168.0.104 'ERPMPROC=`cat process.pidfile`; sudo kill $ERPMPROC'
This seems to work, but it introduces some complexity I'd rather not have. If you have any other suggestions on getting other options to work, I'd appreciate it...
 
Old 06-13-2008, 09:10 AM   #5
ddenton
Member
 
Registered: May 2007
Posts: 114

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jschiwal View Post
Your grep command might catch more than you want, such as an argument containing "desktop". If you want to kill the top command you could:
Code:
kill $(ps -ef | awk '/ top$/{print $2})'
or use "killall top".
Thanks for responding. I can't seem to get this syntax to work either. The single quote after the close paren is giving me the error. Running the command locally on the target machine works, but it doesn't work remotely. I moved the trailing paren to the outside and it doesn't error, but it doesn't create any output, it just logs me in.
Code:
ssh deployer@192.168.0.104 $(ps -ef | awk '/ top$/{print $2}')
Last login: Fri Jun 13 09:04:15 2008 from 192.168.0.2
Killall for my test using top does work, but the real-world application for this is a java process calling a jar file (the name for which is the string I'm grepping for). In this case the process running is java, and there's other java processes running that I can't kill. I haven't tested this exactly though...

Thanks again for the reply.
 
Old 06-14-2008, 07:06 AM   #6
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
I transposed those characters when I typed them in.

For gnu's version of ps, there is a -C option where you can select just the instance of the command. Also you can use the "killall" command to kill a process by name:

I tried this out to quit a top process running. Keep in mind that killall will cause all instances of top to quit.
ssh hpmedia "ps -C top && killall top || echo top isn't running"

I think that this example is more readable then using awk to filter the output of ps.

---

Sorry, I missed the last line on your response, and used "killall" again.

Last edited by jschiwal; 06-14-2008 at 10:28 AM.
 
Old 06-14-2008, 10:05 AM   #7
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 3,378

Rep: Reputation: 824Reputation: 824Reputation: 824Reputation: 824Reputation: 824Reputation: 824Reputation: 824
Have you looked at pgrep or pkill?
e.g. 'pgrep -f $ERPMPROC' should return the PID of the currently running process.
 
Old 09-02-2010, 07:35 PM   #8
jflo
LQ Newbie
 
Registered: Aug 2008
Posts: 2

Rep: Reputation: 0
TRY:

for i in server1 server2 server 3
do
pid=$(ssh $i ps -ef |grep top | awk '{ print $2 }' | head -1)
echo $pid
ssh $i kill $pid
done
 
Old 12-30-2013, 08:22 PM   #9
Andreii
LQ Newbie
 
Registered: Dec 2013
Posts: 1

Rep: Reputation: Disabled
Minor modification of
Quote:
Originally Posted by ramram29 View Post
Don't use a variable. I don't think you can set variables remotely. Try this:

ssh deployer@192.168.0.104 kill $(ps -ef | grep top | awk '{ print $2}' | head -1)
Code:
ssh deployer@192.168.0.104 "kill \$(ps -ef | grep top | awk '{ print \$2}' | head -1)"
worked for me
 
  


Reply

Tags
command, kill, remotely, ssh


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to kill processes using ssh remotely rajaniyer123 Solaris / OpenSolaris 3 04-13-2008 01:22 AM
to know how many users are there and killing some of process started remotely manoj111 Fedora 1 04-03-2007 02:16 PM
Killing processes hongman Linux - Newbie 3 02-28-2005 05:27 PM
[bash] killing processes using ssh Erhnam Programming 13 01-31-2005 11:56 PM


All times are GMT -5. The time now is 11: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