Reputation: 41
I need a shell script for killing a long running user process(which exceeds certain amount of time).
These are my observations regarding this:
This is what I've tried:
user = " " " "
for i in user
users="user1 user2 user3"
for u in $users ; do
ps -o etime,pid,comm -u $u | grep "here what i need to grep?" | while read in ; do
ET=`echo $in | cut -f1 -d":"`
[[ $ET -ge 10 ]]&&{ PID=`echo $in | cut -f2 -d" "` ; kill -HUP $PID ; }
done
done
Help me
Upvotes: 1
Views: 3426
Reputation: 113814
This will kill -HUP
any process that (a) belongs to a user on the specified list, (b) is not attached to a terminal, and (c) is more than 10 minutes old:
#!/bin/sh
users=("user1" "user2")
for user in "${users[@]}"
do
ps -o etime,euid,pid,tty,comm -u "$user" | while read etime euid pid tty comm
do
[ "$etime" = ELAPSED ] && continue
[ "$tty" = '?' ] && continue
do_kill=$(echo "$etime" | awk -F'[-:]' 'NF==3{sub(/^/,"0-")} $1>0 || $2>0 ||$3>=10 {print "Kill"}')
[ "$do_kill" ] || continue
kill -HUP "$pid"
done
done
ps
delivers elapsed time in the format days-hh:mm:ss for jobs more than a day old or hh:mm:ss
for jobs less than a day old. awk
is used to interpret this and decide if the job is ten or more minutes old. That is done with the following command:
echo "$etime" | awk -F'[-:]' 'NF==3{sub(/^/,"0-")} $1>0 || $2>0 ||$3>=10 {print "Kill"}'
If the job is 10 or more minutes old, awk
prints Kill
. Otherwise, it prints nothing.
Taking each part of the awk
command in turn:
-F'[-:]'
This tells awk
to break fields on either a dash or colon.
NF==3{sub(/^/,"0-")}
If there are only three fields, then we add the missing day field to the beginning of the line. This assures that the next command sees a uniform format.
$1>0 || $2>0 ||$3>=10 {print "Kill"}
If (a) the number of days, $1
, is greater than zero, or (b) the number of hours, $2
, is greater than zero, or (c) the number of minutes is greater than or equal to 10, then print Kill
. Otherwise, nothing is printed.
The following prompts the user to OK applying kill -HUP
to the process
#!/bin/bash
users=("user1" "user2")
exec 3<&0
for user in "${users[@]}"
do
ps -o etime,euid,pid,tty,comm -u "$user" | while read etime euid pid tty comm
do
[ "$etime" = ELAPSED ] && continue
[ "$tty" = '?' ] && continue
do_kill=$(echo "$etime" | awk -F'[-:]' 'NF==3{sub(/^/,"0-")} $1>0 || $2>0 ||$3>=10 {print "Kill"}')
[ "$do_kill" ] || continue
echo "$user $etime $euid $pid $tty $comm"
read -p "OK to kill this process (y/N)? " -u 3 ok
[[ "${ok,,}" =~ y ]] || continue
echo "Killing $pid"
kill -HUP "$pid"
done
done
Upvotes: 3