#! /bin/bash
- ################################################
NAME_="timelykill"
PURPOSE_="kill process pid after n seconds|minutes|hours|days"
SYNOPSIS_="$NAME_ [-hl] <pid> <n>s|m|h|d"
REQUIRES_="standard GNU commands"
VERSION_="1.0"
DATE_="2004-06-20; last update: 2004-07-21"
AUTHOR_="Dawid Michalczyk <
dm@eonworks.com>"
URL_="www.comp.eonworks.com"
CATEGORY_="sys"
PLATFORM_="Linux"
SHELL_="bash"
DISTRIBUTE_="yes"
- ###############################################
- This program is distributed under the terms of the GNU General Public License
usage () {
echo >&2 "$NAME_ $VERSION_ - $PURPOSE_
Usage: $SYNOPSIS_
Requires: $REQUIRES_
Options:
<pid> <n>s|m|h|d, pid and n seconds|minutes|hours|days to wait before killing the process
-h, usage and options (this help)
-l, see this script"
exit 2
}
- local func
check_pid() {
kill -0 $1 &>/dev/null # using bash kill built in
[ $? = 0 ] && return 0 || return 1
}
- enabling extended globbing
shopt -s extglob
- option handling
case $1 in
-h) usage ;;
-l) more $0; exit 2 ;;
+([0-9])) # arg1 can only be an integer
[ $2 ] || { echo missing second argument, type $NAME_ -h for help; exit 2; }
check_pid $1 ; [ $? != 0 ] && { echo pid $1 does not exits; exit 2; }
sleep ${2} # before the kill
while kill -0 $1 &>/dev/null ;do
kill -15 $1; check_pid; [ $? == 1 ] && exit 0
kill -3 $1; check_pid; [ $? == 1 ] && exit 0
kill -9 $1; check_pid; [ $? == 1 ] && exit 0
echo process can not be killed, possibly due to lack of ownership rights
exit 1
done ;;
*) echo invalid or missing argument, type $NAME_ -h for help ; exit 2 ;;
esac