Thursday 22 May 2008

Monitor long running task

When I run a long running command and can't be bothered to check up on it.

I start it with nohup so I can log out. It returns a which pid it was started with

bash-2.05$ nohup [command] > log.log &
Sending output to nohup.out
[1] 1247

Then I start a monitor script that mails me when the process is finished

bash-2.05$ nohup ./mail_when_finished.sh 1247 &


Script (works on solaris):
--------------------------------
#!/usr/bin/sh
trap '' 1
pid=$1

bail_out () {
mail name@domain.com <Subject: My_Job has finished
Importance: high
X-Priority: 1


My_Job has finished running.
.
!done
exit 0
}

check_it () {

while :
do
ps -p $pid > /dev/null 2>&1
case $? in
0) sleep 120 ;;
*) bail_out; exit 1 ;;
esac
done
}

check_it

---------------------
I took and modified the script from http://www.unix.com/shell-programming-scripting/3205-lightwight-process-monitor.html