Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I need a script that will automatically kill any user process (other than root or su processes)after being idle for 1 hour or more. I know that I can place this script in the cron to have it fired off every 15, 30 or whatever minutes. However, I'm having trouble with a script.
Any help would be greatly appreciated.
Thanks,
CarlCarl Story

Something like this?
#!/bin/sh
awk -F: '{print $1,$3}' /etc/passwd | while read user pid
do
(( pid > 500 )) && echo "pkill -u $user"
donecheck the start number of your users
remove the echo

try again..
#!/bin/sh
# max hours to be idle
(( HOURS = 8 ))w -h | awk '!/root/{print $1,$5}' | while read user idle
do
[[ -n "$( echo $idle | grep -E 'm|s' )" ]] && continue# make hours of idle
idle=${idle%:*}
uid=$( id -u $user )(( uid > 500 )) && (( idle >= HOURS )) && echo "pkill -u $user"
done

So, this will kill all processes idled for 1 hour or more unless it's root or an su process? I just need to change HOURS to equal 1...is that correct?
Thanks for your help. This will help us a great deal because, I'm not good at this script thing. I can write the most simple scripts. However, when you start talking "awk" and all of that stuff, I have to find a book. But you're right they do need to be idle for about an hour. The problem that we're having is: we have more users than we do licenses for the app that we use. And one of the problems that we run into is that a user may not be able to log into the app but there are other users who are idle for at least an hour. We want to be able to log those users out so that the people who needs to get in will be able to.
Carl Story

This is almost correct.
you have it correct that you have to set the variable HOURS to 1 if you want to kill users after 1 hour.
there are also system users which you do not wish to kill.
On debian systems those are the users below 1000 on others it can be below 500 that is why i put (( uid > 500 )) there
you can better change it to (( uid >= 500 ))
or define a variable UID=500 (or higher)
and make it (( uid >= UID ))So then it will kill processes of users that have an uid above or equal to 500 (Linux users generaly start at 500 but you can check this in /etc/passwd or perhaps in /etc/default/useradd

Ok I could't help myself and played some more..
#!/bin/sh
# max hours to be idle
(( HOURS = 1 ))# start number of user uid's
(( UID = 500 ))# file with users to exclude from killing (one user per line)
excludefile=/path/to/filenamew -h | awk '!/root/{print $1,$5}' | while read user idle
do
# check if user is in excludefile
[[ -n $( grep $user $excludefile ) ]] && continue
# check for idle time
[[ -n "$( echo $idle | grep -E 'm|s' )" ]] && continue# make hours of idle (throw everythig away after and incuding ':'
# e.g. 2:35 will become 2
idle=${idle%:*}
# get uid from user
uid=$( id -u $user )(( uid >= UID )) && (( idle >= HOURS )) && echo "pkill -u $user"
done
create a file in which you define users you do not wish to be killed.the su is a bit trickier because i don't know what te uid is for them and how many child processes the su spawns.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |