Java real-time program in the process of occasional abnormal information interruption in the case, through the shell script to complete the self-boot.
The following is a shell script that monitors a real-time Java program.
Determine if the program is running by checking the Java program's process every 10 seconds. If no programs are found, execute the program's startup script.
For the status information during startup, the nail robot is sent to the Monitoring group in real time, so that the maintenance personnel can understand the program's abnormality.
Sets the maximum number of attempts by maxretry in the script.
#!/bin/SH# maximum Retries maxretry=3# Number of attempts counter retrytimes=0# consumer Parameters consumer_bootstrap_server=192.168.0.1:9092Consumergroup=Bigdataconsumertopic=test1Consumerautooffsetreset=earliest# producer Parameters Producer_bootstrap_server=10.10.10.10:9092Producertopic=test2# Loglogfile=programe. log# send spike message parameter # Note: ${nowtime} The corresponding date parameter cannot have a space # Note: ${programe} is the main program name URL for the executing jar program='https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 'programe=Xxxxxprogrameserver=192.168.0.1content="'# Call function to send nailsfunctionSenddingtalk () {Java-djava.ext.dirs=/home/hadoop/shell/robot/lib com. Dingtalkrobot ${1} ${2} ${3} ${4} ${5}} #sendDingtalk ${url} ${programe} ${server} ${nowtime} ${content} ( while true; DoCount=`PS-ef |grep${programe} |grep-Vgrep|WC-l ' #Echo 'Number of processes:'$count >>${logfile} nowtime=`Date--Date='0 days ago' "+%y-%m-%d_%h:%m:%s"` if[${count}-lt1]; ThenRetrytimes=$[$retryTimes +1] Content='Program Detected "'${programe}'"Not started, trying to start ... Section' "${retrytimes}"'Times' Echo${nowtime} ${content} >>${logfile} senddingtalk ${url} ${programe} ${server} ${nowtime} ${content}if[$retryTimes-ge $maxRetry]; Thencontent='Program Detected "'${programe}'"Exception, try to start' "${maxretry}"'Times failed, program quits, please manually solve' Echo${content} >>${logfile} senddingtalk ${url} ${programe} ${server} ${nowtime} ${content} breakfinohup Java-djava.ext.dirs=lib Com.xxx.Programe ${consumer_bootstrap_server} ${consumergroup} ${consumertopic} ${consumerautooffsetreset} $ {producer_bootstrap_server} ${producertopic}2>&1&Else if[$retryTimes-GT0]; Thencontent='program "'${programe}'"started successfully ... Section' "${retrytimes}"'Times' Echo${content} >>${logfile} senddingtalk ${url} ${programe} ${server} ${nowtime} ${content}Else Echo${nowtime}'program "'${programe}'"... Running in progress'>>${logfile}fiLet retrytimes=0 fi Sleep Ten Done) & >> Shell.log
After the script starts:
You can view real-time monitoring information in the nail Group:
After the program starts successfully, the following information is prompted:
Shell+ nailing robot to complete the Java program after the interrupt self-start and real-time monitoring