linux centos7 crond regular tasks, process monitoring and restart Tomcat

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/jsqfengbao/article/details/91048391

Write your own system, because the process of the CPU caused by excessive secretly hang, deployed on a system service is not available to the customer losses.

Originally prepared to consider the wording daemon, but Baidu in a circle, feeling quite complex, and later adopted the method linux crond cron job system process monitoring and restart.

crondtab command on centos7 a big difference with the previous version. Before the commands are like this:

First, make sure the server is timed open the Task Scheduler service, only the root user can turn on and off crond service

[root@mimvp-bj script]# service crond status
crond is stopped
[root@mimvp-bj script]# service crond start
Starting crond:                                            [  OK  ]
[root@mimvp-bj script]# service crond status
crond (pid  24577) is running…

But centos7 above command will prompt does not exist, automatically jump to the following commands.

Step 1: Check crond.serivce services since the launch of the state
[root @ localhost ~] # systemctl crond.service IS-Enabled
Disabled

At this point crond.serivce since the start of the state is disabled

 

Step 2: Turn crond.serivce service from the start

[root@localhost ~]# systemctl enable crond.service

[root@localhost ~]# systemctl is-enabled crond.service
enabled

List all of the startup files:

systemctl list-unit-files

List all states to enable the startup file

systemctl list-unit-files | grep enable

Since the start of closed state crond.serivce

systemctl disable crond.service

 

Step 1: View of the startup state crond.service

systemctl status crond.service

Open crond.service service commands

systemctl start crond.service

Stop service command crond.service

systemctl stop crond.service

 

According to the above command to ensure that the service is started centos7 above crond status. Then write a script to monitor the following:

#!/bin/sh
# function:自动监控tomcat进程,挂了就执行重启操作

# 获取tomcat PPID
TomcatID=$(ps -ef | grep tomcat | grep -v 'grep' | awk '{print $2}')
# tomcat_startup
StartTomcat=/opt/service/tomcat/bin/startup.sh

#TOmcatCache=/opt/service/tomcatBlog/work

#定义要监控的页面地址
WebUrl=http://localhost:8080/manager/

# 日志输出
GetPageInfo=/dev/null
TomcatMonitorLog=/opt/service/logs/TomcatMonitor.log

Monitor()
{
	echo "[info]开始监控tomcat...[$(date +'%F %H:%M:%S')]"
	if [ "$TomcatID" ];then
		echo "[info]tomcat进程ID为:$TomcatID."
		#获取返回状态码
		TomcatServiceCode=$(curl -s -o /dev/null --connect-timeout 10 -m 20 $WebUrl -w %{http_code})
		if [ $TomcatServiceCode -eq 200 ]; then
			echo "[info]返回码为$TomcatServiceCode,tomcat启动成功,页面正常."
		else
			echo "[error]访问出错,状态码为$TomcatServiceCode,错误日志已输出到$GetPageInfo"
			echo "[error]开始重启tomcat"
			kill -9 $TomcatID #杀掉原Tomcat进程
			sleep 5
			# rm -rf $TomcatCache #清理Tomcat缓存
			$StartTomcat
		fi
	else
		echo "[error]进程不存在!tomcat自动重启..."
		echo "[info]$StartTomcat,请稍后....."
		#rm -rf $TomcatCache
		$StartTomcat
	fi
	echo "---------------------------------------"
}
Monitor>>$TomcatMonitorLog

This script into its own directory above

Tomcat_startup which is tomcat startup script path, WebUrl is to monitor the page address, only you need to modify their use can be.

Tomcat running state acquisition command

ps -ef | grep tomcat | grep -v 'grep' | awk '{print $ 2}'
access page command returns a status code

curl -s -o /dev/null --connect-timeout 10 -m 20 http://lazyrabbit.tech -w %{http_code}

Next, set the timer task #

Add regular tasks, execute every five minutes

crontab -e
*/5 * * * * /usr/local/tomcat/monitor.sh

View regular tasks

the -l crontab
# possible problems

Neither the JAVA_HOME nor the JRE_HOME environment variable is defined

Least One of Environment THESE AT variable IS needed to RUN the this Program
Tomcat start did not find jdk environment, you need to configure jdk in setclasspath.sh file in the tomcat / bin path path

export JAVA_HOME=/usr/local/java/jdk8
export JRE_HOME=/usr/local/java/jdk8/jre
/usr/local/tomcat/monitor.sh: Permission denied

This script does not have permission, you need to set permissions

chmod 777 monitor.sh

 If you reported the following error:

linux  Neither the JAVA_HOME nor the JRE_HOME environment variable is defined

JAVA_HOME and may be added to the uppermost JRE_HOME tomcat / bin / catalina.sh of

export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_202
export JRE_HOME=/usr/lib/jvm/jdk1.8.0_202/jre
 

Where JAVA_HOME and JRE_HOME into its own address on the server

Catalina.sh then change the permissions: chmod a + x catalina.sh

And then observe whether to perform regular tasks. Get!

There is little need to use a partner, take it

 

Guess you like

Origin blog.csdn.net/jsqfengbao/article/details/91048391