Script: Kill certain processes over 24 hours old

Soldato
Joined
1 Oct 2006
Posts
14,520
Scenario: HPUX server that runs client sessions is reaching capacity because users don't shut down their sessions properly.

Solution: Implement a cron script to run every 5 minutes to check the ps list for the process name, and start time and kill if running more than 24 hours.

Problem: Script doesn't work :D I've pilfered the below from a Google thinking it'd just be a case of slipping the process name in and manipulating the output, but no matter what I set the time period to in the section:

Code:
                DAYS=1
                HOURS=0
                MINUTES=0
                SECONDS=0
it just returns all the processes running named "myprocessnamegoeshere ".

I'm wondering if it's something to do with having to alias the ps command at the start of the script to fool it into thinking it's an XPG4 version of ps... OR the time formatting is wrong and it's taking the *.*.* literally as *.*.* and outputting all processes irrespective of the time value set.

Here's what I've got so far:

Code:
#!/bin/sh
alias ps="UNIX95= /usr/bin/ps"

first()
{
        echo $1
}

second()
{
        echo $2
}

third()
{
        echo $3
}

ps -ef -o "pid etime comm" | while read PID ETIME COMM
do
        case "$ETIME" in
        *:* )
                DAYS=1
                HOURS=0
                MINUTES=0
                SECONDS=0

                case "$ETIME" in
                *-* )
                        X=`echo $ETIME | sed y/-/\ /`
                        DAYS=`first $X`
                        ETIME=`second $X`
                        ;;
                * )
                        ;;
                esac

                X=`echo $ETIME | sed y/:/\ /`

                case "$ETIME" in
                *:*:* )
                        HOURS=`first $X`
                        MINUTES=`second $X`
                        SECONDS=`third $X`
                        ;;
                *:* )
                        MINUTES=`first $X`
                        SECONDS=`second $X`
                        ;;
                *)
                        ;;
                esac

                HOURS=`echo $HOURS + \( $DAYS \* 24 \) | bc`
                MINUTES=`echo $MINUTES + \( 60 \* $HOURS \) | bc`
                SECONDS=`echo $SECONDS + \( 60 \* $MINUTES \) | bc`

                if test "$SECONDS" -gt "3600"
                then
                        echo $PID $COMM | grep myprocessnamegoeshere 
                fi
                ;;
        * )
                ;;
        esac
done

Any help would be much appreciated chaps!

Cheers :)
 
I don't know much about HPUX or csh but maybe you could write your own script?
The finger command is useful for idle login session times - you could then link those sessions to the appropriate processes and issue a kill on each one. Just a thought...
 
Back
Top Bottom