1833802 Members
2550 Online
110063 Solutions
New Discussion

User process mode

 
SOLVED
Go to solution
peterchu
Super Advisor

User process mode

I have the below script to kill the user who idle for 180 minutes, it work fine , if I want to have one more checking - if the process is in "Runing" mode not in "Sleep" mode , then the process will not be killed ( that mean only kill the "Sleep" mode process ) could suggest how can I modify the script ? thx.


set -x
for user in `who -u | awk '{ print $6":"$7 }' | grep -v "\."| awk -F : '{ print ($1*60)+$2":"$3 }'`
do

time=`echo $user | awk -F ":" '{ print $1 }'`
pid=`echo $user | awk -F ":" '{ print $2 }'`

if [[ $time -gt 180 ]]
then
kill -1 $pid 2 >> /dev/null
fi
5 REPLIES 5
Hoang Minh Tuan
Regular Advisor
Solution

Re: User process mode

Hi,

You can add to your script :

state=`ps -el |grep $pid | awk '{print $2}'
if [[ $state == S ]] #compare string
then

kill...

fi

HMT
Bus wait at bus station, Work wait at my work station
peterchu
Super Advisor

Re: User process mode

thx reply,

I modified the script to as below , but it seems not kill any user , could suggest what is wrong ? thx


set -x
for user in `who -u | awk '{ print $6":"$7 }' | grep -v "\."| awk -F : '{ print
($1*60)+$2":"$3 }'`
do

time=`echo $user | awk -F ":" '{ print $1 }'`
pid=`echo $user | awk -F ":" '{ print $2 }'`
state=`ps -el |grep $pid | awk '{print $2}'`
if [[ $state == S ]] #compare string
then
if [[ $time -gt 180 ]]
then
kill -1 $pid 2 >> /dev/null
fi
fi
done 2>> /dev/null
Hoang Minh Tuan
Regular Advisor

Re: User process mode

Could you show me the log file (>> /tmp/log, not null).
you change to ps -efl (not ps -el)

HMT
Bus wait at bus station, Work wait at my work station
Hoang Minh Tuan
Regular Advisor

Re: User process mode

Sorry, I give you the incorrect script. I was confused about comparation string in shell script and because my internet be disconnected so I post this script not sooner for you. This is the correct

set -x
for user in `who -u | awk '{ print $6":"$7 }' | grep -v "\."| awk -F : '{ print
($1*60)+$2":"$3 }'`
do

time=`echo $user | awk -F ":" '{ print $1 }'`
pid=`echo $user | awk -F ":" '{print $2}'`
state=`ps -el |grep $pid | awk '{print $2" "$4}'| grep $pid | awk '{print $1}'` #catch pid state

if [ $state=S ] #compare string
then
if [[ $time -gt 180 ]]
then
kill -1 $pid >> /dev/null
fi
fi
done

HMT
Bus wait at bus station, Work wait at my work station
Mark Ellzey
Valued Contributor

Re: User process mode

Peter,

Keep in mind that you are looking only at a snapshot of the process table when you do a 'ps'. All process sleep while waiting for their CPU time-slice, so the chances of your script finding a user's sleeping process is quite high. But since you are also defining the idle time limit at 180 minutes, the state of the process is somewhat of a moot point.

Just my 2cts worth,
Mark