Linux timed run commands
1. Introduction to the Order
Each user has a different list of scheduled tasks to run after logging in with their respective accounts
Copy code code as follows:
Crontab-l
You can view the scheduled tasks for each task and use the following commands to modify your own scheduled tasks
Copy code code as follows:
Crontab-e
Every time you add a task, you must remember to restart the Crond service, otherwise it will not take effect
Copy code code as follows:
Service Crond Restart
2. Examples
The following is a sample of a scheduled task
Copy code code as follows:
#每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
0 4 * mon-wed date
The above meaning is "4th a month and every week on Monday to three morning 11 point time", you change the date to the command you want to execute, and then change the time of execution, you can form a regular execution command
Copy code code as follows:
#每分钟执行一次
*/1 * * * Date
#在/root/run_sh/Write a shell script, named Run.sh, below
#!/bin/sh
echo Hello >/root/hello.txt
Then run the CRONTAB-E command and write
Copy code code as follows:
*/1 * * * */ROOT/RUN_SH/RUN.SH (must be absolute path)
Save Exit Execute service crond Restart, after a minute you will see Hello.txt files under/root.
Copy code code as follows:
#定时运行java程序, write a shell script named run.sh, which reads as follows
#!/bin/sh
cd/root/javaexe/
Java testcron/test
Run Crontab-e write after
*/1 * * * */ROOT/RUN_SH/RUN.SH (must be absolute path)
Restart the Crond service later.
3. The parameter meaning between spaces is as follows
Minutes, days and months of the week to execute the command
The first five arguments are separated by a space, and the last is the command to execute.
Please refer to the details below
Crontab is a handy program to perform a task on a unix/linux system on a regular basis (loop)
Use the Cron service to view the Cron service status with service Crond State, or service Crond start if it is not started,
A cron service is a timed service that can be added to or edited by the crontab command to perform tasks that need to be performed regularly:
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//Remove cron service for no users
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
Edit the/etc/crontab file with one line at the end: 5 * * * Root init 6 This configures the system to automatically restart at 5-point 30 per morning.
You need to set up the Crond service that starts automatically after the system starts, and can be added at the end of the/etc/rc.d/rc.local
Copy code code as follows:
Service Crond Start
If you also need to load additional services at the system start ten, you can continue to add the start command for other services.
Like what:
Copy code code as follows:
Service mysqld Start
Basic usage:
1. Crontab-l
List the current crontab tasks
2. crontab-d
Delete the current crontab task
3. CRONTAB-E (solaris5.8 above is crontab-r)
Edit a crontab task, Ctrl_d end
4. crontab filename
Task list file with filename as crontab and load
Format of crontab file:
The rows in the crontab file are made up of 6 fields, separated by spaces or tabs between the different fields. First 5 fields specify when the command will run
Minutes (0-59)
Hours (0-23)
Date (1-31)
Month (1-12)
Days of the week (0-6, of which 0 represent Sunday)
The 6th field is a string to be executed at the appropriate time
Example:
Copy code code as follows:
#MIN HOUR Day MONTH DayOfWeek COMMAND
#每天早上6点10分
6 * * * Date
#每两个小时
0 */2 * * * DATE (Solaris 5.8 does not seem to support this type of notation)
#晚上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
Add: When using crontab, pay special attention to the environment variables that can be accessed in the run script and the environment variables in the current test environment are not necessarily the same, a more insurance method is to set up the environment variable (export) in the running script program.
(1) First build a file Crond.txt as follows, every 5:36 A.M. reboot
5 * * * reboot
(2) Upload to/opt directory
(3) Run command
Crontab/opt/crond.txt
Crontab-l
Make the configuration file effective: If you have the configuration file in effect, you will have to restart Cron, remember that since the Cron profile is modified under each user. You also need to restart the cron server.
In Fedora and redhat, we should use;
[Root@localhost ~]#/etc/init.d/crond Restart
If you allow Crond to run at boot time, you should change its operating level;
[Root@localhost ~]# chkconfig--levels Crond on
Service Crond Status View Cron service state, if not started service Crond start it, cron service is a timed service, can add or edit tasks that need to be performed by Crontab command
Here is an example file:
Copy code code as follows:
#MIN HOUR Day MONTH DayOfWeek COMMAND
#每天早上6点
106* * * Date
#每两个小时
0*/2* * * Date
#晚上11点到早上8点之间每两个小时, Early Upper point
0 23-7/2,8* * * Date
#每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
0 4* mon-wed Date
#1月份日早上4点
0 4 1 jan* date
Example
LARK:~>CRONTAB-1 lists the user's current crontab.
#MIN HOUR Day MONTH DayOfWeek COMMAND
Ten 6* * * Date
0*/2* * * Date
0 23-7/2,8 * * * Date
Lark:~>
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:
Copy code code as follows:
* * * */usr/local/etc/rc.d/lighttpd restart
The above example shows a 21:30 reboot lighttpd per night. </p> <p>45 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. </p> <p>10 1 * * 6,0/usr/local/etc/rc.d/lighttpd restart
The above example shows the 1:10 restart lighttpd every Saturday and Sunday. </p> <p>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. </p> <p>0 * * 6/usr/local/etc/rc.d/lighttpd restart
The above example indicates that the 11:00 PM restarts lighttpd every Saturday. </p> <p>* */1 * * * */usr/local/etc/rc.d/lighttpd restart
Restart lighttpd</p> <p>* 23-7/1 * * * */usr/local/etc/rc.d/lighttpd restart per hour
Lighttpd</p> <p>0 4 * mon-wed/usr/local/etc/rc.d/lighttpd Restart every hour between 11 o'clock to morning and 7 O ' Night
Each month, number 4th and 11 points per Monday to Wednesday restart lighttpd</p> <p>0 4 1, */usr/local/etc/rc.d/lighttpd restart
Restart lighttpd at 4 o ' January 1.
Timed Execution Open URL