The format and essentials of Crontab

Source: Internet
Author: User

Crond is a scheduled task resident program in Linux that checks the job list every minute to automatically run the specified job at a specified time, which is essential for system operations.

usually we use the CRONTAB program to set up and manage the job, and the specific start-up execution is Crond Deamon. Here's how to use Crontab to set the system to run specific tasks at the time you specify.

Step Description:

1. Write the shell script for the job you want to execute.

2. The MoD that uses chmod to modify the shell script is executable.

3. Add the script to the Crontab configuration file and specify when it will start running.

1. Writing shell scripts

The method of writing shell script is not the main content of this article, see: Shell scripting method; shell common condition judgment and condition test; Shell data passing method.

Now assume that you have correctly written a shell script named myshellscript.sh in your personal directory.

2. Use chmod to modify the shell script MoD as executable

Use the following instructions to modify the MoD:

chmod

In special cases, if you use a command in your shell script that requires root-related permissions, you need to modify the corresponding file mod so that its run time is automatically promoted to root.     

chmod +s xxxxx;

3. Add the script to the Crontab configuration file and specify when it will start running.

The usual usage of the crontab command is:

Crontab-e #会打开编辑器, loads the job settings file for editing, adding, and deleting timed jobs.

Usually we use crontab-l to view the current configuration and then use CRONTAB-E to start the editor and add the tasks we need.

Configuring Crontab Timed jobs must contain two types of information: A time setting, a target program.

The format of its file is:

# Format Description
#--min (0-59)
# | --Hour (0-23)
# | | --Day (1-31)
# | | | --month (1-12)
# | | | | --Week (0-7) (Sunday =0 or 7)
# | | | | |
# * * * * * Command executed

As can be seen above, five *, a jobname, respectively, to define the time frequency and job name.

Time designation Detailed design
The Crontab profile supports filling in multiple values in any one time item in the "Timeshare Week":

1. Comma (', ') separated values, for example: "1,3,4,7,8"
2. The conjunctions ('-') set the range of values, for example: "1-6", meaning equal to "1,2,3,4,5,6"
3. The asterisk (' * ') represents any possible value. For example, asterisks in the "hour field" are "every one hours," and so on.
4. The extended version of some cron programs also supports the slash ('/') operator, which is used to indicate skipping some given number. For example, "*/3" in the Hour field equals "0,3,6,9,12,15,18,21" and so on is divisible by 3 number;

For example:

*/5 1,2,3-9/3 1 1 * touch ' date ' +%y%m%d "'

The above settings indicate that the user wants to be in the January 1 1 o'clock, 2 points, and 3 to 9 can be divisible by 3 points, run every 5 minutes.

Note The main points:

The third and fifth are defined in the case that the two are or the relationship, such as:

 - 1 1-740 /root/shift_my_times. SH

This procedure will be implemented from April 1 to 7th and every Sunday for the remainder of April.

The final explanation:

Advanced usage:

Cron has eight special symbols, the use of special symbols not only save time, but also more intuitive, the following is the symbol description:

Special Symbolic meanings
Run Once @reboot reboot
@yearly Run once a year, equivalent to: "0 0 1 1 *".
@annually mean like @yearly.
@monthly Run once a month, equivalent to: "0 0 1 * *".
@weekly Run once a week, equivalent to "0 0 * 0".
@daily Run once a day, equivalent to "0 0 * * *".
@midnight the same as @daily)
@hourly run hourly, equivalent to "0 * * * *".

Demo Example: Run/root/mkdir.sh every hour:

@hourly/root/mkdir. SH

/etc/crontab and/etc/cron.d/* configuration files

/etc/crontab is the system crontab configuration file, usually only by the root user and daemon to set the System class cron task, all Linux users must use Crontab to create, edit cron task. /var/spool/cron and/var/cron/tabs are crontab profiles for ordinary users.

Default cron configuration file/etc/crontab

General/etc/crontab File Sample:

 shell=/bin/bash PATH  =/sbin:/bin:/usr/sbin:/usr/ binmailto  =# run -parts  01  * * * * root Run-parts/etc/cron.hourly  4  * * * root run-parts/etc/cron.daily  22< /span> 4  * * 0  root Run-parts/etc/cron.weekly  42  4  1  * * Root run-parts/etc/cron.monthly 

As can be seen, crontab has been pre-defined for a specific period of the script storage path, if the per-hour, daily, weekly or monthly run, can not be configured, just put the script directly into the specified folder, and then chmod +x filename, you can. The following is a directory description:

directory                  Description /etc/cron.d/          All scripts in this directory are invoked by setting Crontab to invoke/etc/cron.daily/to      Run all scripts in that directory once a day /etc/cron.hourly/     runs all scripts in this directory every hour /etc/cron.monthly/    All scripts in that directory every month /etc/ cron.weekly/    Run all scripts in this directory once a week

Reference Links:

1. Http://zh.wikipedia.org/wiki/Cron

2. Http://www.zhetenger.com/cron%E9%AB%98%E7%BA%A7%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B

3.

The format and essentials of Crontab

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.