I use the following script to kill process by timeout:
# $1 - name of program and its command line
#launch program and remember PID
eval "$1" &
PID=$!
echo "Program '"$1"' started, PID="$PID
i=1
while [ $i -le 300 ]
do
ps -p $PID >> /dev/null
if [ $? -ne 0 ]
then
wait $PID
exit $? #success, return rc of program
fi
i=$(($i+1))
echo "waiting 1 second..."
sleep 1
done
#program does not want to exit itself, kill it
echo "killing program..."
kill $PID
exit 1 #failed
So far, it have worked excellent, but today, i've noticed a bunch of 'hanging' processes in htop, so i've checked out and it turns out, that $PID
in this case is not ID of the program process but of the script itself, and all the times i checked, ID of the program is $PID+1
. Now, the question is, am i correct to assume, that it will always be $PID+1
and i won't kill something important by replacing kill $PID
with something like kill $PID $($PID+1)
EDIT: $1
may have several urguments, like ./bzip2 -ds sample3.bz2 -k