Linux system is set by the cron (crond) system service to control. Linux system above originally had planned a lot of work, so this system services are enabled by default. In addition, since the user himself can set a scheduled task, therefore, Linux system also provides user-controlled scheduled tasks command: crontab command
One, crond Profile
crond under Linux is used to periodically perform some task or wait for a daemon process certain events, and scheduled tasks under windows similar to the operating system when the installation is complete, the service tool is installed by default and will automatically start crond process, crond process periodically checks every minute if there are tasks to be performed, if there are tasks to be performed automatically perform this task.
Task scheduling under Linux is divided into two categories, task scheduling system and user tasks scheduling.
Task scheduling system: The system periodically work to be performed, such as writing data to the hard disk cache, log cleaning and the like. There is a crontab file in the / etc directory, this is the task scheduling system configuration files.
/ Etc / crontab file includes the following lines:
[Root @ localhost ~] # cat / etc / crontab
SHELL = / bin / bash
PATH = / sbin: / bin: / usr / sbin: / usr / bin
MAILTO = "" HOME = /
51 * * * * root run-parts /etc/cron.hourly
24 7 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
[Root @ localhost ~] #
The first four lines is used to configure crond task to run an environment variable, first row SHELL variable specifies which system to use shell, here is bash, the second line PATH variable specifies the path to the system to execute a command, the third line MAILTO variable specified the crond task execution information will be sent via e-mail to the root user, if MAILTO variable is empty, then do not send information to the user to perform the task, HOME variable fourth line specifies the host while executing a command or script used table of Contents. Meaning sixth row represents the next nine sub-sections described in detail. There is not much to say.
User task scheduling: Users work to be performed on a regular basis, such as user data backup, regular e-mail reminders. Users can use the tools to customize their own crontab scheduled tasks. All user-defined crontab files are stored in / var / spool / cron directory. Consistent with its file name and user name.
User permissions file:
The document listed users are not allowed to use crontab command
The documents listed in the user allowed to use crontab command
/ Var / spool / cron /
User crontab file stored in the directory of all, with your name
crontab file meanings:
User created crontab file, each line represents a task, each row represents a field setting, its format is divided into six fields, paragraph five is the time setting section, the sixth paragraph is command section to be executed in the following format:
minute hour day month week command
minute: for minutes, can be any integer from 0 to 59.
hour: the hour, can be any integer from 0 to 23 in.
day: the date, can be any integer from 1 to 31.
month: the month, it can be any integer from 1 to 12 between.
week: day of the week, can be any integer from 0 to 7, where 0 or 7 represents Sunday.
command: the command to execute, can be a system command, you can also write their own script files.
In each of the above fields, you can also use the following special characters:
An asterisk (*): On behalf of all possible values, for example, if the month field is an asterisk, said after meeting the other fields are constraints execute the command operation month.
Comma (,): You can use a comma-separated list of values to specify a range, for example, "1,2,5,7,8,9"
The bar (-): You can use an integer between bar represents a range of integers, such as "2-6" means "2,3,4,5,6"
Forward slash (/): You can use the forward slash specified time interval on the frequency of, for example, "0-23 / 2" performed once every two hours. At the same forward slash and asterisk can be used in conjunction with, for example, * / 10, if used in minute field representing performed once every ten minutes.
Two, crond service
yum install crontabs
/ Sbin / service crond start // Start Service
/ Sbin / service crond stop // Close Service
/ Sbin / service crond restart // restart the service
/ Sbin / service crond reload // reload the configuration
See crontab service status:
service crond status
Crontab manually start the service:
service crond start
See if crontab service is set to boot, execute the command:
Join at startup:
chkconfig -level 35 crond on
Three, crontab command Detailed
crontab [-u user] file
crontab [-u user] [-e | -l | -r]
2. Use this command:
By crontab command, we can execute the specified system command or shell script script at regular intervals. Unit time interval can be minutes, hours, days, months or any combination, weeks and above. This command is very co-located or periodic data backup log analysis work.
3. Command parameters:
-u user: to set a user's crontab service, for example, "- u ixdba" setting indicates the user's crontab ixdba service, this parameter generally have root user to run.
file: file is the name of the command file, and said it would file as a crontab task list file and loading crontab. If this file is not specified on the command line, crontab command typed will accept standard input (keyboard) on the command, and they loaded crontab.
-e: crontab edit the contents of a user's files. If you do not specify a user, then edit the current user's crontab file.
-l: Displays the contents of a user's crontab file, if you do not specify a user, then displays the contents of the current user's crontab file.
-r: delete a user's crontab file from the / var / spool / cron directory, if the user is not specified, the default is to delete the current user's crontab file.
-i: When you delete a user's crontab file to the confirmation prompt.
4. Common methods:
1) Create a new crontab file
Before considering submitting a crontab file to cron process, the first thing to do is to set the environment variable EDITOR. According to its cron process to determine which editor to edit the crontab file. 99% of the UNIX and Linux users to use vi, if you are such, then you edit the $ HOME directory profile document, to which this line:
EDITOR = vi; export EDITOR
Then save and exit. May wish to create a file called cron file, where is the user name, for example, davecron. Add the following content in the file.
# (Put your own initials here) echo the date to the console every
# 15minutes between 6pm and 6am
0,15,30,45 18-06 * * * / bin / echo 'date'> / dev / console
Save and exit. Confident in front of five fields separated by spaces.
In the above example, the system once every 5 minutes to the console output once the current time. If the system crashes or hangs, the last time can be displayed at a glance what time the system stopped working. In some systems, represented by tty1 console, the above example can make the appropriate changes according to the actual situation. In order to submit crontab file you just created, you can put the newly created file as a parameter cron command:
$ Crontab davecron
Now that the file has been submitted to the cron process, which will run once every 1 5 minutes.