シェルでプロセス監視を実現するサンプル
shellコード:
#!/bin/bash
NAME=java
NOP=/bin/true
DATE=$(/bin/date +"%Y-%m-%d %H:%M:%S")
ps -ef | awk -F " " '{print $8,$2}’ | grep ^java >/dev/null 2>&1
case “$?" in
0)
# It is running in this case so we do nothing.
echo “$NAME is RUNNING OK. Relax."
$NOP
;;
1)
echo “$NAME is NOT RUNNING. Starting $NAME and sending notices."
java -jar sms.jar 2>&1 >/dev/null &
NOTICE=/tmp/watchdog.log
echo “$NAME was not running and was started on $DATE" >> $NOTICE
esac
exit 0