查看“进程监控脚本并避免重复执行脚本”的源代码
←
进程监控脚本并避免重复执行脚本
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
[[category:shell]] [[category:devops]] ==mysql进程监控脚本shell== === cndw version=== <pre> cndw version CPID=$$ #$$ 是脚本运行的当前进程ID号 RUN="`ps -ef | grep "$0" | grep -v "grep" | grep -v "$CPID" | wc -l`" #$0 是脚本本身>的名字 [ "$RUN" -gt 1 ] && exit #!/bin/bash Mycou=`ps -ef | grep data_from_ok:batch_clean | grep -v grep | wc -l` if [ $Mycou -eq 0 ] then #echo 'run at' >>/data/betch-clean.log date >>/data/betch-clean.log 你要执行的命令 fi </pre> ===qcloud version 2022 === <pre> 详细请见最下面 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 ### #前面这些全是检查func check_user check_alive 你运行的脚本的命令 ret=$? if [ $ret -eq 0 ] then echo "xxxx run succ" else echo "xxxx run failed, errcode: $ret" fi exit $ret #!/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@ ~]$ 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> =reference= [https://blog.csdn.net/sweeper_freedoman/article/details/103132999 Shell防止执行程序被多重启动] https://github.com/eyjian/mooon/tree/master/mooon [https://blog.csdn.net/yuki5233/article/details/83994843 Shell编程---监控检查进程是否存活] [https://cloud.tencent.com/developer/article/1179668 通用的进程监控重拉起bash脚本process_monitor.sh] [https://blog.csdn.net/Primeprime/article/details/78538757 shell脚本监控进程] [https://blog.csdn.net/weixin_44100850/article/details/94588412 Shell脚本监控程序运行情况(重启程序)] [https://segmentfault.com/a/1190000019444629 实现简单的监控脚本(Bash的执行和异常捕获)]
返回至
进程监控脚本并避免重复执行脚本
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
我的导航
关于我
shell
python
ops
linuxchina.net
blog.linuxchina
最近更改
随机页面
帮助
工具
链入页面
相关更改
特殊页面
页面信息