crontab detailed usage timing tasks

From: http://blog.chinaunix.net/uid-25785357-id-3434344.html
 
Using crontab you can execute a shell script or a series of Linux commands at specified times. For example, a system administrator schedules a backup task to run every day

How to add a job to cron?

# crontab –e
0 5 * * * /root/bin/backup.sh

This will run /root/bin/backup.sh every morning at 5am

Description of Cron Items

The following is the format of the crontab file:

{minute} {hour} {day-of-month} {month} {day-of-week} {full-path-to-shell-script} 
o minute: interval is 0 – 59 
o hour: interval is 0 – 23 
o day-of-month: range 0 – 31 
o month: range 1 – 12. 1 is January. 12 is December. 
o Day-of-week: range 0 – 7. Sunday can be 0 or 7.

Crontab example

1. Run at 12:01 am, which is one minute past the morning every day. This is an appropriate time to do a backup, as the system load is not too heavy at this time.

1 0 * * * /root/bin/backup.sh

2. Backup jobs are performed every working day (Mon – Fri) at 11:59 pm.

59 11 * * 1,2,3,4,5 /root/bin/backup.sh

The following example has the same effect as the above example:

59 11 * * 1-5 /root/bin/backup.sh

3. Run a command every 5 minutes

*/5 * * * * /root/bin/check-status.sh

4. Runs at 1:10pm on the first day of every month

10 13 1 * * /root/bin/full-backup.sh

5. Operates every weekday at 11 pm.

0 23 * * 1-5 /root/bin/incremental-backup.sh

Crontab options

The following are valid options for crontab:

o crontab -e : Modify the crontab file. If the file does not exist, it will be created automatically. 
o crontab -l : Display the crontab file. 
o crontab -r : delete the crontab file.
o crontab -ir : warn the user before deleting the crontab file.

The above is the specific use of crontab command.

 

If you need to implement the task scheduling function on the linux platform, you can write a cron script to achieve it.
Executing tasks at a certain frequency
will start the crond process by default, and the crond process does not require the user to start or close it.
The crond process is responsible for reading and executing scheduled tasks. Users only need to write the corresponding scheduling script into the cron scheduling configuration file.
The schedule files of cron are as follows:

   1. crontab
   2. cron.d
   3. cron.daily
   4. cron.hourly
   5. cron.monthly 6.
   cron.weekly 

The corresponding crontab can be written to the crontab or cron.d directory.
Example:
Execute the script /opt/bin/test-cron.sh every minute.
You can create a new script echo-date.sh in cron.d with the
content
*/1 * * * * root /opt/bin/test-cron. sh
to run the task at the specified time
can also be controlled by the at command to run the task at the specified time,
such as:
at -f test-cron.sh -v 10:25
where -f specifies the script file, -v specifies the running time
quote:ea946d690b ="lophyxp"] first use
contab -l >contabs.tmp
Export the configuration of the contab.
Then edit the contabs.tmp file. Add a line in the following format:
minute hour day month week command
such as
10 3 * * 0,6 hello
is to execute the hello program every Saturday and Sunday at 3:10.
15 4 * * 4-6 hello
is to execute the hello program from Thursday to Saturday at 4:15.
Then use the
contab contabs.tmp
command to import the new configuration.
It is generally not recommended to directly modify the relevant configuration files under /etc/.
The method of starting the cron process: /etc/init.d/crond start
The setting command to start the cron process at boot: chkconfig --add crond
Method 2:
Add cron to the startup script:
# rc-update add vixie-cron default
crontab -l #View your task
crontab-e#Edit your task
crontab-r#Delete the content of the user's crontab
Example explanation 2:
System cron settings: /etc/crontab
    Through the /etc/crontab file, the system can be set to execute regularly Of course, in order to edit this file, you must have root privileges
0 7 * * * root mpg123 ~/wakeup.mp3
minutes day month week
example:
0 4 * * 0 root emerge --sync && emerge -uD world #Every Sunday at 4 am, update the system
0 2 1 * * root rm -f /tmp/* # 2 am on the 1st of every month, clean up /tmp The file under
0 8 6 5 * root mail robin < /home/galeki/happy.txt #Every year on May 6th, send a letter to robin to wish him a happy birthday
If, I want to execute a certain command every 2 minutes, or I want to execute commands at 6:00, 12:00, 18:00 every day, and so on, which can be set by "/" and ",":
*/2 * * * * root .......... ..... #execute every two minutes........
0 6,12,18 * * * root ............... #6 every day, 12 Executed at 18:00...
every two hours
0 */2 * * * echo "have a break now." >> /tmp/test.txt
Every time between 11:00 pm and 8:00 am Two hours, 08:00
23-7/2, 8 * * * echo "have a good dream :)" >> /tmp/test.txt
4th of every month and every Monday to Wednesday at 11:00 am
0 11 4 * 1-3 command line
January 1st at 4:00
4 1 1 * command line
harvest: you can put some things you often do into it to simplify the workload, such as checking the running status of the server every Monday, checking reports, killing some processes, etc. ......


* * * * * command
Time division, day, month, week command
, the first column means minutes 1 to 59 every minute, use * or */1 to mean
the second column means hours 1 to 23 (0 means 0 o'clock)
and the third column means the date 1~31
4th column indicates month 1~12
5th column ID number week 0~6 (0 means Sunday)
6th column Commands to run
Some examples of crontab files:
30 21 * * * /usr/local/etc/ rc.d/lighttpd restart
The above example means restarting apache at 21:30 every night.
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
The above example means restart apache at 4:45 on the 1st, 10th, and 22nd of every month.
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
The above example means to restart apache every Saturday and Sunday at 1:10.
0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
The above example means to restart apache every 30 minutes between 18:00 and 23:00 every day.
0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
The above example means to restart apache every Saturday at 11:00 pm.
* */1 * * * /usr/local/etc/rc.d/lighttpd restart
restarts apache every hour
* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
11pm Restart apache every hour until 7am
0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart restart apache 0 4
every 4th of every month and every Monday to Wednesday at 11am
1 jan * /usr/local/etc/rc.d/lighttpd restart
Restart apache at 4:00 on January 1st
Name: crontab
permission: all users
Usage:
crontab file [-u user]-replace with the specified file Current crontab.
crontab-[-u user]-replace current crontab with standard
input.crontab-1[user]-list user's current
crontab.crontab-e[user]-edit user's current
crontab.crontab-d[user]- Delete the user's current crontab.
crontab-c dir- Specifies the directory of the crontab.
Format of the crontab file: MHD md cmd.
M: minutes (0-59).
H: hours (0-23).
D: Day (1-31).
m: month (1-12).
d: Day of the week (0~6, 0 is Sunday).
The program to be run by cmd, the program is sent to sh for execution. This shell has only three environment variables: USER, HOME, SHELL
:
crontab is used to allow users to execute programs at a fixed time or at fixed intervals, in other words, That is, similar to the user's schedule. -u user means to set
the schedule of the specified user, the premise is that you must have the authority (for example, root) to be able to specify the schedule of others. If you don't use -u user, it means to
set your own schedule.
Parameters:
crontab -e : Execute the text editor to set the schedule. The default text editor is VI. If you want to use another text editor, please set the VISUAL environment variable first
to specify which text editor to use (eg setenv VISUAL joe)
crontab -r : delete the current schedule
crontab -l : list the current schedule
crontab file [-u user] - replace the current crontab with the specified file.
The format of the schedule is as follows:
f1 f2 f3 f4 f5 program
where f1 is the minute, f2 is the hour, f3 is the day of the month, f4 is the month, and f5 is the day of the week. program represents the program to be
executed .
When f1 is *, it means that the program should be executed every minute, and when f2 is *, it means that the program should be executed every hour, and so on
. When it is ab, it means that it will be executed from the ath to the bth hour, and the rest of the analogy
When f1 is */n, it means that it will be executed every n-minute interval, and f2 is */n, which means that it will be executed every n-hour interval. For the rest of the analogy
, when f1 is a, b, c,..., it means that the a, b, c,... minute needs to be executed, and when f2 is a, b, c,..., it means a, b, c. ...hours to be executed, other analogy
users can also store all the settings in the file first, and use the crontab file to set the schedule.
Example:
#Execute /bin/ls every day at 7 am:
0 7 * * * /bin/ls
During 12 months, execute /usr/bin/ every 3 hours from 6 am to 12 am every day backup :
0 6-12/3 * 12 * /usr/bin/backup
5:00 pm every day from Monday to Friday Send a letter to  [email protected]  :
0 17 * * 1-5 mail -s "hi"  [email protected]  < /tmp/maildata
every day of the month at midnight 0:20, 2:20, 4:20....do echo "haha"
20 0-23/2 * * * echo "haha "
Note:
When the program is executed at the time you specify, the system will send you a letter showing the content of the program execution. If you do not want to receive such a letter, please add a space
after > /dev/null 2>&1
Example 2:
#Every day at 6:10am
10 6 * * * date #Every
two hours
0 */2 * * * date #Every
day between 11:00pm and 8:00am Two hours, 8:00 am
23-7/2, 8 * * * date #the
4th of every month and every Monday to Wednesday at 11:00 am
0 11 4 * mon-wed date
#1 Month day
4:04 1 jan * date
Example
$crontab -l List the user's current crontab.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325004841&siteId=291194637