1752577 Members
5328 Online
108788 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