Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto Killing Idle Processes 2

Status
Not open for further replies.

burden010

Technical User
Feb 8, 2005
21
0
0
GB
Hi, appologies if this has already been posted - I did seach but couldn't find an exact match for my criteria.

Also, I think it's best to point out that I'm a complete newbie to Unix so you may have to explain any suggestions a little :)

Anyways, I'm running a Unixware 7.1.1 box which runs only 1 user accessed database. The problem is that some users sit idle for hours while others can't log in due to licencing restrictions. Currently I'm using who -u to find the process ID and killing them off, this is very time consuming and I need to find a way to auto kill idle processes after say, 30 minutes.

But, I also need to maintain a list of users whose processes are NOT killed off. Most users are casual, read only users but you can imagine the problems I would have if I start killing off users who are editing the database.

Any help would be greatly appreciated. Thanks.

Paulo
 
As PHV likes to say...
Here's a starting point:
Code:
who -u | grep -v -f filecontainingexemptusers | while read junk junk junk junk junk time pid junk
do if [[ $time = '.' ]] 
then 
  continue
fi
mins=$( echo $time | cut -d: -f2 )
if [[ $mins > 30 ]]
then 
  kill $pid
fi
done
Cron that every 10 minutes.
 
I did this for a user wanting to kill idle processes at a certain time and put the script in cron to run X hours.

Code:
#!/bin/ksh

Ctime=$(date +%T)

__chk_time() {
CtimeH=$(echo $Ctime | awk -F: '{ print $1 }')
CtimeM=$(echo $Ctime | awk -F: '{ print $2 }')
CtimeS=$(echo $Ctime | awk -F: '{ print $3 }')
PtimeH=$(echo $Ptime | awk -F: '{ print $1 }')
PtimeM=$(echo $Ptime | awk -F: '{ print $2 }')
PtimeS=$(echo $Ptime | awk -F: '{ print $3 }')

((CtimeMinutes=CtimeH * 60))
((PtimeMinutes=PtimeH * 60))

((CtimeRunning=CtimeMinutes + CtimeM))
((PtimeRunning=PtimeMinutes + PtimeM))

if [[ $CtimeRunning -gt $PtimeRunning ]]
then
        ((TimeDiffH=(CtimeRunning - PtimeRunning) / 60))
        ((TimeDiffM=(CtimeRunning - PtimeRunning) % 60))
        TimeDiff=$TimeDiffH.$TimeDiffM
        return
else
        break
fi
}

__chk_proc() {
for Proc in $(ps -fu YOUR_USER | grep YOUR_PROCESS | awk '{ print $2 }')
do
        Ptime=$(ps -ef | grep $Proc | grep YOUR_PROCESS | awk '{ print $5 }')
        __chk_time
        if (( TimeDiff >= 1.00 ))
        then
                kill -6 $Proc
        fi
done
}

__chk_proc
 
If your using ksh then you could look at the TMOUT environment variable. Then add to /etc/profile, for example,
Code:
grep $LOGNAME /etc/exemptusers > /dev/null 2>&1 || export TMOUT=1800
You might want to make it read only.

Columb Healy
 
So if I go with Eric's suggestion, all I need to do is replace "filecontainingexemptusers" with something like "/etc/default/textfile"?

Does the file need to have any format? Or just a list of user Ids?

Also... um, how do I cron something? I assume cron's a task scheduler of some sort? Is it a case of putting the text in a file and pointing cron to that file?

Sorry for being rubbish :(
 
We all started somewhere, so don't put yourself down so. Your interpretation is correct - put your exempt userids in /etc/default/exemptusers or wherever you like as long as you reference it within the script. Copy Eric's script into a text file (these are usually called shell scripts in this context), and to test, I suggest replacing the kill $pid with something like echo $pid >> userstobekilled.txt ie another text file listing all the users who would have been killed had the script been operationa. Once you're happy with that, chage back to the kill $pid construct.

Point cron at the script you've made (don't forget to make it executable: chmod 755 <yourscript>), with an entry like:

30 * * * * <path to yourscript>

to run the script every hour of every day on the half-hour.

Hope this helps.
 
That's great, thanks guys. I'm running the script with output to a killed.txt file and seems to run fine when I manually type "#ksh scriptname" and I can see which processes would be killed but when trying to cron it I think I'm entering the wrong syntax as I get an error.

Here's what I'm doing:

#cron 30 * * * * /etc/default/scriptname
#cron aborted: Cannot set lock on file.

Obviously cron is being accessed but have I somehow locked the file elsewhere? Or am I on the wrong trail?

Thanks.
 
Gettin there if it's running interactively. I think you misunderstand cron. Cron is a scheduler, not a script. You need to do the following to add your script to cron:

crontab -l > newcron

to create a file containing current cron commands

edit (with vi) the newcron file and add

 
Sorry - continuation from ....and add:



30 * * * * /etc/default/scriptname

at the bottom. Save this and then issue the command:

crontab newcron to activate it. Hopefully this will work for you.
 
OK, thanks. Done that suppose I'll wait and see what happens in a half hour.

Cheers.
 
Sorry Gersh,

I must have misled you unwitingly. Here is the sample file again:
200095,BIB,STEVEN,5/1/2003,10/1/1993,5/1/2003
200095,BIB,STEVEN,5/1/2004,10/1/2003,5/1/2002
200095,BIB,STEVEN,5/1/2003,5/1/2002,5/1/2003
200095,BIB,STEVEN,5/1/2004,10/1/1993,5/1/2005
200099,BEN,TAMMY,5/29/2000,5/29/2000,5/04/2002
200099,BEN,TAMMY,5/23/2001,1/02/2001,1/1/2001
200099,BEN,TAMMY,5/29/2000,5/29/2000,5/04/2002
200099,BEN,TAMMY,1/1/2001,5/23/2001,1/02/2001

Note that all is seperated by ","
also I am trying to extract the records only where that 6th element does not match either 4th or 5th elements for a particular 1st element. So the comparisson chages when the first element changes.

Thanks
 
Sorry, have been pretty busy. It seems to be working though when I changed "echo" to "kill" it seemed to stop writing to killed.txt, I'm not sure.

Also, in crontab, I have inserted 30 * * * * ksh /etc/default/scriptname

on the basis that at the prompt I have to issue the command ksh to execute the scriptname file. Is this right or should I remove the ksh command?
 
It's fine. You can also include it as the first line in the script. To write everything to killed.txt put > 2>&1 killed.txt at the end of your command. This will write all output (including errors) to killed.txt. HTH.
 
> 2>&1 killed.txt

Is this at the end of the cron line or within the scriptfile?

I am assuming it's cron at the moment as you used the word command instead of script... :) I may be wrong.
 
Yup, your line in cron should be:

30 * * * * /etc/default/scriptname > 2>&1 killed.txt

BTW, to ensure the script runs using ksh from within the script (as alluded to above), you can add #!<path to ksh> as the first line.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top