Crond resident command for task scheduling
Crond is a command that Linux uses to execute programs on a regular basis. When the operating system is installed, the task Scheduling command is started by default. The Crond order will periodically check whether there is any work to be done and will automatically carry out the work if there is any work to be done. and Linux task scheduling is mainly divided into the following two categories:
1, the system implementation of the work: the system periodically to perform the work, such as backup system data, clean cache
2, Personal work: a user to do the work on a regular basis, such as every 10 minutes to check the mail server for new letters, these work can be set by each user.
I. The writing of task scheduling setup file
The format is as follows:
Minute Hour Day Month dayofweek command
Minutes hours days month days per week order
Each field represents the following meanings:
Minute the first few minutes of each hour
Hour a few hours a day to perform this task
Day of the month to perform this task
Month a few months of the year to perform this task
DayOfWeek to perform the task on the first day of the week
Command Specifies the program to be executed
In these fields, except that the Command is a field that must be specified each time, the other fields are optional fields that can be determined as needed. For fields that are not specified, use "*" to fill their position.
Examples are as follows:
1, specify the first 5 minutes per hour to execute the LS command
5 * * * * ls
2, specify 5:30 per day to execute the LS command
5 * * * ls
3, the designated monthly 8th number 7:30 points Execution ls command
7 8 * * ls
4, designated every June 8 5:30 to execute the LS command
5 8 6 * ls
5, specify every Sunday of 6:30 execute LS command
6 * * 0 ls
Note: 0 means Sunday, 1 for the Week 1, and so on, can also be expressed in English, Sun said Sunday, Mon said Monday.
6, monthly 10th and 20th 3:30 execute LS command
3 10,20 * * ls
Note: "," used to connect multiple discontinuous periods
7, 8-11 per day, the first 25 minutes to execute the LS command
8-11 * * * ls
Note: "-" to connect a continuous period of time
8. Execute the LS command every 15 minutes
*/15 * * * ls
That is, the No. 0 15 30 45 60 minutes per hour executes the LS command
9, every month, every 10 days 6:30 execute the LS command
6 */10 * * ls
That is, 1, 11, 21, 31st of the month, yes 6:30 executes the LS command.
10. Execute all executable files in the/etc/cron.daily directory as root 7:50 every day
7 * * * Root run-parts/etc/cron.daily
Note: The Run-parts parameter indicates that all executable files in the following directory are executed.
Second, the new scheduling task
There are two ways to add new scheduling tasks:
1, at the command line input: CRONTAB-E and then add the corresponding tasks, Wq disk exit.
2, directly edit/etc/crontab file, that is, vi/etc/crontab, add the corresponding task.
Third, view the scheduling task
Crontab-l//List all current Scheduled tasks
Crontab-l-u JP//List all scheduling tasks for user JP
Iv. Delete Task scheduling work
Crontab-r//Delete all task scheduling work
V. Steering of the implementation results of task scheduling
Example 1: Execute the LS command 5:30 every day and output the result to the/jp/test file
5 * * * ls >/jp/test 2>&1
Note: 2>&1 represents execution results and error messages.
Edit/etc/crontab File Configuration cron
The cron service does not only have to read all the files in the/var/spool/cron once per minute, but also read the/etc/crontab, so we configure this file
can use cron service to do some things. The crontab configuration is for a user, and editing/etc/crontab is a task for the system. The file format for this file is:
Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root//If there is an error, or if there is data output, the data is sent to this account as mail
home=///user Run path, this is the root directory
# Run-parts
* * * * * root run-parts/etc/cron.hourly//hourly execution of scripts within/etc/cron.hourly
4 * * * Root run-parts/etc/cron.daily//daily execution of/etc/cron.daily scripts
4 * * 0 root run-parts/etc/cron.weekly//per week execute/etc/cron.weekly script
4 1 * * Root run-parts/etc/cron.monthly//monthly to execute/etc/cron.monthly script
Attention to the "run-parts" This parameter, if you remove this parameter, then you can write a script to run the name, not the folder name.
Parameters:
Crontab-e: Execute a text editor to set the time table, the default text editor is VI, if you want to use a different text editor, please set the VISUAL environment variable to specify the use of that text editor (for example, Setenv VISUAL Joe)
Crontab-r: Deletes the current schedule
Crontab-l: Lists the current schedule table
crontab file [-u user]-replaces the current crontab with the specified file. The format of the
Time table is as follows:
F1 F2 f3 f4 f5 program
where F1 is for minutes, F2 represents hours, F3 represents the day ordinal of a month, F4 represents the month, and F5 represents the day of the one week. Program represents the programs to be executed. The
when F1 is * means that the PROGRAM,F2 is executed every minute for * to execute the program every hour. The rest of the analogy
when F1 is a-b to be executed from the time of the first a minute to the second B minute, F2 is the a-b when it represents a small is executed, the rest of the analogy
when F1 is */n is performed once every n minute interval, F2 for */n for every n-hour interval, and so on
when F1 is a, B, C,... The first A, B, C,... Minutes to execute, F2 for a, B, C,... The first is a, B, c ... Hours to execute, the rest of the analogy
users can also store all the settings in file file, using crontab file to set the schedule.
Example:
#每天早上7点执行一次/bin/ls:
0 7 * * */BIN/LS
In December, every day at 6 to 12, every 3 hours of the morning/usr/bin/backup are performed:
0 6-12/3 */usr/bin/backup
Monday to Friday send a letter 5:00 every day:
0 * * 1-5 mail-s "HI" </tmp/maildata
Every month at midnight 0:20, 2:20, 4:20 .... Execute echo "haha"
0-23/2 * * * echo "haha"
Attention:
When the program is executed at the time you specify, the system will send you a letter showing the execution of the program, if you do not wish to receive such a letter, please add >/dev/null 2>&1 after each line blank
Example 2:
#每天早上6点10分
6 * * * Date
#每两个小时
0 */2 * * * Date
#晚上11点到早上8点之间每两个小时, 8 in the morning.
0 23-7/2,8 * * * Date
#每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
0 4 * mon-wed date
#1月份日早上4点
0 4 1, * date
Example
$crontab-L lists the user's current crontab.
Linux Task Scheduler
2008/11/21 10:35
Cron is a regular execution tool under Linux that can run jobs without human intervention. Because Cron is a built-in service for Linux, it does not automatically get up and you can start and close this service in the following ways: /sbin/service crond Start//boot service /sbin/service Crond Stop//Off service /sbin/service crond Restart//Restart service /sbin/service Crond Reload//Reload Configuration You can also start this service automatically when the system is started: At the end of the/etc/rc.d/rc.local script, add: /sbin/service Crond Start Now that the Cron service is in the process, we can use this service, and the Cron service provides the following interfaces for everyone to use: 1. Edit directly with crontab command The Cron service provides the crontab command to set the Cron service, and here are some of the parameters and instructions for this command: Crontab-u//Set a user's Cron service, which is required by the general root user when executing this command CRONTAB-L//list 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, root view your cron settings: Crontab-u root-l Again for example, Root wants to delete Fred's cron settings: Crontab-u fred-r When editing a cron service, there are some formatting and conventions for editing the content, input: Crontab-u root-e into the vi editing mode, the contents of the edit must conform to the following format: */1 * * * * ls >>/tmp/ls.txt The first part of this format is the set of time, the next part is the command to execute, if there are too many commands to execute, you can write these commands into a script, and then call the script here directly, and then remember to write the full path of the command. Time set we have a certain agreement, the front five * number represents five digits, the number of the value range and the meaning is as follows: Minutes (0-59) Hours (0-23) Date (1-31) Month (1-12) Week (0-6)//0 on behalf of Sunday In addition to the numbers there are a few special symbols is "*", "/" and "-", ",", * represents all the values within the range of the number, "/" for each meaning, "*/5" means every 5 units, "-" representing from a number to a number, "," separate several discrete numbers. Here are a few examples to illustrate the problem: Every morning at 6. 0 6 * * * echo "Good morning." >>/tmp/test.txt//Note that pure echo does not see any output from the screen because cron emails any output to Root's mailbox. Every two hours 0 */2 * * echo "Have a break now." >>/tmp/test.txt Between 11 o'clock and 8 in the morning, every two hours, eight in the morning. 0 23-7/2,8 * * * echo "Have a Good Dream:)" >>/tmp/test.txt 4th a month and a week on Monday to three in the morning 11. 0 4 * 1-3 command line January 1 morning, 4. 0 4 1 1 * command line After each edit of a user's cron settings, Cron automatically generates a file with the same name as this user under/var/spool/cron, and the user's cron information is recorded in this file, which is not directly editable and can only be edited with CRONTAB-E. After Cron starts, read the file every time it is written, and check if you want to execute the commands inside. Therefore, you do not need to restart the Cron service after this file has been modified. 2. Edit/etc/crontab File Configuration cron The cron service does not only have to read all the files in the/var/spool/cron every minute, but also read the/etc/crontab, so we can configure the file to do something with the cron service. The crontab configuration is for a user, and editing/etc/crontab is a task for the system. The file format for this file is: Shell=/bin/bash Path=/sbin:/bin:/usr/sbin:/usr/bin Mailto=root//If there is an error, or if there is data output, the data is sent to this account as mail home=///user Run path, this is the root directory # Run-parts * * * * * root run-parts/etc/cron.hourly//hourly execution of scripts within/etc/cron.hourly 4 * * * Root run-parts/etc/cron.daily//daily execution of/etc/cron.daily scripts 4 * * 0 root run-parts/etc/cron.weekly//per week execute/etc/cron.weekly script 4 1 * * Root run-parts/etc/cron.monthly//monthly to execute/etc/cron.monthly script Attention to the "run-parts" This parameter, if you remove this parameter, then you can write a script to run the name, not the folder name. -------------------------------------- Basic format: * * * * command Time-sharing and Lunar Week command The 1th column represents minutes 1~59 per minute with * or */1 The 2nd column represents the hour 1~23 (0 for 0 points) The 3rd column represents the date 1~31 The 4th column represents the month 1~12 The 5th list of the week 0~6 (0 for Sunday) 6th column the command to run Some examples of crontab files: * * * */usr/local/etc/rc.d/lighttpd restart The above example shows a 21:30 reboot lighttpd per night. 4 1,10,22 * */USR/LOCAL/ETC/RC.D/LIGHTTPD restart The above example represents the 4:45 restart LIGHTTPD for 1, 10, and 22nd of each month. 1 * * 6,0/USR/LOCAL/ETC/RC.D/LIGHTTPD restart The above example shows the 1:10 restart lighttpd every Saturday and Sunday. 0,30 18-23 * * * */usr/local/etc/rc.d/lighttpd restart The example above indicates 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 the 11:00 PM restarts lighttpd every Saturday. * */1 * * * */usr/local/etc/rc.d/lighttpd restart Restart lighttpd every hour. * 23-7/1 * * * */usr/local/etc/rc.d/lighttpd restart Between 11 o'clock and 7 in the morning, restart lighttpd every hour. 0 4 * mon-wed/usr/local/etc/rc.d/lighttpd restart 4th per month with 11 points per Monday to Wednesday restart LIGHTTPD 0 4 1/USR/LOCAL/ETC/RC.D/LIGHTTPD * Restart Restart lighttpd at 4 o ' January 1. |
Linux timed reboot tomcat
If Tomcat is placed in the/OPT directory
1, in the/opt/tomcat directory to establish restart.sh, the contents are as follows
#!/bin/sh
/opt/tomcat/bin/shutdown.sh
Sleep 1m
/opt/tomcat/bin/startup.sh
After you've built it, you can test the script to see if there are any problems with the script.
(2) Re-write a script
#!/bin/bash
pid= ' ps auwx | grep java | grep "/usr/local/tomcat/" | awk ' {print $} '
if (test-n "$pid") then
/bin/kill-9 $pid >/dev/null 2>&1 sleep
2
cd/usr/local/tomcat/bin/
/bin/bash startup.sh >/ Dev/null 2>&1
elif
(test-z "$pid") then
cd/usr/local/tomcat/bin/
/bin/bash startup.sh >/ Dev/null 2>&1
fi
and add it to crontab.
2, after the script is no problem, to add a certain time in the Crond service to execute this script command, the following methods
Command: CRONTAB-E
Content: * * */opt/tomcat/restart.sh
In this case, the exact meaning of the crontab content is: Execute that script at 3:30 every day. Detailed parameter settings please Google (or Baidu)
3, in the online search tutorial recommended restart Crond service, the following command
Service Crond Stop
Service Crond Start
To remind you that the restart.sh script is recommended to use the VI editor under Linux, if editing in Windows may have coding problems, I have encountered this situation, I suggest that we do not make similar mistakes.
Automatic restart problem solved, but also brought a new problem: The site is running well, but some places show the characters are garbled, manual restart Tomcat after no garbled. The reason is that manual restart is not the same as the character set that is automatically restarted by the default system task. The workaround is to edit the/usr/local/apache-tomcat-6.0.20/bin/catalina.sh and add one line inside:
Catalina_opts=-dfile.encoding=utf8
OK (the specific encoding is modified according to the page code).