知识屋:更实用的电脑技术知识网站
所在位置:首页 > 操作系统 > linux

守护进程的shell--tipserver

发布时间:2014-09-05 14:46:46作者:知识屋

守护进程的shell--tipserver
 
1:可以直接关掉终端,在后台运行
2:可以查询服务的状态
 
3:可以定时启动需要运行的程序(范例为17点启动)
 
4:可以在运行时避免重复启动
tipserver
 
#!/bin/sh
 
run_main_programer()
{
while true
do
if [ "$date_hour" = '17' ]
then
as=`date +'%Y%m%d_%H%M%S'`
sh ./control_sh.sh >log/control_sh_$as.log
sleep 3600
fi
sleep 300
date_hour=`date +'%H'`
done
}
 
 
myself=`echo $0 | sed 's/^/.////g'`
 
 
if [ $# -eq 0 ]
then
  echo "Unknown command option"
  echo "Usage::"
  echo "       $myself start | stop | status"
  exit
fi
 
 
case "x$1"
in
xstart)
#当前后台运行的线程列表
thread=`ps -ef | grep -v grep | grep -w "$myself" | grep "start" | awk '{ print $2 }'`
#只有本线程运行
if [ "$thread" = "$$" ]
then
nohup sh $myself start >/dev/null &
sleep 3
echo "The $myself have been started."
else
#取得父线程号
father_thread=`ps -ef | grep -v grep | grep "$myself" | grep "start" | grep "$$" | awk '{ print $3 }'`
#当前线程的父线程存在
if [ `ps -ef | grep -v grep | grep "$myself" | grep "start" | grep "$father_thread" | wc -l` -eq 2 ]
then
run_main_programer
fi
echo "The $myself is running."
fi
;;
xstop)
thread=`ps -ef | grep -v grep | grep "$myself" | grep "start" | awk '{ print $2 }'`
if [ -n "$thread" ]
then
kill -9 ${thread} 2>/dev/null
fi
echo "The $myself have been stoped."
;;
xstatus)
thread=`ps -ef | grep -v grep | grep "$myself" | grep "start" | awk '{ print $2 }'`
if [ -n "$thread" ]
then
echo "The $myself is running."
else
echo "The $myself is not running."
fi
;;
*)
echo '  Error parameter.'
esac
 
(免责声明:文章内容如涉及作品内容、版权和其它问题,请及时与我们联系,我们将在第一时间删除内容,文章内容仅供参考)
收藏
  • 人气文章
  • 最新文章
  • 下载排行榜
  • 热门排行榜