“进程监控脚本并避免重复执行脚本”的版本间的差异
跳到导航
跳到搜索
第10行: | 第10行: | ||
qcloud version 2022 | qcloud version 2022 | ||
− | + | 详细请见最下面 | |
+ | agent_name="$installPath/bin/sgagent" | ||
check_alive() | check_alive() | ||
{ | { | ||
第76行: | 第77行: | ||
echo "Done." | echo "Done." | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | [evan@prod-insurance-core ~]$ cat /usr/local/qcloud/stargate/admin/start.sh | ||
+ | #!/bin/bash | ||
+ | |||
+ | umask 0022 | ||
+ | unset IFS | ||
+ | unset OFS | ||
+ | unset LD_PRELOAD | ||
+ | unset LD_LIBRARY_PATH | ||
+ | export PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' | ||
+ | |||
+ | if [ -w '/usr' ]; then | ||
+ | installPath="/usr/local/qcloud/stargate" | ||
+ | else | ||
+ | installPath="/var/lib/qcloud/stargate" | ||
+ | fi | ||
+ | agent_name="$installPath/bin/sgagent" | ||
+ | |||
+ | check_user() | ||
+ | { | ||
+ | if [ "root" != "`whoami`" ]; then | ||
+ | echo "Only root can execute this script" | ||
+ | exit 2 | ||
+ | fi | ||
+ | } | ||
+ | |||
+ | check_alive() | ||
+ | { | ||
+ | status=`ps ax | grep "$agent_name" | grep -v "grep" |wc -l` | ||
+ | |||
+ | if [ $status -ne 0 ]; then | ||
+ | # process exist | ||
+ | echo "stargate agent already exist" | ||
+ | exit 1 | ||
+ | fi | ||
+ | } | ||
+ | |||
+ | ### Main Begin ### | ||
+ | |||
+ | check_user | ||
+ | check_alive | ||
+ | cd $(dirname $0) | ||
+ | export LD_LIBRARY_PATH=$installPath/lib:$LD_LIBRARY_PATH | ||
+ | $agent_name -d | ||
+ | |||
+ | ret=$? | ||
+ | if [ $ret -eq 0 ] | ||
+ | then | ||
+ | echo "stargate agent run succ" | ||
+ | else | ||
+ | echo "stargate agent run failed, errcode: $ret" | ||
+ | fi | ||
+ | exit $ret | ||
+ | |||
+ | ### Main End ### | ||
+ | |||
</pre> | </pre> | ||
2022年7月12日 (二) 02:26的版本
mysql进程监控脚本shell
cndw version CPID=$$ #$$ 是脚本运行的当前进程ID号 RUN="`ps -ef | grep "$0" | grep -v "grep" | grep -v "$CPID" | wc -l`" #$0 是脚本本身>的名字 [ "$RUN" -gt 1 ] && exit qcloud version 2022 详细请见最下面 agent_name="$installPath/bin/sgagent" check_alive() { status=`ps ax | grep "$agent_name" | grep -v "grep" |wc -l` if [ $status -ne 0 ]; then # process exist echo "stargate agent already exist" exit 1 fi } ### Main Begin ### check_user check_alive #!/bin/bash # 请放到crontab中运行,如(注意要以后台方式运行,因为脚本是常驻不退出的): # Usage */20 * * * * /home/mon/proce-moin.sh > /dev/null 2>&1 & LOCK_FILE="/tmp/my.lock" if [[ -e $LOCK_FILE ]] ; then echo "re-entry, exiting" exit 1 fi ### Placing lock file touch $LOCK_FILE echo -n "Started..." #ps -ef | grep proce-moin #if [ $? -eq 0 ] #then # exit 0 #fi #checkmyself #mypn=`ps -ef | grep proce-moin | grep -v grep | wc -l` ##if [ $mypn -gt 1 ] #if [ -n "$myupn" ] #then # exit 0 #fi # ps -ef | grep mysqld | grep -v grep if [ $? -eq 0 ] then echo "mysqlok" #exit 1 else systemctl start mysqld.service fi sleep 500 ### Removing lock rm -f $LOCK_FILE echo "Done." [evan@prod-insurance-core ~]$ cat /usr/local/qcloud/stargate/admin/start.sh #!/bin/bash umask 0022 unset IFS unset OFS unset LD_PRELOAD unset LD_LIBRARY_PATH export PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' if [ -w '/usr' ]; then installPath="/usr/local/qcloud/stargate" else installPath="/var/lib/qcloud/stargate" fi agent_name="$installPath/bin/sgagent" check_user() { if [ "root" != "`whoami`" ]; then echo "Only root can execute this script" exit 2 fi } check_alive() { status=`ps ax | grep "$agent_name" | grep -v "grep" |wc -l` if [ $status -ne 0 ]; then # process exist echo "stargate agent already exist" exit 1 fi } ### Main Begin ### check_user check_alive cd $(dirname $0) export LD_LIBRARY_PATH=$installPath/lib:$LD_LIBRARY_PATH $agent_name -d ret=$? if [ $ret -eq 0 ] then echo "stargate agent run succ" else echo "stargate agent run failed, errcode: $ret" fi exit $ret ### Main End ###
R
https://github.com/eyjian/mooon/tree/master/mooon