1826243 Members
3029 Online
109692 Solutions
New Discussion

Re: user process

 
SOLVED
Go to solution
ust3
Regular Advisor

user process

I hv existing script to generate some pid to a file, the below is the format of the file ,

$vi pid_file
15484
4521

now I would like to have a script to kill all process , I would like to use the command "kill" first , if can't kill it , then use "kill -1" , if still can't then use "kill -3" , if still not then send mail to administrator , can advise what can i write the script ? thx .
23 REPLIES 23
Shrikant Lavhate
Esteemed Contributor

Re: user process

Here is logic for what you want:

Read file under script and use kill process under case structure with default case to sendmail. Use echo $? to check id process is killed or not and then accordingle proceed.
Will it remain a personal, if I broadcast it here!
Roberto Arias
Valued Contributor

Re: user process

hi ust3:

try this:

if -z pid_file
then
for i in `cat pid_file`
do
kill $i
done
fi
if -z pid_file
then
mailx ....
fi

or this:


kill $(ps -fu |awk {'print $2'})

regards
The man is your friend
Jeeshan
Honored Contributor

Re: user process

first think which process and which user process you want to kill.

Here is a command with arguments which kill all user process at a glance

#ps -ef | awk '{print $1" "$2}' | grep | awk '{print $NF}' | xargs kill

be careful to use this command.
a warrior never quits
Dennis Handly
Acclaimed Contributor
Solution

Re: user process

Try something like this. Remove echo before kill if happy. Add a "false" after the echo to go to the next step.
for PID in $(< pid_file); do
kill -0 $PID 2> /dev/null
if [ $? -ne 0 ]; then
echo "PID $PID is not alive"
continue
fi
echo kill $PID
false # remove after script tested
if [ $? -eq 0 ]; then continue; fi
echo kill -INT $PID
if [ $? -eq 0 ]; then continue; fi
echo kill -QUIT $PID
if [ $? -eq 0 ]; then continue; fi
echo mailx -s "Problem killing process" root <Problem killing $PID.
EOF
done
ust3
Regular Advisor

Re: user process

thx replies,

I am trying the script
for PID in $(< pid_file); do
kill -0 $PID 2> /dev/null
if [ $? -ne 0 ]; then
echo "PID $PID is not alive"
continue
fi
echo kill $PID
false # remove after script tested
if [ $? -eq 0 ]; then continue; fi
echo kill -INT $PID
if [ $? -eq 0 ]; then continue; fi
echo kill -QUIT $PID
if [ $? -eq 0 ]; then continue; fi
echo mailx -s "Problem killing process" root <Problem killing $PID.
EOF
done

But I found the process only run the first part

echo $PID
kill -0 $PID 2> /dev/null
if [ $? -ne 0 ]; then
echo "PID $PID is not alive"
continue
fi

what I want is if the parocess is not killed , then run kill -1 , then kill -3 , if still not , then send me a mail , please help again. thx

Dennis Handly
Acclaimed Contributor

Re: user process

>But I found the process only run the first part
kill -0 $PID 2> /dev/null
if [ $? -ne 0 ]; then
echo "PID $PID is not alive"
continue
fi

If it only does this, then that PID doesn't exist.

>what I want is if the process is not killed, ...

That part is there.
ust3
Regular Advisor

Re: user process

thx Dennis ,

I tried the script , it try to kill the PID , but if it fail to kill , it pop the message PID is not alive , it works fine , but it seems not try to kill again by kill -1 , it seems the script is stopped at that time , and do not run the rest part of it (eg. if [ $? -eq 0 ]; then continue; fi
echo kill -INT $PID) , can advise what is wrong ? thx


Dennis Handly
Acclaimed Contributor

Re: user process

>but if it fail to kill, it pop the message PID is not alive

The "kill -0", doesn't kill it, it checks the PID existance.

>but it seems not try to kill again by kill -1, it seems the script is stopped at that time

If the PID isn't there, it does no good to try to kill it.
To prove this, you could add: ps -fu $PID
ust3
Regular Advisor

Re: user process

thx dennis,

I have problem to use kill -0 to check the existence of pid , can advise if I want to skip this checking , what I want is to kill the pid by kill -1 first , if can't then kill -2 , then kill -3 , can advise can can I modify it? thx

how to skip the below part from the script
==========================================
kill -0 $PID 2> /dev/null
if [ $? -ne 0 ]; then
echo "PID $PID is not alive"
continue
fi
Dennis Handly
Acclaimed Contributor

Re: user process

>can advise if I want to skip this checking

You can just remove this "kill -0 if ... fi" block of code. But you'll get a mail message.
ust3
Regular Advisor

Re: user process

thx dennis ,

I am not understand what is the use the that part of script

===========================================
kill $PID 2> /dev/null
if [ $? -eq 0 ]; then continue; fi
===========================================
is it mean ?
if it fail to kill the PID , then its output = 0 , then do next loop kill -INT ?
but in my mind , the logic should be if it is successful kill PID , the output should be = 0 , that should not go to next loop .


Dennis Handly
Acclaimed Contributor

Re: user process

>I am not understand what is the use the that part of script
kill $PID 2> /dev/null
if [ $? -eq 0 ]; then continue; fi
>if it fail to kill the PID, then its output = 0, then do next loop kill -INT?

If kill works, its exit status is 0 and then it continues to the next iteration of the loop.
(This is goto/break/continue logic, not scummy nested if.)

>the logic should be if it is successful kill PID, the output should be = 0, that should not go to next loop.

It doesn't go to the next kill -INT but does the next PID.
ust3
Regular Advisor

Re: user process

thx dennis,

for PID in $(< pid_file); do
kill -0 $PID 2> /dev/null
if [ $? -ne 0 ]; then
echo "PID $PID is not alive"
continue
fi
echo kill $PID
false # remove after script tested
if [ $? -eq 0 ]; then continue; fi
echo kill -INT $PID
if [ $? -eq 0 ]; then continue; fi
echo kill -QUIT $PID
if [ $? -eq 0 ]; then continue; fi
echo mailx -s "Problem killing process" root <Problem killing $PID.
EOF
done

I tried the script , I hve edit the pid_file , some of pid are exist and some are non-exist , but when the script , it shows are pid is not alive .

PID 58745 is not alive
PID 15645 is not alive
PID 87852 is not alive
PID 15645 is not alive
PID 14454 is not alive


while PID 15645 is exist in my system , can advise what is wrong ? thx
ust3
Regular Advisor

Re: user process

I tried

kill -0 15645 , the result is "Operation not permitted" .

thx
Dennis Handly
Acclaimed Contributor

Re: user process

>kill -0 15645, the result is "Operation not permitted"

Something wrong here. I don't get that on HP-UX. You may get "permission denied".
And if these are the same, it means you need to be root to kill that process.
ust3
Regular Advisor

Re: user process

thx dennis ,

if use root to kill -0 , it works, I would like to further ask , if there are many process in the file pid_file , some are of process are alive , some are not alive , the script will still kill the process are alive ?

thx
Dennis Handly
Acclaimed Contributor

Re: user process

>the script will still kill the process are alive?

Yes. But if your file has data from hours and days ago, you might kill the wrong process, because PIDs are eventually reused.
ust3
Regular Advisor

Re: user process

thx Dennis ,

Yes. But if your file has data from hours and days ago, you might kill the wrong process, because PIDs are eventually reused -->

I will remove pid_file after it kill the process , the new pid_file will be generated if it is required to kill .

I would like to confirm you whether the logic is :
it first read all pid in pid_file (kill -0) , it try to kill it by "kill" , if successful to kill , then do nothing , if unsuccessful to kill , then try kill -INT , if still not success , then try kill -QUIT , if still not success , then mailx , is the logic correct ? thx
Dennis Handly
Acclaimed Contributor

Re: user process

>I would like to confirm you whether the logic is:
>it first read all pid in pid_file (kill -0),

It reads them all in the shell but it does them one by one.

>it try to kill it by "kill", if successful to kill, then do nothing, if unsuccessful to kill, then try kill -INT, if still not success, then try kill -QUIT, if still not success, then mailx, is the logic correct?

Yes, if you remove those debugging "echo"s and one "false".
ust3
Regular Advisor

Re: user process

thx Dennis ,

I am thinking how to test it , I hv try to use the script to kill the PID , it can kill all process I created , can advise if I want to test to kill a process that is "never be killed" that means it can not be killed , so that I can test the mail will be sent to me , can advise what pid like that ? thx
Dennis Handly
Acclaimed Contributor

Re: user process

>I am thinking how to test it,

I put those "false" calls there.

>can advise if I want to test to kill a process that is "never be killed" that means it can not be killed, so that I can test the mail will be sent to me, can advise what pid like that?

You can try PID 1, that can't be killed.
Or you could call signal for SIGINT and SIGQUIT and use SIG_IGN. (You can still use kill -KILL on that process.)
ust3
Regular Advisor

Re: user process

thx dennis ,

I try to kill 1 , I know the process is not killed , but do not have any message , is there other process that i can test ? thx
Dennis Handly
Acclaimed Contributor

Re: user process

>I try to kill 1, I know the process is not killed, but do not have any message

Are you root? If you aren't root, it will say you can't kill it, with that kill -0.
You can of course comment out the kill -0 so it goes to the mailx.