PDA

View Full Version : Unix Heeeeelp !!!!



Burzhui
11-01-2001, 10:41 AM
Dudes and dudettes i need to write a script by the end of today for unix korn shell.

this is what it should do, you know how when you do a ps command it gives you the process id and name and then you have to do kill and then the process id???

well i need to write a script that will kill the process if you know the name of it.

Such as you are running process bob

so instead of going through ps getting id and then doing kill id number for bob

do murder(script name) bob


if someone knows how to do this i would appr3eciate it.

DoPeY5007
11-01-2001, 10:51 AM
isn't this a Software & OS question...


:heh: :P :bonk:

attgig
11-01-2001, 10:54 AM
i say...
grep the ps command into a stream (or a file, or whatever) and find the line with 'bob' (use -e)
extract the pid from the line (it's always the first number right? - search for space, and chop off the rest of the line)
then run a kill command on that pid
cheesy way to do it...but i think it should work...

coleslaw
11-01-2001, 10:54 AM
This should go in the Software & OS forum, but I will try to answer it.

You could use the "ps -ax | grep (process name)" in your script to find the process that you are trying to kill by name.

Actually, you might try doing:


ps -ax | grep (process name) > process.txt

to pass the process line into a file and then run a perl script on that text file to extract the physical process number. Then, spit the process number out into a simple kill -9 [process id] command.

Burzhui
11-01-2001, 11:30 AM
Sorry for posting in the wrong place but there were several programming questions here before, so i though why not.

In any case thanks a lot, if anybody knows a way to not redirect the output into a file then please post the whole code.. thanks

Burzhui
11-01-2001, 01:02 PM
ok i can't do it through a file any other ideas

atomicdog
11-01-2001, 02:45 PM
just use the killall command

Burzhui
11-01-2001, 08:12 PM
no that won't work

i need a script that will get the pid... and well you know the rest

coleslaw
11-01-2001, 08:14 PM
Why are you needing this script? Is it an assignment?

coleslaw
11-01-2001, 08:25 PM
With a little Google action, I found this:



#!/bin/ksh
# killprog: kill a program by name instead of by process id.
# If there is more than one copy, display a list, and
# ask to pick the right one.
# Author: Fred Brunet

Kill=/tmp/killprogout.$$
if [ $# -eq 0 ]
then
printf "Usage: killprog name\n"
exit 1
fi

# eliminate present program and 'grep $1' from process
ps -ef | grep -v "grep $1"|grep -v $0| grep $1 > $Kill
Count=`wc -l $Kill | awk '{ print $1 }'`
if [ $Count -ge 1 ]
then
awk ' { printf "%2d : %s\n", NR, $0 }' < $Kill
else
printf "No matches to kill\n"
if [ -f $Kill ]
then
rm $Kill
fi
exit 1
fi
printf "Which of the above do you mean: enter number or '0' to quit: "

while true
do
read lineno
# non-numeric data returns 0
lcnt=`echo $lineno|awk ' { if ($0 ~ /[^0-9]/ )
print 0
else
print 1 } '`
if [ $lcnt -eq 0 ]
then
printf "Valid input: 1-$Count or '0' to quit. Reenter number: "
continue
fi

# default to 0 and quit
lineno=${lineno:=0}
if [ $lineno -eq 0 ]
then
if [ -f $Kill ]
then
rm $Kill
fi
exit 1
fi

if [ $lineno -gt $Count -o $lineno -lt 0 ]
then
printf "Valid input: 1-$Count or '0' to quit. Reenter number: "
else
break
fi
done

Flist=`sed -n "${lineno}p" $Kill`
echo $Flist | awk '{ printf "%s\n", $0 }'
Killproc=`echo $Flist | awk '{ print $2 }'`
printf "Kill ($Killproc) ? y/n "
read answer
case $answer in
y|Y) kill $Killproc
printf "$Killproc terminated!\n";;
*)
printf "Nothing killed\n";;
esac
if [ -f $Kill ]
then
rm $Kill
fi
# end killprog


Sample output:


$ killprog vi
1 : fred 12075 11088 0 13:57:14 p0 0:00 vi
2 : fred 12102 11088 0 13:57:41 p0 0:00 vi untab
Which of the above do you mean: enter number or '0' to quit: 1
fred 12075 11088 0 13:57:14 p0 0:00 vi
Kill (12075) y/n ? y
12075 terminated!


Very sexy script. I will have to use that one! :thumb:

Burzhui
11-01-2001, 08:32 PM
you rule thanks a lot dude

me very happy :D

coleslaw
11-01-2001, 08:33 PM
You could, of course, modify it so that it does not prompt for verification, etc., but I will leave that up to you.

Burzhui
11-02-2001, 09:02 AM
#!/bin/ksh

pid=$(UNIX95=1 ps -eo pid,comm | grep " $1\$" | awk '{print $1}')
[[ -n $pid ]] && kill $pid


Here we go isn't that so much simpler

coleslaw
11-02-2001, 11:27 AM
sexy simple.

Burzhui
11-02-2001, 11:36 AM
Originally posted by coleslaw
sexy simple.

Ain't it though? aint it?

Markel
11-02-2001, 08:10 PM
No wonder Unix causes brain damage. :rolleyes: