Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Im creating a script that i am executing every night at 03.00 that will kill all screens that has been running longer than 3 weeks.

anyone done anything similar that can help? If you got a script or suggestion to a better method please help by posting :)

I was thinking maybe somthing like this.

First do a dump to textfile

ps -U username -ef | grep SCREEN >> dump.txt

then do a loop running through all lines of dump.txt with a regex and putting pid of the prosseses with STIME > 3weeksago in a array.

then do a kill loop on the array result.


edit: added script i ended up with

this is the script i ended up with, im killing all screens that has been opend longer than 30days buy user 1002.

#!/bin/bash

clear
echo "Starting Screen cleanup script this will stop any screens older then 30 days";
echo "Starting in 5 seconds, press ctrl-c to cancel";

    c=1
    while [ $c -le 5 ]
    do
    echo "start $c "
    sleep 1
    ((c++))
    done

    first()
    {
    echo $1
    }

    second()
    {
    echo $2
    }

    third()
    {
    echo $3
    }

    COUNT=0

    ps -e -o "pid etime comm uid" | egrep '1002'  | egrep 'screen' | while read PID ETIME COMM
    do
    case "$ETIME" in
    *:* )

            DAYS=0
            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 "2592000"
            then
                    echo $PID $COMM 
                    echo "DIE-DIE-DIE--------------------->killing pid------>"$PID 
                    ((COUNT++))
                    kill -15 $PID
                    echo $COUNT
            fi
            ;;
    * )
            ;;
    esac
    done
share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

For shell you can build off of this:

ps -A -o etime,pid,cmd | while read etime pid rest; do 
    if grep '^/usr/bin/screen' <(echo $rest) >/dev/null; then 
       echo $etime $pid 
    fi
done

Just parse out etime for days greater than x and kill the pid. See man ps for what etime (Elapsed time is).

This assumes Linux.

share|improve this answer
add comment

Maybe this SO article can help you, it seems similar. You could change the regex to match your 3weeks.

share|improve this answer
add comment

killall can kill processes by name and user.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.