Scheduled Task crontab

You can use the crontab execute a shell script or a series of Linux commands at a specified time.

For example, a system administrator to schedule a backup job so that it runs every day

crontab valid options:

  crontab -e: Modify crontab file if the file does not exist will be created automatically. 

  crontab -l: Display a crontab file. 

   crontab -r: remove crontab files.

  crontab -ir: crontab files before deleting alert the user.

How to add a job to the crontab?

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

It will run at 5:00 every morning /root/bin/backup.sh

crontab the description

The following is the format of crontab file:

minute hour} {} {Day-of-month The {} {} {month The Day-of-Week} {Full-path-to-the shell-Script} 
  minute: interval 0 - 59 
  hour: interval 0 - 23 is 
  Day- of-month: interval 0 - 31 is 
  month The: interval 1-- 12.1 is January 12 December.. 
  Day-of-Week: interval 0 - 7. Sun may be 0 or 7.

Crontab Examples

1. In the 12:01 am run, that every morning a minute. This is an appropriate time to back up, because the system is not loaded.

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

2. Every weekday (Mon - Fri) 11:59 pm will be the backup job.

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

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

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

3. Run the command once every 5 minutes

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

4. The first day of each month 1:10 pm to run

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

5. 11 pm every working day to run.

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

If you want a task scheduling function on the linux platform you can write cron script to achieve.
Perform tasks at a certain frequency
linux default start crond process, crond process does not require the user to start, shut down.
crond process is responsible for reading schedule and execute tasks, users only need to write a script corresponding scheduling cron scheduler configuration file.
cron scheduler has the following documents:

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

if the task is not performed in hourly monthly weekly embodiment, the corresponding crontab crontab may be written to or cron.d directory.
Example:
every minute execute a script /opt/bin/test-cron.sh
can cron.d New Script echo-date.sh the
contents
* / 1 * * * * root / opt / bin / test-cron. sh
at a specified time to run the task
can also be controlled by the at command at a specified time to run tasks
such as:
at -f -v 10:25 test-cron.sh
where -f specify the script file, -v specifies the run-time
quote: ea946d690b = "lophyxp"] first with
contab -l> contabs.tmp
export contab configuration.
Contabs.tmp then edit the file. To add a line about the format:
minute hour day month week command
such as
10 3 * * 0,6 hello
is every Saturday, Sunday 3:10 hello program execution.
15 4 * * 4-6 hello
is hello program execution from 4:15 Dian Thursday to Saturday.
Then
contab contabs.tmp
introducing new configuration commands.
Generally not recommended to directly modify the configuration file / etc / under.
Start cron process methods: /etc/init.d/crond start
boot to start the setup process cron command: chkconfig --add crond
two:
the cron added to the startup script:
# rc-Update vixie the Add-default cron
crontab -l # View your tasks
crontab-e # edit your task
content crontab-r # delete a user's crontab of
examples to explain two:
system set cron: / etc / crontab
    by / etc / crontab file, you can set the system to perform a regular basis the task, of course, to edit this file, and must have root privileges
0 7 * * * root mpg123 ~ / wakeup.mp3
time-moon and circumferential
Example:
0. 4 0 * * the root Emerge Emerge --sync && # -uD World Sunday 4:00, updating system
0 2 1 * * root rm -f / tmp / * # 1 2:00 monthly, clean up files in the / tmp
0 8 6 5 * root mail robin </home/galeki/happy.txt # annual May 6 letter to wish robin him a happy birthday
if I want every 2 minutes will execute a command, or I want to 6:00 every day, 12:00, 18:00 execute the command that sort of period, through the "/" and "" to set : *
/ 2 * # * * * the root ............... performed every two minutes ........
0 6,12,18 * * * the root .. ............. # day 6:00, 12:00, 18:00 ........ performed
every two hours
0 * / 2 * * * echo "have a break now. ">> /tmp/test.txt
every two hours between 23:00 to 8:00 in the morning, eight in the morning
0 23-7 / 2,8 * * * echo "have a good dream :)" >> /tmp/test.txt
4th of every month and each week Monday to Wednesday's 11:00 am
011 4 * 1-3 command line
January 1 at 4 am
0 4 1 1 * command line
harvest: you can put something into which often do, to simplify the workload, such as running every Monday to check the server status, and view reports , kill some processes like ......


* * * * * command
sharing command periphery moon and
the first column represents 1 minute to 59 minutes per * or * / 1 indicates
the second column shows the hours 1 to 23 (0 represents 0 point)
the third column indicates 1 to 31
fourth column represents the month 1 to 12
of five identification number week 0 to 6 (0 for Sunday)
in column 6 of the command to run
some examples crontab file:
30 21 * * * /usr/local/etc/rc.d/lighttpd restart
the example above indicates night 21:30 restart apache.
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
the above example is 1, 10, 422 days per month: 45 restart apache.
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
The above example represents 1 every Saturday, Sunday: 10 to restart apache.
0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
the above example are shown in day 18: 00-23: 00 every 30 minutes to restart apache.
0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
the above examples represent every Saturday in 11: 00 pm restart apache.
* * / 1 * * * /usr/local/etc/rc.d/lighttpd restart
every hour restart Apache
* 23-7 /. 1 * * * the restart /usr/local/etc/rc.d/lighttpd
23:00 between 7 am, every hour restart Apache
0 4 *. 11 Mon-Wed /usr/local/etc/rc.d/lighttpd the restart
of each month 4 11:00 every Monday to Wednesday restart Apache
0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
January 1st of 4:00 restart apache
name: crontab
Access: All users
use:
crontab file [-u the user] - replace with the specified file the current crontab.
crontab - [- u user] - replace the current standard input crontab.
crontab-1 [user] - List of current users crontab.
crontab-E [User] - edit the user the current crontab.
crontab-D [User] -. Remove the user's current crontab
crontab crontab-C DIR- specified directory.
crontab file format: the MHD MD cmd.
M: min (0-59).
H: Hour (0-23).
D: day (1-31).
m: month (1-12).
d: days of the week (Sunday to 0 ~ 6,0).
cmd to run a program, the program is sent to sh executed, the shell only USER, HOME, SHELL three environment variables
Description:
crontab is used to allow users at a fixed time or at regular intervals with enforcement proceedings, in other words, when the user is similar schedule. -u user refers to the setting specified
user time-table, the premise is that you must have permission (for example, a root) can specify the time-table of others. If you do not use the -u user, then, is set represents
set their own time-table.
Parameters:
crontab -e: execute a text editor to set the time-table, the default text editor is VI, if you want to use another text editor, please set the VISUAL environment variable
to specify the text editor (For example, the VISUAL joe setenv)
crontab -r: delete the current time-table
crontab -l: lists the current time-table
crontab file [-u user] - replace the current crontab with the specified file.
Time-table format is as follows:
f1 F2 F3 F4 F5 Program
which is a minute f1, f2 for hours, f3 represents a month in the first few days, f4 represents the month, f5 represents the day of the week. program pledged to execute
a program line.
When f1 represents * represents every hour is executing a program, to be executed when the rest by analogy per minute program, f2 * is
expressed from within a minute to a minute of b to be performed this time, when f1 is ab, f2 ab must perform when expressed from a to b of hours, the rest by analogy
when f1 is * / n when n represents each of a minute time intervals, f2 is * / n represents the time intervals every n hours once, the rest by analogy
denotes a, b, c when f1 is a, b, c, denotes a, b, c ... when ... minutes to perform, f2 is a, b, c, ... when ... hours to be performed, the rest by analogy
users can also all of the first set stored in the archive file, the way with crontab file to set the time-table.
Examples:
# 7:00 executed once per day / bin / LS:
0. 7 * * * / bin / LS
within 12 months, 6 to 12 o'clock in the morning every day, every three hours to perform a / usr / bin / Backup:
0 6-12 / 3 * 12 * / usr / bin / Backup
Monday through Friday at 5:00 pm each day send a letter to [email protected]  :
0. 17 * * 1-5 mail -s "Hi"  [email protected]  </ tmp / MAILDATA
month every day at midnight 0:20, 2:20, 4:20 .. .. perform echo "haha"
20 0-23 / 2 * * * echo "haha"
Note:
when your program is executed in the time specified, the system will send a letter to you, to display the contents of the program execution, if you do not wish to receive such a letter, please leave a space in each row of
rear plus> / dev / null 2> & 1 to
example 2:
# every morning 6:10
10 6 * * * DATE
# every two h
0 * / 2 * * * date
every two hours between # 11 pm to 8:00 am, 8:00 am
0 23-7 / 2,8 * * * DATE
# 4 of every month and every week Monday to Wednesday of the 11:00 am
0 11 4 * Mon-Wed dATE
1 Yuefen morning 4:00 #
0 4 1 jan * dATE
paradigm
$ crontab -l lists the user the current crontab.

发布了14 篇原创文章 · 获赞 0 · 访问量 161

Guess you like

Origin blog.csdn.net/qq_41572664/article/details/103948496