Killing process on remote server


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Killing process on remote server
# 1  
Old 11-03-2014
Killing process on remote server

Hi guys,

I am trying to write a script that logs into a remote server as root and greps a particular process, awks its PID and then xargs kill -9 .

This is the actual command :

Code:
for i in `cat nodes.txt`
do
ssh root@$i 'ps -ef | grep puppet | grep -v grep | awk "{print $2}" |  xargs kill -9 '
done

It is not working as expected.

When I try to do ;

Code:
jan@server1 ~/scripts $ ssh root@server2 'ps -ef | grep puppet | grep -v grep |awk "{print $2}" '

I get :

Code:
root     10090 10089  0 Nov01 ?        00:00:00 /opt/puppet/bin/ruby /opt/puppet/bin/puppet agent --onetime --ignorecache --no-daemonize --no-usecacheonfailure --detailed-exitcodes --no-splay

It does not seem to be awking the PID # .

Does any one know the issue with the Syntax ?

Regards
# 2  
Old 11-03-2014
Quote:
Originally Posted by Junaid Subhani
Does any one know the issue with the Syntax ?
awk's program text definitely needs to be within single quotes, try
Code:
ssh root@server2 "ps -ef | grep puppet | grep -v grep |awk '{print $2}' "

# 3  
Old 11-03-2014
Seems like the same result.

Code:
jan@server1 ~ $ ssh root@server2 "ps -ef | grep puppet | grep -v grep |awk '{print $2}' "

Code:
root     10090 10089  0 Nov01 ?        00:00:00 /opt/puppet/bin/ruby /opt/puppet/bin/puppet agent --onetime --ignorecache --no-daemonize --no-usecacheonfailure --detailed-exitcodes --no-splay

Regards
# 4  
Old 11-03-2014
Try:

Code:
$ ssh root@server2 "ps -ef | grep puppet | grep -v grep |awk '{print \$2}' "

Or:

Code:
$ ssh root@server2 <<"EOF"
ps -ef | grep puppet | grep -v grep |awk '{print $2}'
EOF

This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 11-03-2014
man pkill

Hopefully there's never a user "puppet" on any of those machines...
This User Gave Thanks to achenle For This Post:
# 6  
Old 11-03-2014
Code:
$ ssh root@server2 "ps -ef | grep puppet | grep -v grep |awk '{print \$2}' "

This worked perfectly fine. Thank you !
# 7  
Old 11-03-2014
You should also try and avoid using kill -9 unless it is a last resort as this signal doesn't allow the process any opportunity to cleanup/save open files/flush buffers and the like.

Best practice is to use kill -15 which is basically a "request" for the process to close. If the process doesn't respond to this "request" in a timely manor then you can assume it is "stuck" and use kill -9.
This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script connect to remote server, not find files and exit only from remote server, but not from scrip

I have a script, which connecting to remote server and first checks, if the files are there by timestamp. If not I want the script exit without error. Below is a code TARFILE=${NAME}.tar TARGZFILE=${NAME}.tar.gz ssh ${DESTSERVNAME} 'cd /export/home/iciprod/download/let/monthly;... (3 Replies)
Discussion started by: digioleg54
3 Replies

2. Shell Programming and Scripting

Killing the process ID's

Hi , I have a list of application process id's. Is there a way to kill all the process listed below using the script, except the once which are starting with " Genesis " adm 1522 ABC_Process.tra adm 1939 Genesis_Process.tra adm 2729 Genesis_Archive.tra adm 3259 xyz_Process.tra (5 Replies)
Discussion started by: murali1687
5 Replies

3. UNIX for Dummies Questions & Answers

Killing a process which is running in different server

Consider two servers tst01 and tst02. i need to log in tst01 and check for the processes which run in tst02. then based on pid , i need to kill that process. is it possible to achieve? I am able to connect to tst02 using ftp. But the problem is, if i use ps it says invalid command. ... (3 Replies)
Discussion started by: pandeesh
3 Replies

4. Shell Programming and Scripting

Finding the age of a unix process, killing old processes, killing zombie processes

I had issues with processes locking up. This script checks for processes and kills them if they are older than a certain time. Its uses some functions you'll need to define or remove, like slog() which I use for logging, and is_running() which checks if this script is already running so you can... (0 Replies)
Discussion started by: sukerman
0 Replies

5. Shell Programming and Scripting

Help needed in killing a process

I'm trying to kill a process which is in sleep mode and the parent PID is 1 but I can't kill it with "kill -9" command. Is there a way to kill this process without rebooting? Any help will be appreciated. Steve (10 Replies)
Discussion started by: stevefox
10 Replies

6. Shell Programming and Scripting

Killing process!!!!

Hi friends, i m in big trouble.... i have one script which connects two server ...like below.. script1.sh ------------------------------------- bash test.sh & eval x=$@ export x=`echo $x` #echo $x # ssh user@8.2.5.6 bash /mbbv/location/script.sh $x|sed '/Binary file/d'... (1 Reply)
Discussion started by: Shahul
1 Replies

7. UNIX for Dummies Questions & Answers

killing the process

Hi, First, I am running a scipt.While the script is running I realize that I dont want the script to be run so I am killing the script externally.Before the process gets terminated or killed it should delete all the temporary files created by the script.How to do this?Can anyone help me? ... (3 Replies)
Discussion started by: arthi
3 Replies

8. Shell Programming and Scripting

Killing of a process and send a mail if the process doesnot come up within 2 minutes

Hi Friends, I am new to this forum as well as new to shell scripting. I have a problem here and i need someone to solve this. Let us consider there are two processes(abc & def).There is a script which kills these two processes(i.e killtheprocess abc). Here abc is the argument . There is a... (1 Reply)
Discussion started by: Prince89
1 Replies

9. UNIX for Dummies Questions & Answers

killing a process

I can kill running processes on my linux red hat system using ctrl-c but cannot do it from command line of another terminal using kill -2 pid. Although I can kill them from command line using kill -9 pid and other signals. I would like to do it using the kill -2 pid. Thanks for your suggestions (6 Replies)
Discussion started by: bbhayana
6 Replies

10. AIX

killing a process from a script

Hey all. I'm brand new to this forum and am looking for some help. I have a script that verifies that the backup tapes are working correctly. Basically is uses 1 command: restore -xpqvf > rootvglog I use this for each volume group that we have. We run this everyday but the problem is, we... (4 Replies)
Discussion started by: jalge2
4 Replies
Login or Register to Ask a Question