1. 程式人生 > >shell後臺daemon指令碼

shell後臺daemon指令碼

有些指令碼,可能防止重複啟動,處於後臺daemon狀態,用鎖檔案來監控保持daemon狀態

#!/bin/bash
#指定鎖檔案=========
lockfile=/test/lock.txt
#檢測鎖檔案,避免重複啟動========
function check()
{
#==============kill -0不傳送訊號,判斷程序是否存在,返回1/0
if [ -e $lockfile ] && kill -0 cat ${lockfile};
then
echo “process is already running!!”
exit
fi
trap “rm -f ${lockfile};exit” INT TERM EXIT
}
#======監控lockfile

=
monitor_lockfile()
{
if [ ! -e $lockfile ]
then
echo “process has stoped,because lockfile has been delete”
exit
fi
}
#=======main方法
function work()
{
check
echo $$ > ${lockfile}
while true
do
monitor_lockfile
echo “working !! ===>”
sleep 2
done
}

work