Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

I have a Linux computer that I remotely SSH into using plink on a Windows XP machine. I have the Plink program set to execute commands in a script using automated login:

plink -ssh [domain name] -l [username] -pw [password] -m commands.txt >> Outputtext.txt

commands.txt only contains the following

echo "Killing process"
ps -ef | grep 'processname' | awk ‘{print $2}’ | head -n 1` | xargs kill -9
sleep 2
echo "Restarting process"
processd &

echo "Exiting..."

This script simply searches for a processname kills it then restarts it. It works everytime if the process is actually started on linux before I execute the script. However, if the process is NOT started it will actually kill the controlling terminal and exit plink.exe right on the ps -ef command.

I've narrowed it down to when I'm doing the ps -ef | grep 'processname' it will return two PID's one is the process I'm searching for, and the other is the grep processname PID itself

When the process isn't started it will only return one PID the grep 'processname' and I think kill -9 is killing that and somehow killing plink connection or the controlling terminal? Is there any way around this?

or to check if the grep 'processname' returns 2 lines meaning the process started, or if there is only 1 line then don't kill it.

Not sure if I can echo "#!bash" a bash script out and then have the bash script execute?

Or is this a weird bug in plink?

share|improve this question

marked as duplicate by Gilles, vonbrand, Hauke Laging, jasonwryan, slm May 19 '13 at 1:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2  
If possible, I'd recommend using pkill instead of the ps|grep|... pipeline you're using now. pkill is immune to the problem you noted, whereby grep on the process list finds the grep process itself. (There are tricks to avoid that, but pkill is cleaner, IMO.) –  Steven Monday May 18 '13 at 1:50
    
what about pgrep -f /path/of/programm –  Rahul Patil May 18 '13 at 1:54
    
yes, also you can use killall -9 programmname or pkill as Steven suggested. –  Rahul Patil May 18 '13 at 2:01

2 Answers 2

Your command line seems really weird to me:

ps -ef | grep 'processname' | awk ‘{print $2}’ | head -n 1 | xargs kill -9
  1. You feed awk (i.e. a program which can perfectly filter its input, the much mightier program) with the output of grep.
  2. You pipe the output of awk (i.e. a program which can perfectly filter its output) into head.
  3. You use xargs for definitely just one argument.

So independently of better approaches (and this question being a duplicate) I feel obliged to correct this:

kill -9 $(ps -ef | awk '/processname/ {print $2;exit;}')
share|improve this answer
    
Good catch, even though if we're strict, piping ps -ef to grep is in fact redundant considering that ps provides the -C option just for this purpose. (see also Ignacio's answer) –  syntaxerror Oct 25 at 3:23

Restrict ps to only find the command and output the info you care about.

ps -C processname -o pid

You may want to check the result code of the command before trying to actually kill anything though.

share|improve this answer
    
Oh thank you! I have another (related) question. I dont have disown or nohup installed on this linux machine, and the process I start through the plink script is bound to the plink (terminal). Is there anyway to start the process from the plink session so that the output could be available to any connecting terminal? Because if the plink session ends, the output from the started process doesn't display on another terminal for some reason only the original terminal that started the process –  zacharoni16 May 18 '13 at 1:58
    
Run the command in a detached screen. –  Ignacio Vazquez-Abrams May 18 '13 at 1:59
    
not sure I understand? –  zacharoni16 May 18 '13 at 1:59
    
    
ah sh: Command not found? wow this linux box doesn't have anything installed nohup, screen, disown, its an openlinux embedded system –  zacharoni16 May 18 '13 at 2:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.