LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 11-18-2010, 02:11 PM   #1
jasonws
LQ Newbie
 
Registered: Oct 2010
Location: Vermont, USA
Posts: 15

Rep: Reputation: 0
kill all idle user processes


Hi, all the kill idle user processes scripts I've seen don't take into account that the user might have multiple sessions open. Such is the case with one of our clients. Currently, every hour or two I need to do the following:

This will get the TTY and idle time for all users.
w |grep pts/ | tr -s ' ' | cut -d " " -f2,5

For each idle time over a half hour, I do the following (TTY is the TTY from the previous command with a space ("$TTY ")):
ps -ef |grep TTY | tr -s ' ' | cut -d " " -f2

I then kill those processes.

There must be a way to do this automatically in a bash or perl script. I've tried both, but can't seem to get things to work properly.

Any suggestions?

Thanks! -Jason
 
Old 11-19-2010, 07:12 AM   #2
norbert74
Member
 
Registered: Apr 2006
Posts: 63

Rep: Reputation: 23
Is this what you want to do?

Code:
#!/bin/bash

# max time in minutes
MAXTIME=2

OLDIFS=$IFS
IFS='
'

# get idle times of ttys
for i in `w | sed '1,3d' | awk '{ print $2,$4 }' | sed 's/:.*$//' | sed -e '/^.*[0-9]s$/d'`;
do
  idletime=`echo $i |awk '{ print $2 }'`

  # if idle time is greater than max time, get corresponding PIDs of these ttys
  if [ $idletime -gt $MAXTIME ];
  then
    TTYTOKILL=`echo "$i" | awk '{print $1}' | sed 's/^.*\///'`
    PIDSTOKILL=`ps -t $TTYTOKILL | awk '{print $1}' | sed '1d'`

    # kill processes
    for ps in $PIDSTOKILL;
    do
        echo -n "killing: "
        ps -f -p $ps | sed '1d'
        kill -9 $ps
    done

  fi
done
IFS=$OLDIFS
 
Old 11-19-2010, 04:09 PM   #3
jasonws
LQ Newbie
 
Registered: Oct 2010
Location: Vermont, USA
Posts: 15

Original Poster
Rep: Reputation: 0
Thanks for your help! I really appreciate it! I got it to work with a few modifications. The most significant one is that the idle time on my system is in column 5. Here's the modified script which checks for any processes over half an hour (misses all #:##m idle times under 30 hours), kills them all at once, then kill -9 them all 10 seconds later.

Take care,
Jason

#!/bin/bash

# max time in minutes
MAXTIME=30

OLDIFS=$IFS
IFS='
'

# get idle times of ttys
for i in `w | sed '1,3d' | awk '{ print $2,$5 }' | sed 's/:.*$//' | sed -e '/^.*[0-9]s$/d'`;
do
idletime=`echo $i |awk '{ print $2 }'`

# if idle time is greater than max time, get corresponding PIDs of these ttys
if [ $idletime -gt $MAXTIME ];
then
TTYTOKILL=`echo "$i" | awk '{print $1}' | sed 's/^.*\///'`
NEWPIDSTOKILL=`ps -t $TTYTOKILL | awk '{print $1}' | sed '1d'`
PIDSTOKILL=$(echo $PIDSTOKILL $NEWPIDSTOKILL)
fi
done

IFS=$OLDIFS

echo "killing:"
for ps in $PIDSTOKILL;
do
ps -f -p $ps | sed '1d'
kill $(echo $ps)
done
sleep 10
for ps in $PIDSTOKILL;
do
kill -9 $(echo $ps)
done
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Search/destroy old/idle user processes Deviathan Linux - General 2 08-06-2007 02:38 PM
How to kill an idle user via the command line? goemon Linux - General 7 12-01-2005 06:01 PM
How can I kill all processes form a certain user ? Menestrel Linux - Newbie 6 07-05-2005 09:53 AM
kill idle user ust Linux - Software 3 08-31-2004 02:19 PM
who idle user how to kill saavik Linux - Networking 1 03-04-2004 10:12 AM


All times are GMT -5. The time now is 05:38 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration