How to use the Linux crontab, SH script, SH script automatically start Tomcat server, SH monitoring system operation situation

Source: Internet
Author: User
Tags tomcat server

1. If you want to use a Linux crontab (similar to Java quartz), you need to start crontab. The way to start, shut down, restart, and reload the configuration for crontab is as follows:

/sbin/service Crond Start//Startup service
/sbin/service Crond stop//Shut down service
/sbin/service crond Restart//Restart service
/sbin/service Crond Reload//Reload Configuration

2, Crontab's command introduction:

A: Add crontab command is: CRONTAB-E and then open the file to write what you want to write (using the same way VI tools)

Method Two is: Directly edit the/etc/crontab file, that is, vi/etc/crontab, add the corresponding task

B: List all currently scheduled tasks: Crontab-l

C: Delete all task scheduling work: Crontab-r

D: Cron expression for task scheduling

* * * * * Program
Time-sharing Weekly command
The 1th column represents minutes 1~59 per minute with * or */1
The 2nd column represents the hour 1~23 (0 means 0 points)
The 3rd column represents the date 1~31
The 4th column represents the month 1~12
5th Column Identification Number Week 0~6 (0 = Sunday)
6th List of commands to run

When the 1th column indicates that the program is to be executed every minute, the 2nd column indicates that it executes every hour, and the rest of the analogy
when the 1th column is a-B, the period from the minute of a to the minute is executed, and the 2nd column A-B indicates Line, the rest of the analogy
when the 1th column */n is executed once every n minutes, the 2nd column */n indicates that every n hour interval is executed, and the rest of the analogy
when the 1th column is a, B, C,... A, B, C,... Minutes to execute, 2nd column A, B, C,... The time indicated that the first, B, c ... Hours to execute, and the rest of the analogy

* * * */USR/LOCAL/ETC/RC.D/LIGHTTPD restart    The above example indicates a 21:30 restart lighttpd per night.
4 1,10,22 * */USR/LOCAL/ETC/RC.D/LIGHTTPD restart     The above example shows 4:45 restart lighttpd per month for 1, 10, 22nd.
1 * * 6,0/USR/LOCAL/ETC/RC.D/LIGHTTPD restart     The above example shows the 1:10 restart lighttpd per Saturday and Sunday.
0,30 18-23 * * */USR/LOCAL/ETC/RC.D/LIGHTTPD restart    The above example shows that the LIGHTTPD is restarted every 30 minutes from 18:00 to 23:00 every day.
0 * * 6/USR/LOCAL/ETC/RC.D/LIGHTTPD restart         The above example indicates that every Saturday 11:00 PM Restart LIGHTTPD.
* */1 * * * */USR/LOCAL/ETC/RC.D/LIGHTTPD restart           Restart every hour lighttpd
* 23-7/1 * * */usr/local/etc/rc.d/lighttpd restart    11 o'clock to 7 in the morning and restarts every hour lighttpd
0 4 * mon-wed/usr/local/etc/rc.d/lighttpd restart  4th per month and 11-point restart from Monday to Wednesday lighttpd
0 4 1 Jan */USR/LOCAL/ETC/R C.D/LIGHTTPD restart   January 1 4-point restart lighttpd

----------------------------------------------

The case restarts Tomcat by restarting Tomcat at a specified time, or by monitoring the startup of the Web project crontab:

1, write crontabtongweb.sh (and give this file executable permissions: chmod 777 crontabtongweb.sh)

The code is as follows:

#/usr/tomcat7/apache-tomcat-7.0.47/bin/shutdown.sh
#sleep 1m
#/usr/tomcat7/apache-tomcat-7.0.47/bin/startup.sh

#echo Test >> $ (date-d "Today" + "%y%m%d_%h%m%s"). Log
#echo Test >> $ (date-d "Today" + "%y%m%d"). Log

#kill Tomcat PID
PS aux|grep tongweb|grep Start|awk ' {print $} ' |xargs kill-9
#log
#echo ' kill Tongweb pid ' >> $ (date-d "Today" + "%y-%m-%d"). Log

#sleep 1m
/root/tongweb5.0/bin/start.sh
echo $ (date-d "Today" + "%y-%m-%d%h:%m:%s") tongweb restart >>/root/tongweb5.0/crontab_log/css_restart.log

Save these configurations, and then give

2, write crontabmonitor.sh (and give the file executable permissions)

#/usr/tomcat7/apache-tomcat-7.0.47/bin/shutdown.sh
#sleep 1m
#/usr/tomcat7/apache-tomcat-7.0.47/bin/startup.sh

#echo Test >> $ (date-d "Today" + "%y%m%d_%h%m%s"). Log
#echo Test >> $ (date-d "Today" + "%y%m%d"). Log


Rm-f index.html
Wget-t 10-t 3-q http://192.168.58.2/swordcms/
if [!-e index.html]; Then
#kill Tomcat PID
PS aux|grep tongweb|grep Start|awk ' {print $} ' |xargs kill-9
#log
#echo ' kill Tongweb pid ' >> $ (date-d "Today" + "%y-%m-%d"). Log
#sleep 1m
/root/tongweb5.0/bin/start.sh
echo $ (date-d "Today" + "%y-%m-%d%h:%m:%s") Tongweb error Restart >>/root/tongweb5.0/crontab_log/css_restart.log
#else
# echo $ (date-d "Today" + "%y-%m-%d%h:%m:%s") Tongweb normal >>/root/tongweb5.0/crontab_log/css_normal.log
Fi

Save Configuration

3, set crontab, use CRONTAB-E to write the following content:

* */59 * * */root/tongweb5.0/bin/crontabtongweb.sh
* */5 * * */root/tongweb5.0/bin/crontabmonitor.sh

4. Re-load the configuration into:

/sbin/service Crond Reload

/sbin/service Crond Restart

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.