#function to lookup and return pid's based on program name
#usage: lookupId wget ; var=`lookupId wget`
function lookupId() {
prog=$1
all=`ps -aux | grep $prog | awk ' { if ($0 !~ /grep/) {print $2}}'`
if [ ! -z "$all" ] ; then
echo $all
fi
return
}
#function to monitor and kill a running process.
#usage: sleepCheck 3600 60 wget
function sleepCheck() {
tbin=$PWD/tt
max=$1
interval=$2
prog=$3
start=`exec $tbin`
now=`exec $tbin`
pidlist=`lookupId $prog`
while :
do
etime=`expr $now - $start`
if [ $etime -gt $max ] ; then
for all in $(echo $pidlist)
do
kill -kill $all || echo "Unable to kill $all"
done
exit 0 > /dev/null
fi
sleep $interval
pidlist=`lookupId $prog`
if [ -z "$pidlist" ] ; then
echo "No processes for $prog found"
exit 0 > /dev/null
fi
now=`exec $tbin`
done
}
#snippet to generate a program for epoch time
#usage: genStamp
function genStamp() {
binp=$PWD/tt
rfile=$PWD/raw.c
if [ -e $binp ] ; then
return
fi
cat << EOF > $rfile
#include <stdio.h>
#include <time.h>
int main(void) {
printf("%d\n",time(NULL));
return 0;
}
EOF
gcc $rfile -o $binp || echo "Could not create $binp"
rm $rfile
}