scenario

One day suddenly told me that the stand-alone service request failed, I have a look at the situation, is tomcat hang up, but it doesn’t have any error log information, use top command to see also didn’t find what problem, directly after the restart and no problem, so I guess will be running too long, there are some within the program out of the exceptions, but there were no thrown out, I could not locate the problem in a short time, so I thought that I would make a regular restart of Tomcat to ensure the normal operation of the service every day.

The body of the

Restart the Tomcat shell script

#! /bin/bash/etc/profile tomcatPath=" Where tomcat is stored "binPath="$tomcatPath/bin" echo "[info][$(date +'%F %H:%M:%S')] Tomcat is being monitored. $tomcatPath" pid=`ps -ef|grep java | grep catalina | awk '{print $2}'` if [ -n "$pid" ]; Then the echo "[info] [$(date + '% % H: F % M: % S')] running tomcat process as follows: $pid "echo" [info] [$(date + 'M: H: % F % % % S')] tomcat is started, ready to use the shutdown command to shut down..."$binPath"/shutdown.sh"sleep 10 pid=`ps -ef|grep java | grep catalina | awk '{print $2}'` if [ -n "$pid" ]; Then echo "[info][$(date +'%F %H:%M:%S')] Kill 9 $pid echo "[info] [$(date + '% % H: F % M: % S')] kill process!" Sleep 5 else echo "[info][$(date +'%F %H:%M:%S')] Fi else echo "[info] [$(date + '% % H: F % M: % S')] tomcat don't start!" Fi echo "[info] [$(date + '% % H: F % M: % S')] ready to start tomcat..."$binPath"/startup.sh"                   
Copy the code

Now that we’re done, let’s see if the implementation is what we want:

sh /tomcat.sh
Copy the code

If the tomcat restart logs are found, the script is written successfully

Setting a Scheduled Task

Add a scheduled task (cron on centos), let’s look at some parameters for crontab:

-u user: indicates the crontab service of a user. For example, -u ixdba indicates the crontab service of user IXdba. This parameter is usually run by the root user.

File: File is the name of the command file, indicating that file is used as the crontab task list file and crontab is loaded. If this file is not specified on the command line, the crontab command will accept commands typed on standard input (keyboard) and load them into crontab. -e: edits the crontab file of a user. If no user is specified, the crontab file of the current user is edited. -l: displays the crontab file of a user. If no user is specified, the crontab file of the current user is displayed. -r: deletes the crontab file of a user from the /var/spool/cron directory. If no user is specified, the crontab file of the current user is deleted by default. -i: prompts confirmation when deleting the crontab file of the user.

We will write our task to crontab:

crontab -e
Copy the code

Write a scheduled task expression:

58 3 * * * /tomcat.shCopy the code

Then type crontab -l to check whether the expression we wrote has been put into the scheduled task. If we see the expression we just put in, the scheduled task is set successfully ~

By the way

There is a problem? Can you leave me a message or chat privately? If you want to find a job, you can also contact me

Of course, you can also go to my official account “6 Xi Xuan”,

Reply to “Learn” and receive a copy of the Video tutorial for Advanced Architects for Java Engineers

Answer “interview”, can obtain:

MySQL brain Map MySQL brain map

The sunrise hin I am trained programmers, PHP, Android and hardware are done, but in the end or choose to focus on Java, so have what questions to ask the public for discussion (emotional pouring technology can ha ha ha), see words will reply as soon as possible, hope can with everyone common learning progress, about the server architecture, Java core knowledge analysis, career, interview summary and other articles will be pushed irregularly output, welcome to pay attention to ~~~