• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Unix Scripting

reicherb

Platinum Member
I am trying to write a script that will execute different degrees of the kill command until a process is killed. Without just using -9.

This is what I have so far. I doen't work. Can anybody tell me what Ive done wrong?

Thanks

x = 1
while &quot;x <= 4&quot;
do

case $* in
1) /bin/kill -2 $*
break ;;
2) /bin/kill -3 $*
break ;;
3) /bin/kill -6 $*
break ;;
4) /bin/kill -9 $*
esac
x = x + 1
done
 
What shell are you using? This is the format I usually use in a c shell script on my Solaris 2.6 boxes. The ProcessName is the name of the process that you are trying to kill as it is displayed by &quot;ps&quot;. Using the &quot;|&quot; you can kill multiple processes. Let me know if this works for you :

#####

for pid in `ps -eo pid,args | egrep '(ProccessName|ProcessName2|ProcessName3)' \
| egrep -v grep | tee /usr/tmp/x | awk '{ print $1}' `; do
echo kill -2 $pid
kill -2 $pid
done

for pid in `ps -eo pid,args | egrep '(ProccessName|ProcessName2|ProcessName3)' \
| egrep -v grep | tee /usr/tmp/x | awk '{ print $1}' `; do
echo kill -9 $pid
kill -9 $pid
done

#####

You can replace the ProcessName with the variables passed in from the command line.

As far as what is wrong with your script, have you tried ...

without the quotes:

x = 1
while x <= 4
do

or with parentheses instead:

x = 1
while (x <= 4)
do

 
I am using the sh shell. I tried your suggestions for fixing mine and it didn't make a difference.

Thanks for the help,
 
Hmm... have you tried my script to see if that works for you? The arguments passed to the script should be the name of the process, not the process ID.

for pid in `ps -eo pid,args | egrep '($1|$2|$3)' \
| egrep -v grep | tee /usr/tmp/x | awk '{ print $1}' `; do
echo kill -2 $pid
kill -2 $pid
done

for pid in `ps -eo pid,args | egrep '($1|$2|$3)' \
| egrep -v grep | tee /usr/tmp/x | awk '{ print $1}' `; do
echo kill -9 $pid
kill -9 $pid
done


Or if you already know the process ID :

while [ $# -gt 0 ]
do
echo &quot;Attempting to kill process $1 with -2 ...&quot;
kill -2 $1
sleep 2

echo &quot;Attempting to kill process $1 with -3 ...&quot;
kill -3 $1
sleep 2

echo &quot;Attempting to kill process $1 with -6 ...&quot;
kill -6 $1
sleep 2

echo &quot;Attempting to kill process $1 with -9 ...&quot;
kill -9 $1
sleep 2

shift
done
 
do a &quot;man expr&quot;. you need to use expr for math functions:

x=`expr $x -1`

or something like that...

-Nexus9

 
I have &quot;Unix Shells by Example&quot; by Ellie Quigley. It's a pretty good book. She also has a &quot;Perl by Example&quot; book that I am thinking of picking up.

I just found the SHELLdorado site while look for some clues to his problem ... I leave all of my books at work for reference. 🙂
 
This one that you wrote works.

while [ $# -gt 0 ]
do
echo &quot;Attempting to kill process $1 with -2 ...&quot;
kill -2 $1
sleep 2

echo &quot;Attempting to kill process $1 with -3 ...&quot;
kill -3 $1
sleep 2

echo &quot;Attempting to kill process $1 with -6 ...&quot;
kill -6 $1
sleep 2

echo &quot;Attempting to kill process $1 with -9 ...&quot;
kill -9 $1
sleep 2

shift
done


and so does one that I wrote.

if [ &quot;ps | grep $*&quot; != NULL ]
then /bin/kill -2 $*
fi

if [ &quot;ps | grep $*&quot; != NULL ]
then /bin/kill -3 $*
fi

if [ &quot;ps | grep $*&quot; != NULL ]
then /bin/kill -6 $*
fi

if [ &quot;ps | grep $*&quot;i != NULL ]
then /bin/kill -9 $*
fi

But if the first kill works then the others all come back with errors. I put the != NULL hoping to remedy that problem but it didn't work.
 
Well I get the variable problems figured out but not it doesn't like the case syntax.
I get
1: not found


x=1
while ($x <= 4)
do
for x
do
case $x in
1) /bin/kill -2 $*
break ;;
2) /bin/kill -3 $*
break ;;
3) /bin/kill -6 $*
break ;;
4) /bin/kill -9 $*
esac
x=`expr $x + 1`
done
done
 
Add a -x after the first line of the shell script (#!/bin/sh -x). Turns on debugging. You'll see exactly where it fails.
 
This is what I get.

bash$ die 3787
+ x=1
+ 1 4
1: not found
bash$

I assume that it is failing at case 1. Any ideas why?
 
That didn't work and ofcourse there are no man pages on case. I tried all of the following and none made any difference.

case &quot;$x&quot; in
&quot;1&quot😉 /bin/kill -2 $*

case &quot;$x&quot; in
`1`) /bin/kill -2 $*

case &quot;$x&quot; in
1🙂 /bin/kill -2 $*

case &quot;$x&quot; in
&quot;1:&quot😉 /bin/kill -2 $*

Is there something I am missing with the case statements?
 
I'm wondering if there's a problem with giving a case statement an integer. I know it takes string variables. Hmmm....
 
Does it have to use a case statement? I tried this with the ksh shell.

for x in 2 3 6 9
do
kill -&quot;$x&quot; $*
if [ $? -ne 2 ]
then exit
fi
done
 
I don't see why it would have to be a case statement. The beauty of UNIX. There is always more than one way to do it. Hey, that sounds like PERL. 😉
 
Back
Top