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
For shell you can build off of this:
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.
Maybe this SO article can help you, it seems similar. You could change the regex to match your 3weeks.
killall
can kill processes by name and user.