#! /bin/sh
#
# See contrib/init.resin for /etc/rc.d/init.d startup script
#
# resin.sh can be called like apachectl
#
# resin.sh -- execs resin in the foreground
# resin.sh start -- starts resin in the background
# resin.sh stop -- stops resin
# resin.sh restart -- restarts resin
#
# resin.sh will return a status code if the wrapper detects an error, but
# some errors, like bind exceptions or Java errors, are not detected.
#
# To install, you'll need to configure JAVA_HOME and RESIN_HOME and
# copy contrib/init.resin to /etc/rc.d/init.d/resin. Then
# use "unix# /sbin/chkconfig resin on"
PCK_TAG=pcheck
export=PCK_TAG
#JAVA_HOME
JAVA_HOME=/usr/local/jdk1.6.0_29
export JAVA_HOME
#本脚本的绝对路径
EXEC_START="/usr/local/sns/snsconnector.sh"
#日志输入路径
WATCH_LOG="/usr/local/sns/watchdog.log"
export WATCH_LOG
#执行脚本
RESIN="bin/httpd.sh"
#执行多个resin的多路径
EXEC="/usr/local/sns/snsapi/resin3 /usr/local/sns/snsapi2/resin3"
array=($EXEC)
length=${#array[@]}
echo $length
for ((i=0; i<$length; i++))
do
echo ${array[$i]}
done
#####################################################
# Find a PID for the pid file
#####################################################
for ((i=0; i<$length; i++))
do
RESIN_HOME=${array[$i]}
SNS_PIDS="$SNS_PIDS $RESIN_HOME/snsconnector.pid "
done
pids=($SNS_PIDS)
size=${#pids[@]}
echo $size
for ((i=0; i<$size; i++))
do
echo ......${pids[$i]}
done
#
SNS_TAG=snstag
if test -n "${JAVA_HOME}"; then
if test -z "${JAVA_EXE}"; then
JAVA_EXE=$JAVA_HOME/bin/java
fi
fi
#
# trace script and simlinks to find the wrapper
#
if test -z "${RESIN_HOME}"; then
script=`/bin/ls -l $0 | awk '{ print $NF; }'`
while test -h "$script"
do
script=`/bin/ls -l $script | awk '{ print $NF; }'`
done
bin=`dirname $script`
RESIN_HOME="$bin/.."
fi
running()
{
[ -f $1 ] || return 1
PID=$(cat $1)
echo ==========$PID==========
ps -p $PID >/dev/null 2>/dev/null || return 1
return 0
}
zombiling()
{
PID=$(cat $1)
echo "zombiling $PID" >>$WATCH_LOG
IMZOMBIE=`ps -el |grep $PID|awk '{print $2}'` >/dev/null 2>/dev/null
imarray=($IMZOMBIE)
echo "SNSConnector process current run status is: ${imarray[0]}" >>$WATCH_LOG
echo "SNSConnector process current run status is: $IMZOMBIE"
echo "`date '+%Y-%m-%d %T'` [INFO] SNSConnector process current run status is: $IMZOMBIE" >>$WATCH_LOG
if [ ! -z `echo ${imarray[0]}|grep '^[TZ]'` ]
then
echo "SNSConnector process is zombied. now restart the service."
echo "`date '+%Y-%m-%d %T'` [WARN] SNSConnector process is zombied. now restart the service." >>$WATCH_LOG
return 0
fi
return 1
}
usage()
{
echo "Usage: $0 {start|stop|status} [ CONFIGS ... ] "
exit 1
}
[ $# -gt 0 ] || usage
ACTION=$1
MAINTE_CHECK=$2
ZOMBIE_CHECK=$3
#####################################################
# Get current script belongs to User and Group.
#####################################################
[ -z ${SCRIPT_NAME} ] && SCRIPT_NAME=httpd.sh
if [ -f ${RESIN_HOME}/bin/${SCRIPT_NAME} ]
then
CURRENT_GROUP="$(echo `ls -l ${RESIN_HOME}/bin/${SCRIPT_NAME}` | awk '{print $4}')";
CURRENT_USER="$(echo `ls -l ${RESIN_HOME}/bin/${SCRIPT_NAME}` | awk '{print $3}')";
else
echo "`date '+%Y-%m-%d %T'` ERROR: Current start service script is:${RESIN_HOME}/bin/${SCRIPT_NAME}" >>$WATCH_LOG
echo "`date '+%Y-%m-%d %T'` ERROR: The SNS start file is not exist."
fi
setCrontab()
{
echo "setCrontab...";
crontab_tmp=$(mktemp -p "${RESIN_HOME}/bin/")
if [ x$CURRENT_USER != x$(whoami) ]
then
echo "* * * * * su - $CURRENT_USER -c "${EXEC_START} start $SNS_TAG" > /dev/null 2>&1" >> "${crontab_tmp}"
echo "* * * * * sleep 10; su - $CURRENT_USER -c "${EXEC_START} start $SNS_TAG" > /dev/null 2>&1" >> "${crontab_tmp}"
echo "* * * * * sleep 20; su - $CURRENT_USER -c "${EXEC_START} start $SNS_TAG" > /dev/null 2>&1" >> "${crontab_tmp}"
echo "* * * * * sleep 30; su - $CURRENT_USER -c "${EXEC_START} start $SNS_TAG" > /dev/null 2>&1" >> "${crontab_tmp}"
echo "* * * * * sleep 40; su - $CURRENT_USER -c "${EXEC_START} start $SNS_TAG" > /dev/null 2>&1" >> "${crontab_tmp}"
echo "* * * * * sleep 50; su - $CURRENT_USER -c "${EXEC_START} start $SNS_TAG $PCK_TAG" > /dev/null 2>&1" >> "${crontab_tmp}"
elif [ x$CURRENT_USER == x$(whoami) ]
then
echo "* * * * * "${EXEC_START} start $SNS_TAG" > /dev/null 2>&1" >> "${crontab_tmp}"
echo "* * * * * sleep 10; "${EXEC_START} start $SNS_TAG" > /dev/null 2>&1" >> "${crontab_tmp}"
echo "* * * * * sleep 20; "${EXEC_START} start $SNS_TAG" > /dev/null 2>&1" >> "${crontab_tmp}"
echo "* * * * * sleep 30; "${EXEC_START} start $SNS_TAG" > /dev/null 2>&1" >> "${crontab_tmp}"
echo "* * * * * sleep 40; "${EXEC_START} start $SNS_TAG" > /dev/null 2>&1" >> "${crontab_tmp}"
echo "* * * * * sleep 50; "${EXEC_START} start $SNS_TAG $PCK_TAG" > /dev/null 2>&1" >> "${crontab_tmp}"
fi
tail "${crontab_tmp}"
crontab "${crontab_tmp}"
rm -f ${crontab_tmp}
return 0
}
case "$ACTION" in
start)
echo "`date '+%Y-%m-%d %T'` [INFO] snsconnector service start begin.$MAINTE_CHECK $ZOMBIE_CHECK " >>$WATCH_LOG
for ((i=0; i<$length; i++))
do
RESIN_HOME=${array[$i]}
RUN_CMD=$RESIN_HOME/$RESIN
SNS_PID=${pids[$i]}
echo -n "$RESIN_HOME Starting snsconnector: "
if [ x$MAINTE_CHECK == x$SNS_TAG ] && [ -f "$RESIN_HOME/$SNS_TAG" ]
then
echo "Maintenance period,can't start sns service."
exit 1
fi
echo "`date '+%Y-%m-%d %T'` [INFO] snsconnector service start begin.[x$ZOMBIE_CHECK] [x$PCK_TAG] " >>$WATCH_LOG
if [ x$ZOMBIE_CHECK == x$PCK_TAG ] && zombiling $SNS_PID
then
PID=`cat $SNS_PID 2>/dev/null`
echo "`date '+%Y-%m-%d %T'` snsconnector(pid $PID $SNS_PID) is zombie." >>$WATCH_LOG
kill -9 $PID
echo "`date '+%Y-%m-%d %T'` snsconnector(pid $SNS_PID) is stopped." >>$WATCH_LOG
continue
else
if [ x$ZOMBIE_CHECK == x$PCK_TAG ]
then
echo "snsconnector(pid `cat $SNS_PID`) is not zombie." >>$WATCH_LOG
fi
fi
echo "`date '+%Y-%m-%d %T'` [INFO] >>>>>>>>>>[x$ZOMBIE_CHECK] [x$PCK_TAG] " >>$WATCH_LOG
if [ -f $SNS_PID ]
then
if running $SNS_PID
then
echo "Already Running."
echo "`date '+%Y-%m-%d %T'` snsconnector running pid is:"`cat $RESIN_HOME/snsconnector.pid` >>$WATCH_LOG
#exit 1
continue
else
# dead pid file - remove
rm -f $SNS_PID
fi
fi
if [ x$CURRENT_USER != x ] && [ x$CURRENT_USER != x$(whoami) ]
then
touch $SNS_PID
chown $CURRENT_USER:$CURRENT_GROUP $SNS_PID
su - $CURRENT_USER -c "
$RUN_CMD > /dev/null 2>&1 &
PID=\$!
disown \$PID
echo \$PID > $SNS_PID"
echo pid=$PID
echo sns_pid=$SNS_PID
elif [ x$CURRENT_USER == x$(whoami) ]
then
echo ===$RUN_CMD===
touch $SNS_PID
eval "
$RUN_CMD > /dev/null 2>&1 &
PID=\$!
disown \$PID
echo \$PID > $SNS_PID"
echo pid=$PID
echo sns_pid=$SNS_PID
fi
sleep 5
if running $SNS_PID
then
rm -f $RESIN_HOME/$SNS_TAG
echo "OK"
echo "pid is `cat $SNS_PID 2>/dev/null`"
echo "STARTED AT `date`"
echo "`date '+%Y-%m-%d %T'` [INFO] snsconnector service start OK." >>$WATCH_LOG
else
echo "FAILED."
echo "`date '+%Y-%m-%d %T'` [ERROR] snsonnector service start FAILED." >>$WATCH_LOG
fi
done
setCrontab;
exit 1
;;
stop)
echo "`date '+%Y-%m-%d %T'` [INFO] snsconnector service stop begin." >>$WATCH_LOG
for ((i=0; i<$length; i++))
do
RESIN_HOME=${array[$i]}
SNS_PID=${pids[$i]}
echo -n "$RESIN_HOME Stopping snsconnector... "
if running $SNS_PID
then
PID=`cat $RESIN_HOME/$SNS_PID 2>/dev/null`
TIMEOUT=60
while running $SNS_PID && [ $TIMEOUT -gt 0 ]
do
kill $PID 2>/dev/null
echo -n "."
sleep 1
let TIMEOUT=$TIMEOUT-1
done
[ $TIMEOUT -gt 0 ] || kill -9 $PID 2>/dev/null
rm -f $SNS_PID
echo OK
echo "`date '+%Y-%m-%d %T'` [INFO] $RESIN_HOME snsconnector service stop OK." >>$WATCH_LOG
else
echo "$SNS_PID snsconnector service already stopped,stop failed."
echo "`date '+%Y-%m-%d %T'` [ERROR] $RESIN_HOME snsconnector service already stopped,stop FAILED." >>$WATCH_LOG
fi
touch $RESIN_HOME/$SNS_TAG
done
exit 1
;;
status)
echo "`date '+%Y-%m-%d %T'` [INFO] snsconnector service status." >>$WATCH_LOG
for ((i=0; i<$length; i++))
do
RESIN_HOME=${array[$i]}
SNS_PID=${pids[$i]}
if running $SNS_PID
then
PID=`cat $SNS_PID 2>/dev/null`
echo "snsconnector(pid $PID) is running."
echo "`date '+%Y-%m-%d %T'` [INFO] $RESIN_HOME snsconnector(pid $PID) is running." >>$WATCH_LOG
else
rm -f $SNS_PID
echo "snsconnector has stoped."
echo "`date '+%Y-%m-%d %T'` [INFO] $RESIN_HOME snsconnector(pid $PID) has stoped." >>$WATCH_LOG
fi
done
exit 1
;;
*)
usage
;;
esac
exit 0
分享到:
相关推荐
Linux 操作系统中,僵死进程(Zombie Process)是一种特殊的进程状态,它是指一个进程已经终止,但是其父进程没有通过wait或waitpid函数来回收它的状态信息,而使得进程所占用的资源无法释放,导致系统资源的浪费。...
"LINUX查看和处理僵死进程" 在LINUX系统中,僵死进程(Zombie Process)是一个非常重要的概念。僵死进程是指已经死亡的进程,但是没有释放系统资源,包括内存和一些系统表等。如果这样的进程很多,会引发系统问题。...
### Oracle杀僵死进程 在Oracle数据库管理过程中,经常会遇到僵死进程的问题,这些问题可能会导致数据库性能下降甚至无法正常运行。本文将详细介绍如何通过SQL查询定位僵死进程,并提供具体的解决方案,帮助解决如...
linux 下的 bash shell脚本,用于杀死僵死的进程,并重新启动.
linux 查找僵死进程,重启服务,有利于知道哪些服务已经死掉进程还在
JobObject来管理子进程,确保主进程关闭时子进程自动结束,避免异常退出时留下僵尸进程
对于企业环境,自动化监控和管理系统,如Nagios、Zabbix和Prometheus等,能够实时监控全网的僵死和低效进程,提供报警服务,并生成详细的性能报告,有助于系统管理员更有效地管理和优化系统。 总结来说,识别并处理...
心跳检测是一种常见的IPC机制,用于确认进程是否正常运行,即判断进程是否"live"或者已经僵死(僵尸状态)。这种机制常用于分布式系统、服务监控以及高可用性解决方案中。 心跳检测的基本思想是定期发送一个信号或...
核心服务器上跑了一堆的脚本、程序,难免有时候会出现僵尸进程,死不死活不活的在那里占用资源,最初只是写了个根据关键字查杀进程的linux shell脚本,后来发现很多时候进程死在那里的时候其实是内部调用子进程的...
Linux 允许进程查询内核以获得其父进程的 PID,或者其任何子...这就是引入僵死状态的原因:尽管从技术上来说进程已死,但必须保存它的描述符,直到父进程得到通知。 如果一个进程已经终止,但是它的父进程尚未调用 wai
异常情况下,进程可能会进入僵死状态(zombie),这意味着进程已经结束,但其父进程尚未调用`wait(2)`或`waitpid(2)`来回收其资源。长时间存在的僵死进程会占用系统资源,因此应当避免这种情况。系统管理员可以通过...
- **僵死进程**:子进程退出后,其进程描述符不会立即释放,除非父进程调用`wait()`或`waitpid()`。若父进程未收集子进程信息,子进程就成为僵死进程,状态显示为`Z`。大量僵死进程可能导致系统无法创建新进程。...
调用`abort()`函数,导致进程异常终止,通常是因为程序检测到严重的错误。 - b. 进程接收到某些信号,比如`SIGKILL`或`SIGTERM`,这些信号会导致进程立即停止。 在退出过程中,无论是哪种方式,系统都会执行相同...
### Shell脚本—判断循环:深入理解与应用...通过以上知识点的学习,我们不仅掌握了如何利用Shell脚本来监控和管理进程,还能高效地处理和操作数据文件,这些技能在日常的系统管理和自动化脚本编写中都将发挥重要作用。
- **僵死态(Zombie)**:进程已结束,但父进程未回收其资源。 **Unix/Linux进程调度** Unix/Linux系统的进程调度主要关注如何公平有效地分配CPU时间。调度策略包括: - **先来先服务(FCFS)**:按照进程到达的...
Linux 操作系统中,进程的运行状态是指进程当前的执行状态,包括运行状态、可中断睡眠状态、不可中断睡眠状态、暂停状态和僵死状态等。这些状态之间的转换是通过内核中的调度程序实现的。 运行状态(TASK_RUNNING)...
- **僵死**:进程已结束,但其父进程未对其进行清理。 - **调度**:进程正等待被调度器选择执行。 - **返回**:进程完成执行并退出。 - **调度中断**:进程被中断,等待重新调度。 - **终止**:进程结束并被父进程...
### Linux中僵尸进程和孤儿进程详解 #### 一、引言 在Linux系统中,进程是操作系统进行资源分配和调度的基本单位。为了更好地理解和管理进程,我们需要了解一些特殊类型的进程,比如孤儿进程和僵尸进程。这两种进程...
僵死态是进程的运行已经结束,但它的任务结构体仍在系统中。 Linux 进程处理机上运行时,处理机提供了两种不同的执行状态:核心态和用户态。核心态具有较高的特权,能执行所有的机器指令,包括由操作系统执行的...