I have a Linux computer that I remotely SSH into using plink on a Windows XP
machine. I have a 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?
pkill
instead of theps|grep|...
pipeline you're using now.pkill
is immune to the problem you noted, wherebygrep
on the process list finds thegrep
process itself. (There are tricks to avoid that, butpkill
is cleaner, IMO.) – Steven Monday May 18 '13 at 01:50pgrep -f /path/of/programm
– Rahul Patil May 18 '13 at 01:54killall -9 programmname
orpkill
as Steven suggested. – Rahul Patil May 18 '13 at 02:01