The scheduled tasks crontab and tomcat of Centos are automatically restarted.

Source: Internet
Author: User
The crond resident command for task scheduling crond is the command for linux to regularly execute programs. After the operating system is installed, the task scheduling command is started by default. The crond command periodically checks whether there is any job to be executed every minute. if there is any job to be executed, the job is automatically executed. For linux task scheduling, there are two main types of jobs: 1. jobs executed by the system: jobs to be executed by the system periodically, such as backing up system data and clearing the cache. 2. jobs executed by individuals: what a user needs to do on a regular basis

Crond resident command for task scheduling
Crond is a linux command used to regularly execute programs. After the operating system is installed, the task scheduling command is started by default. The crond command periodically checks whether there is any job to be executed every minute. if there is any job to be executed, the job is automatically executed. Linux task scheduling mainly includes the following two types:
1. work performed by the system: work to be performed periodically by the system, such as backing up system data and clearing cache
2. personal work: the work that a user regularly performs, such as checking whether there are new emails on the email server every 10 minutes. this work can be set by each user.

I. writing of the task scheduling setting file
The format is as follows:
Minute Hour Day Month Dayofweek command
Minute Hour Day week Command

The meaning of each field is as follows:
Minute executes the task in minutes of each hour.
Hour executes the task the Hour of the day
Day of each month
Month: the Month of each year.
DayOfWeek executes this task the day of the week
Command specifies the program to be executed
In these fields, except that "Command" is a field that must be specified each time, other fields are optional fields, which can be determined as needed. For unspecified fields, use "*" to fill their positions.

Example:
1. specify to execute the ls command every 5th minutes of an hour.
5 * ls

2. specify to execute the ls command at every day
30 5 * ls

3. specify to execute the ls command at on October 8.
30 7 8 ** ls

4. specify to execute the ls command at on January 1, June 8 each year.
30 5 8 6 * ls

5. specify to execute the ls command at every Sunday.
30 6 ** 0 ls
Note: 0 indicates Sunday, 1 indicates Monday, and so on. sun indicates Sunday, mon indicates Monday, and so on.

6. execute the ls command at on the 10th and 20th every month.
30 3 10, 20 ** ls
Note: "," is used to connect multiple discontinuous periods.

7. execute the ls command at every day.
25 8-11 *** ls
Note: "-" is used to connect consecutive periods.

8. execute the ls command every 15 minutes.
*/15 * ls
That is, the ls command is executed every hour for 0th 15 30 45 60 minutes.

9. execute the ls command at every 10 days.
30 6 */10 ** ls
That is, execute the ls command once every month at on May 1, 11, 21, and 31.

10. execute all executable files in the/etc/cron. daily directory as root at every day.
50 7 * root run-parts/etc/cron. daily
Note: the run-parts parameter indicates all executable files in the directory after execution.

II. new scheduling tasks
There are two ways to add a scheduling task:
1. enter crontab-e in the command line and add the corresponding task. The wq disk will exit.
2. directly edit the/etc/crontab file, that is, vi/etc/crontab, and add the corresponding task.

3. View scheduling tasks
Crontab-l // list all current scheduling tasks
Crontab-l-u jp // list all scheduling tasks of user jp

4. delete task scheduling
Crontab-r // delete all task scheduling tasks

V. Transition of task scheduling execution results
Example 1: execute the ls command at every day and output the result to the/jp/test file.
30 5 * ls>/jp/test 2> & 1
Note: 2> & 1 indicates the execution result and error message.

Edit the/etc/crontab file configuration cron

The cron service reads not only all files in/var/spool/cron every minute, but also/etc/crontab once. Therefore, we configure this file
You can use the cron service to do something. Crontab configuration is intended for a user, and editing/etc/crontab is a system task. The file format of this file is:

SHELL =/bin/bash

PATH =/sbin:/bin:/usr/sbin:/usr/bin

MAILTO = root // if an error occurs or data is output, the data is sent to this account as an email.

HOME = // path of the user running. the root directory is used here.

# Run-parts

01 *** root run-parts/etc/cron. hourly // execute the script in/etc/cron. hourly every hour

02 4 *** root run-parts/etc/cron. daily // run the script in/etc/cron. daily every day.

22 4 ** 0 root run-parts/etc/cron. weekly // execute the script in/etc/cron. weekly every week

42 4 1 ** root run-parts/etc/cron. monthly // run the script in/etc/cron. monthly every month.

Note the "run-parts" parameter. If this parameter is removed, you can write a script name to be run, instead of the folder name.

Parameters:
Crontab-e: execute the text editor to set the time table. the preset text editor is VI. if you want to use another text editor, set the VISUAL environment variable to specify the text editor (for example, setenv VISUAL joe)
Crontab-r: delete the current time table
Crontab-l: to list the current time table
Crontab file [-u user]-replace the current crontab with the specified file.
The time table format is as follows:
F1 f2 f3 f4 f5 program
F1 indicates the minute, f2 indicates the hour, f3 indicates the day of the month, f4 indicates the month, and f5 indicates the day of the week. Program indicates the program to be executed.
When f1 is *, the program is executed every minute. when f2 is *, the program is executed every hour, and so on.
When f1 is a-B, it indicates that execution is performed from the minute a to the minute B. When f2 is a-B, it indicates that execution is performed from the hour a to the hour B, and so on
When f1 is */n, it indicates execution is performed every n minutes. if f2 is */n, it indicates execution is performed every n hours, and so on.
When f1 is a, B, c ,... a, B, c ,... execute in minutes. f2 is a, B, c ,... a, B, c... execution in hours, and so on
You can also store all settings in the file first, and use crontab file to set the time table.

Example:
# Run/bin/ls at every morning:
0 7 ***/bin/ls
During October 11, December, execute/usr/bin/backup every three hours from to every day:
0 6-12/3*12 */usr/bin/backup
Send a letter:
0 17 ** 1-5 mail-s "hi" </tmp/maildata
Execute echo "haha" at midnight, 00:20, and 02:20 every month"
20 0-23/2 *** echo "haha"
Note:
When the program is executed at the specified time, the system will send you a letter showing the program execution content. if you do not want to receive such a letter, add>/dev/null 2> & 1 after each line is empty.

Example 2:
#06:10 every morning
10 6 **** date
# Every two hours
0 */2 * date
# Every two hours from PM to AM, AM
0 23-7/2, 8 **** date
# Am on the 4th day of each month and from Monday to Wednesday of each week
0 11 4 * mon-wed date
# A.m. of July
0 4 1 jan * date
Example
$ Crontab-l list the current crontab of a user.

Linux task plan

2008/11/21 10: 35

Cron is a linux scheduled execution tool that can run jobs without human intervention. Since Cron is a built-in service in Linux, but it does not automatically get up, you can use the following methods to start and close this service:

/Sbin/service crond start // start the service
/Sbin/service crond stop // close the service
/Sbin/service crond restart // restart the service
/Sbin/service crond reload // reload the configuration

You can also enable the service automatically when the system starts:
Add:
/Sbin/service crond start

Now that the Cron service is in the process, we can use it. The Cron service provides the following interfaces for you to use:

1. directly use the crontab command to edit

The cron service provides the crontab command to set the cron service. The following are some parameters and descriptions of this command:

Crontab-u // set a user's cron service. Generally, the root user needs this parameter when executing this command.
Crontab-l // list the details of a user's cron service
Crontab-r // delete a user's cron service
Crontab-e // edit a user's cron service

For example, to view your cron settings as root: crontab-u root-l
For another example, root wants to delete fred's cron settings: crontab-u fred-r
When editing the cron service, the edited content has some formats and conventions. enter crontab-u root-e.
In vi editing mode, the edited content must conform to the following format: */1 ***** ls>/tmp/ls.txt

The first part of this format is the time setting, and the last part is the command to be executed. if there are too many commands to be executed, you can write these commands into a script, then you can directly call this script here. remember to write the complete path of the command during the call. We have a certain agreement on the time setting. the first five * numbers represent five numbers. the value range and meaning of the numbers are as follows:

Minutes (0-59)
Small? R (0-23)
Date (1-31)
Month (1-12)
Week (0-6) // 0 represents Sunday

In addition to numbers, there are also several special symbols: "*", "/", "-", * representing all numbers in the value range, "/" indicates the meaning of each, "*/5" indicates every five units, "-" indicates the number from a number to a number, "," separate several discrete numbers. The following examples illustrate the problem:

Every morning

0 6 *** echo "Good morning. ">/tmp/test.txt // note that no output is visible from the screen with pure echo, because cron has emailed any output to the root mailbox.

Every two hours

0 */2 *** echo "Have a break now.">/tmp/test.txt

Every two hours from PM to AM, AM

0 23-7/2, 8 *** echo "Have a good dream :)">/tmp/test.txt

Am from Monday 4 to Wednesday every week

0 11 4*1-3 command line

AM, January 1, January 1

0 4 1 1 * command line

After you edit the cron settings of a user, cron automatically generates a file with the same name under/var/spool/cron. The cron information of this user is recorded in this file, this file cannot be edited directly. you can use crontab-e to edit it. The cron reads the file every minute after it is started, and checks whether to execute the commands in it. Therefore, you do not need to restart the cron service after the file is modified.

2. edit the configuration cron in the/etc/crontab file.

The cron service not only reads all files in/var/spool/cron every minute, but also reads/etc/crontab once. Therefore, we can use the cron service to configure this file. Crontab configuration is intended for a user, and editing/etc/crontab is a system task. The file format of this file is:

SHELL =/bin/bash

PATH =/sbin:/bin:/usr/sbin:/usr/bin

MAILTO = root // if an error occurs or data is output, the data is sent to this account as an email.

HOME = // path of the user running. the root directory is used here.

# Run-parts

01 *** root run-parts/etc/cron. hourly // execute the script in/etc/cron. hourly every hour
02 4 *** root run-parts/etc/cron. daily // run the script in/etc/cron. daily every day.
22 4 ** 0 root run-parts/etc/cron. weekly // execute the script in/etc/cron. weekly every week
42 4 1 ** root run-parts/etc/cron. monthly // run the script in/etc/cron. monthly every month.

Note the "run-parts" parameter. If this parameter is removed, you can write a script name to be run, instead of the folder name.

--------------------------------------
Basic format:
* *** Command
Hour, day, month, and Week commands

The 1st column indicates minute 1 ~ 59. each minute is represented by * or */1.
The first column indicates the hour 1 ~ 23 (0 indicates 0 points)
The 3rd column indicates the date 1 ~ 31
The 4th column indicates the month 1 ~ 12
The identification number of column 5th is from day of the week to day ~ 6 (0 indicates Sunday)
6th columns of commands to run

Some examples of crontab files:

30 21 ***/usr/local/etc/rc. d/lighttpd restart
The above example indicates restarting lighttpd at every night.

45 4, 10, 22 **/usr/local/etc/rc. d/lighttpd restart
The preceding example indicates that the lighttpd is restarted at on the 1st, 10th, and 22th of each month.

10 1 ** 6, 0/usr/local/etc/rc. d/lighttpd restart
The preceding example indicates restarting lighttpd at every Saturday and Sunday.

0, 30 18-23 ***/usr/local/etc/rc. d/lighttpd restart
The preceding example indicates restarting lighttpd every 30 minutes between and every day.

0 23 ** 6/usr/local/etc/rc. d/lighttpd restart
The preceding example indicates restarting lighttpd at every Saturday.

**/1 ***/usr/local/etc/rc. d/lighttpd restart
Restart lighttpd every hour

* 23-7/1 ***/usr/local/etc/rc. d/lighttpd restart
Restart lighttpd every hour between PM and PM.

0 11 4 * mon-wed/usr/local/etc/rc. d/lighttpd restart
Restart lighttpd from every Monday to Wednesday on the 4th of each month.

0 4 1 jan */usr/local/etc/rc. d/lighttpd restart
Restart lighttpd at on January 1, January 1.

Restart tomcat regularly in linux

Assume that tomcat is placed in the/opt directory.

1. create restart. sh in the/opt/tomcat Directory. the content is as follows:
#! /Bin/sh
/Opt/tomcat/bin/shutdown. sh
Sleep 1 m
/Opt/tomcat/bin/startup. sh
After the script is created, test the script to see if it is correct.

2. after the script is correct, add the command to the crond service to execute the script at a certain moment. the method is as follows:
Command: crontab-e
Content: 30 03 ***/opt/tomcat/restart. sh
This completes. the specific meaning of crontab content is: execute the script at 03:30 every day. For detailed parameter settings, google (or Baidu)

3. in the online tutorial, we recommend that you restart the crond service. The command is as follows:
Service crond stop
Service crond start

Please note that restart. we recommend that you use vi to edit the sh script in linux. if you edit the sh script in windows, there may be encoding problems. I have encountered this problem before. we recommend that you do not make similar errors.

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.