时间:2016-02-26 18:03 来源: 我爱IT技术网 作者:佚名
欢迎您访问我爱IT技术网,今天小编为你分享的电脑教程是服务器系列之:【linux下的守护进程】,下面是详细的分享!
linux下的守护进程
#!/bin/sh
#filename test.sh
#绝对定位该文件的位置,不随执行目录而变化
cd $(cd "$(dirname "$0")";pwd)
readonly path=$(pwd)/
file=$1;
RunFile="${path}data/${file}.run"
DieFile="${path}data/${file}.die"
readonly file="${path}${file}.php"
if [ ! -f "$file" ]; then
echo "please select a exists file"
elif [ ! -f "$RunFile" ]; then
#这里进行判断如果RunFile文件不存在,则表示该进程不存在,下面启动进程
echo $$>${RunFile}
while true
do
if [ ! -f $DieFile ]; then
#这里如果DieFile文件不存在,则表示程序继续执行,否则进入else,执行退出操作
/usr/bin/php -f ${file}
touch $RunFile
sleep 1
else
#如果DieFile文件存在清除RunFile和DieFile退出
if rm -rf $RunFile && rm -rf $DieFile ; then
exit
fi
fi
done
else
#这里是在存在RunFile的情况下试图启动该进程
oldpid=`cat $RunFile`
newpid=`ps aux | grep "process.sh $1" | grep -v grep | grep "$oldpid" | awk '{print $2}'`
if [[ $oldpid -eq $newpid ]]; then
#如果RunFile中的进程号和正在运行的目标进程号一致,表明一切安好^_^
echo "the process is runing now"
exit
else
#如果用RunFile中的进程号匹配不到正在运行的目标进程,则表示进程有问题,直接删除RunFile并结束运行的进程
echo "error situation,kill the run process and delete the run file"
ps aux | grep "process.sh $1" | grep -v 'grep' | awk '{print $2}' | grep -v $$ | xargs --no-run-if-empty kill
if [ $? -eq 0 ]; then
rm -f $RunFile
else
echo $?>${path}/data/error
fi
fi
fi
以上就是关于linux下的守护进程的服务器维护教程分享,更多电脑教程请移步到>>电脑教程频道。
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
