Computing.Net > Forums > Linux > Script to kill Linux user processes

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Script to kill Linux user processes

Reply to Message Icon

Name: stimp
Date: August 12, 2008 at 14:25:54 Pacific
OS: Linux
CPU/Ram: 4 GB
Product: IBM
Comment:

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,
Carl

Carl Story



Sponsored Link
Ads by Google

Response Number 1
Name: Shadowlight
Date: August 22, 2008 at 12:08:37 Pacific
Reply:

Something like this?

#!/bin/sh

awk -F: '{print $1,$3}' /etc/passwd | while read user pid
do
(( pid > 500 )) && echo "pkill -u $user"
done

check the start number of your users
remove the echo


0

Response Number 2
Name: Shadowlight
Date: August 22, 2008 at 12:30:54 Pacific
Reply:

Sorry didn't see the idle before


0

Response Number 3
Name: Shadowlight
Date: August 22, 2008 at 12:47:54 Pacific
Reply:

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


0

Response Number 4
Name: stimp
Date: August 22, 2008 at 13:10:02 Pacific
Reply:

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


0

Response Number 5
Name: Shadowlight
Date: August 23, 2008 at 15:56:28 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: Shadowlight
Date: August 23, 2008 at 16:25:55 Pacific
Reply:

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/filename

w -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.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Linux Forum Home


Sponsored links

Ads by Google


Results for: Script to kill Linux user processes

Automatically Killing the Idle Processes www.computing.net/answers/linux/automatically-killing-the-idle-processes/30350.html

How to kill a user on a shell www.computing.net/answers/linux/how-to-kill-a-user-on-a-shell/23728.html

Script to automate adding users www.computing.net/answers/linux/script-to-automate-adding-users/7955.html