Use of cron (2)

Source: Internet
Author: User
Tags month name ranges

Understand cron concepts

The cron daemon is a small subsystem composed of utilities and configuration files. cron of a certain style can be found in almost all UNIX-like systems. Cron
Components include the daemon itself, a set of system-wide configuration files, a set of configuration files for specific users, and a utility for adding, modifying, and deleting user configuration files; and a simple access control
Manufacturing facilities. Generally, the list of cron configuration files or cron jobs is called crontab or cron schedule.

The daemon cron runs continuously and checks modifications in the configuration file once every minute. Cron reads the system range and
Crontab (described in the following two sections respectively), update the event scheduling plan accordingly, and execute all the commands that should be executed within one minute. This daemon also captures the output of each job (if any)
And send the results to the job owner by email.

You can define system-related jobs in three locations:/etc/crontab, any files in/etc/cron. d, and special directories.
/Etc/cron. Hourly,/etc/cron. daily,/etc/cron. weekly, and/etc/cron. Monthly:

The main system crontab is/etc/crontab. This file has a unique syntax (discussed in the next section), where each job defined runs as a specified user based on its own schedule (for example, twice an hour or once a day. Use/etc/crontab to schedule various management and maintenance tasks.

You can also maintain a group of crontabs in the/etc/cron. d directory. By creating
Crontab: logically groups commands belonging to a subsystem. For example, in the PHP 5 Programming Language package, install a package named PhP5 in/etc/cron. d.
Crontab, which regularly clears unused sessions. Files in/etc/cron. d use the/etc/crontab
With the same syntax, each job runs according to its own schedule and as a specific user.

You can also directly place the shell script in
/Etc/cron. Hourly,/etc/cron. daily,/etc/cron. Weekly, or/etc/cron. Monthly
Directory to run the script once every hour, every day, every week, or every month. Run the script here as a Super User.

The crontab set for users is usually stored in/var/spool/cron/crontabs. (For the specific location, see your UNIX
System documentation. Some systems put the user crontab in/usr/lib ). However, instead of directly editing files in this directory, you can use crontab
Create a crontab and submit a file. I will discuss how to manage my crontab later.

Finally, you can use the access control file/etc/cron. allow and/etc/cron. deny to allow or deny user access to cron. For example, if a user's job may damage the normal operation of the system, he can be denied access to cron.

As you can see, you do not need to sacrifice your rest time to keep the system running continuously. You only need to determine the job, define its scheduling plan, and set the job in the appropriate crontab, and then rest assured. Now let's take a look at the special syntax of the cron file.

Process crontab

Crontab is just a text file and can be edited in any UNIX editor. It can contain four types of code lines: Empty lines, comments, environment variable settings, and commands.

Empty rows and comments

Empty lines and extra spaces in the file are ignored. Empty rows and spaces help improve the readability of crontab and organize crontab in an orderly manner.

You can also use annotations to describe the schedule and purpose of each job. To create a comment, you only need to add a pound sign (#) at the beginning of a line (#).

Environment Variables and commands

Cron finally uses a shell to execute each command. You can modify or customize shell behaviors through environment variables.

It is easy to set shell environment variables in crontab. You only need to enter variable = value, replace variable with the variable name, and replace value with a value. For example, row crontab:

PATH=/usr/bin:/bin:/usr/local/bin

Specify an ordered directory list as the shell search path.

Cron predefines five environment variables:

The default value of path is/usr/bin:/bin.

Pre-set shell to/bin/sh.

LOGNAME is the username of the crontab owner.

Home is set to the Home Directory of the crontab owner, such as/home/Joe.

Set mailto to the name of the crontab owner.

To modify these default values or set any variables, you only need to set the appropriate environment variables in crontab.

Of course, crontab can contain any number of command lines. Each Command Line specifies a frequency, a user name (for system crontab only), and a task to be run. For example, the command:

5 0 * * *  root  find /tmp -type f -empty -delete

Delete all empty files and directories (find/tmp-type F-empty-delete) in/tmp at A.M (5 0 ***) every day ). This job is run as the root user.

The crontab command must specify a user name as the user used to run the task. (Therefore, in/etc/crontab
You may see the above command ). The user name cannot be specified for a user's crontab; a user's cron command is always run as this user. Whether the user name is a system
The only difference between crontab and user crontab.

The following describes how to customize a timetable.

Custom timetable

The cron schedule allows you to run jobs in multiple ways, such as every minute or at a specific time of a specific date. The scheduling parameters are flexible.

Cron Field

You can adjust the frequency by using five fields: minute, hour, month, China, Japan, month, and week date (such as Monday and Tuesday ). Table 1 summarizes how to adjust each field.

Table 1. scheduling options of cron jobs

 

Location Field Value Description
1 Minute 0-59  
2 Hours 0-23  
3 China and Japan 1-31 Unlike minutes and hours, China and Japan do not start from scratch.
4 Month 1-12 The month does not start from scratch. You can also use the first three letters of the month name, such as Jan or may, instead of a number ranging from 1 to 12.
5 Zhou Sino-Japanese 0-7 Both 0 and 7 represent Sunday. You can also use the first three letters of the name, such as Mon or wed.

In addition to names or numbers, you can also use an asterisk (*) to indicate "all ". For example, an asterisk in the minute represents every minute of the day. (In some cases, such a high frequency is required, but be careful that the tasks executed at this frequency should be very simple and will not run for a long time ).

You can also use the Value List, range, and step (increment) to specify multiple values, continuous value ranges, and discontinuous value ranges. You can even combine the list and range. The list is a comma-separated value set. The range consists of the start value, end value (inclusive), and optional step values.

Let's look at some examples. Each row in table 2 contains a time table and its description. When the minute, hour, and date fields match the current time, cron executes the command. If the day of the month and day of the week are restricted (that is, not *), then, when at least one of the two fields matches the current time, cron will also execute the command.

Table 2. Example of a Cron Job Schedule

 

Scheduling plan  
Minute Hours China and Japan Month Mid-week date Description
0 1 15 1, 3, 5, 7, 9, 11 * Run the command at 1 a.m. On October 1, 3, 5, 7, 9, and October 15. For easier understanding, you can also write the time table as 0 1 15 Jan, MAR, May, Jul, SEP, Nov *. Do not add spaces after the comma (,) when specifying a list.
0-59/15 * * * * The scheduling plan runs the command every 15 minutes.
30 * * * Wed, Fri This schedule only executes commands once every 30 hours on Wednesday and Friday. (The names of days and months can be used in the list, but not in the range ).
0, 30 0-5, 17-23 * * * Run the command from midnight to 5 a.m. and 7 p.m. to 11 p.m. On the hour and at 30 o'clock.
0 0 1 1 * Execute the command at midnight on January 1, January 1 every year.
0 0 * * 0 Run the command at midnight every Sunday. This is equivalent to once a week.
30 0 10, 20, 30 * 6 Because the day of the month and the day of the week are limited, this schedule runs the command at a.m. On the day 10, 20, and 30 of each week (except February.

As you can see, you can use these five parameters to specify any scheduling plan. To make it easier, vixie cron also provides a simplified form of common scheduling plans. Table 3 lists some abbreviations.

Table 3. Abbreviations of common scheduling plans

 

Abbreviated form Description
@ Reboot Run commands whenever the computer restarts.
@ Daily The abbreviated form once a day.
@ Weekly Weekly abbreviation.
@ Annually The abbreviated form once a year. You can also write it as @ yearly.
@ Midnight Run the command at midnight every day. This abbreviation is equivalent to @ daily.

If you prefer to use abbreviations, you only need to use them to replace the first five fields of the cron command. The following command looks much simpler.

@daily root /usr/local/scripts/clean_old_files.sh

Crontab command example

After understanding the basic concepts, let's take a look at some examples of the crontab command. The same command can also be used in the system scope: you only need to specify a user name after the weekly and Sino-Japanese fields (the fifth field) in all system crontab items.

Create a personal crontab

To create a personal crontab, you can use any text editor to create a file. By convention, the personal crontab file is saved in ~ /. Crontab, but any file name can be used.

PATH=/usr/bin:/bin:/usr/local/bin
#
# Every day, print and delete all temporary files whose names begin with '.#'
@daily   find $HOME -type f -name '.#*' -print -delete
  
#
# Every week, show me what is consuming space in my home directory
@weekly   du -sh $HOME

Submit a personal crontab using the crontab Utility

When editing a file (such ~ /Mycrontab), submit it to cron through the crontab utility:

% crontab ~/mycrontab

View information stored in cron

To view information stored in cron, enter crontab-l:

% crontab -l
PATH=/usr/bin:/bin:/usr/local/bin
  
#
# Every day, print and delete all temporary files whose names begin with '.#'
@daily   find $HOME -type f -name '.#*' -print -delete
#
# Every week, show me what is consuming space in my home directory
@weekly   du -sh $HOME

Replace crontab

You can use the crontab utility to replace your crontab at any time. You only need to submit a new file or a revision of the same file. To delete a crontab job, enter crontab-R:

% whoami
joe
% crontab ~/mycrontab
% crontab -l
PATH=/usr/bin:/bin:/usr/local/bin
...
% crontab -r
% crontab -l
crontab: no crontab for joe

Replacing the cron Mechanism

Although cron is useful, you should also understand the two mechanisms that can replace it.

Anacron

If the system is often shut down or sleep (for example, if a Unix laptop is used), consider adding
Anacron. Anacron is similar to cron in that it also schedules jobs to run later. However, it is similar to cron.
Different, even if the scheduled running time of the job has passed, anacron runs the job.

For example, if the file system backup is scheduled to run on Saturday but the system is shut down from Friday to Monday, anacron will immediately run the job on Saturday when the system restarts on Monday. In contrast, cron only checks whether a job should be run now. Therefore, if the system is disabled at the scheduled running time of the job, the job will not be run.

Anacron has fewer scheduling options than cron. It can only schedule jobs at full-day intervals, such as one, seven, or 30 days. However, it is a better choice for jobs that must run frequently and reliably.

In addition, you must start anacron from cron. Every time anacron
When running, it reads its own configuration file. The configuration file contains a configuration pair consisting of a job and its frequency (expressed in days. Anacron
Run the job and record the time of running the job. After running all the jobs, anacron exits.

Anacron can be found in most Linux distributions, but it is also easy to download and build the source code by yourself. Visit the anacron project page to obtain the latest version.

The main configuration file of anacron can be found in/etc/anacron. You can set the environment variables as you did when configuring Cron, but it is simpler:

SHELL=/bin/zsh
PATH=/usr/bin:/bin:/usr/local/bin
# format: frequency delay name job
1 10 day-to-day daily.chores.sh

The first number is a cycle, so 1 indicates running once a day, 7 indicates running once every 7 days, and so on. The second number is latency, that is, from anacron
The number of minutes to wait until the job is executed. If the latency field is set to a different value, it can prevent all jobs from starting at the same time. Name: day-to-day
It's just a helpful nickname. Specify the job for the remaining part of the configuration line. Here, the shell script daily. Chores. Sh found in the specified path is run once every day.

Anacron provides excellent documentation in the form of a manual page, you can also find tips about anacron on the Internet. (Please refer to my 2007
The Linux magazine article by Rod Smith, edited monthly ). Anacron is suitable for Unix
Fans and system administrators who require additional protection.

Launchd: Modern substitution mechanism of Cron

Cron is indeed a powerful and trustworthy utility. Its popularity proves this. Vixie cron has been improved recently. For example, the abbreviated @ reboot method has been added to further simplify management. However, cron still has some disadvantages:

Although cron jobs can be defined in the crontab file, cron jobs cannot be started or stopped from the command line. In addition, you cannot create a dedicated job on the command line and submit it to the calendar.

Cron does not implement resource restrictions. If a job runs as the root user, it can consume countless processor time and memory. In practice, you may want to restrict one job to avoid affecting the overall quality of other cron jobs and system operations.

Cron jobs are strictly associated with a scheduling plan. For example, you cannot start a job only when an event occurs (such as creating a file.

In a larger scope, many core components of Unix-like systems can start other programs as needed, including Cron, xinetd for network daemon (or
Inetd) and init (the origin of all system processes ). Each core component has its own configuration file, so it is difficult to know which component is most suitable for completing a modification.

To overcome these disadvantages, Apple Computer has developed a unified startup tool.
Launchd, which can start processes at boot, as needed, and at specified intervals. In fact, launchd in Mac OS 10.4 Tiger
Cron has been replaced (and init and several other system utilities used to boot and initialize the system ). (Apple retains
Cron, which is convenient for users and more flexible scheduling options for vixie cron ). Mac OS X boot speed can be attributed
Launchd: it lists the programs to be started at boot, but executes the programs only when needed for the first time.

Launchd is an open source code that can be obtained from its home page on Mac OS forge. Currently, launchd has been transplanted
FreeBSD, but has not been transplanted to other UNIX or Linux systems. However, many projects are actively implementing
Equivalent program, so it is necessary to briefly understand its features:

Launchd
Instead of creating a job to check whether there are new files in the directory, it automatically monitors whether there are new files in the directory or whether any files are added to the empty directory, and starts the job as needed. Launchd
Instead of polling, the kqueues facility is used to notify the kernel when the directory changes. (Linux has a similar event facility inotify, a later article
It will be discussed in the developerworks article ).

If this parameter is specified, launchd uses chroot to send your job to a new directory. Chroot reading
"Cha-root" is a system call that can change the forward slash (/) and the Directory pointed to by the root directory. Therefore, if you use chroot to send the file
/Opt/root, all files except/opt/root are inaccessible (after all,/opt/root is
/, That is, the top-level directory of the file system). All directories in/opt/root become level-1 directories. Chroot is usually used.
Restrict jobs and prevent code from entering a larger file system to avoid damages.

You can set resource limits for jobs. Resources that can be restricted include memory, stack size, and maximum number of opened files.

After defining a task and loading it into launchd, you can start and stop the job from the command line by name.

Launchd consists of three components: the launchd daemon itself, which is used to add, modify, and delete jobs and control
Launchctl utility; one or more configuration files, each defining one or more jobs. Because it originated from Mac OS X and launchd
The configuration file is only a simple property file, which can be expressed in Extensible Markup Language (XML.

To put it simply, follow these steps to use launchd on Mac OS X (to monitor the files added to a directory and run jobs as needed ):

Create an attribute file to express this job and all its attributes.

You can use the Mac property editor or manually edit the XML file. Whatever method is used, the generated file should be similar to listing 1.

Listing 1. Examples of launchd jobs that monitor file system directory changes

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST
      1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.example.processor</string>
  <key>OnDemand</key>
  <true/>
  <key>Program</key>
  <string>/Users/strike/bin/processor</string>
  <key>ProgramArguments</key>
  <array>
    <string>processor</string>
  </array>
  <key>WatchPaths</key>
  <array>
    <string>/Users/strike/data/incoming</string>
  </array>
</dict>
</plist>

To put it simply, when the content in the/users/strike/data/incoming directory changes, this file runs
The utility script in/users/strike/bin/processor. Set OnDemand to true to enable launchd
Start the job as needed. Save this file ~ /Library/launchagents/COM. example. processor. plist.

Use launchctl to load the job to launchd:% launchctl load ~/Library/LaunchAgents/com.example.processor.plist

If you want to check the latest operations or view the list of saved jobs, you only need to enter the launchctl list.

To delete a job, you only need to use launchctl with the unload option:% launchctl unload -w ~/Library/LaunchAgents/com.example.processor.plist

What is the role of-W? It completely deletes the jobs in launchd. Without this option, the job will be automatically reloaded upon Logon (because the job is still in the user set where the proxy is started ).

The launchd manual page contains a large amount of information. If you are a Mac OS X user, you can also find a large number of applications for launchd. Some smart developers are migrating launchd to a wider platform.

Use cron to make work easier

Learn tips and tips on using cron and learn why this daemon and similar programs are your best friends.

Tips and tips

The following are some tips, tips, and FAQs about cron:

Unlike shell or shell scripts used in terminal windows, cron does not expand environment variables in the crontab file. In other words, if you enter:HOME=/home/joe
PATH=$HOME/bin:/usr/bin:/bin

The path is not set as the expected path. You must expand all variables manually, for example:

HOME=/home/joe
PATH=/home/joe/bin:/usr/bin:/bin

However, because each cron command is executed by shell, the command can reference the variable name. For example, if you write the following command in personal crontab (note that the username parameter is omitted in this line ):

@daily $HOME/bin/cleanup_daily.sh

Then $ home will be correctly expanded.

Do not schedule computing-intensive tasks to start at the same time, such as @ midnight. If possible, start these tasks separately in the early hours to avoid resource contention.

As mentioned above, by default, the environment variable shell is set to/bin/sh. If you do not modify this variable, all the commands in crontab are
/Bin/sh. However, if you are not familiar with/bin/sh and prefer another shell, you can set the shell and use this shell.
Command syntax.

For example, if shell =/bin/zsh is set, all commands can use the Z shell function, such as its advanced redirection OPERATOR:

SHELL=/bin/zsh
@daily uptime > daily >> weekly

Here, the output of the uptime command overwrites the daily file (> daily) and appends it to the weekly file (> weekly ).

Use the access control list (ACL)-/etc/cron. allow and/etc/cron. deny-to allow or deny specific user running
Cron job. If you want to restrict access to cron to a few users
List the user names of these users. Unspecified users cannot use the crontab utility to submit crontab. However, if you want to allow most people to access Cron
If a few users are rejected, the Restricted Users are listed in/etc/cron. deny.

For example, If/etc/cron. allow has the following content:

joe
zelda

No user except Joe and Zelda can access cron:

% whoami
strike
% crontab ~/.crontab
You (strike) are not allowed to use this program (crontab)
See crontab(1) for more information

To disable email reports sent by Cron, set mailto = "" in crontab "".

Again, do not use spaces in the list. List values are separated by commas. In vixie Cron, the names of days and months are not used in the range.

Read the cron documents carefully. The path, features, and simple measures may be different. Enter man 5 crontab on the command line to learn about crontab.
File Syntax. Enter MAN 1 crontab to learn about the crontab utility. Enter man cron or man 8 cron on the command line to learn more.
Cron Daemon options.

Best Friend of the system administrator

Cron and similar programs are very helpful to system administrators. If you need to execute the same task repeatedly, you can use cron for automation. Shell scripts are often required to capture complex tasks with many steps, but many tasks only need one line of command.

The following are some ideas:

Create daily dump by combining cron and database tools. For example, the command:@daily joe mysqldump -pjoespwd accounts > $HOME/backups/accounts.`date +%F`.sql

Dump the database accounts to a file every day. The embedded date command ('date + % F') ensures that the file name is unique, for example
Accounts.2008-08-07. SQL. This command is run as user Joe, so use-P to specify the password of Joe. This command can also be placed on Joe
In his crontab, because the dump requires his MySQL credential.

The locate subsystem indexes all files in the system and stores the full path of each file in the database. Then, query the database from the command line to find the file immediately. Of course, you can use find to search for a file as needed, but you must wait for it to search for a file system, which may be slow.

In order for the location subsystem to play a role, the file system must be indexed on a regular basis because files may be added or deleted at any time. Cron is ideal for this scenario.

0 0,12 * * * root updatedb

This crontab item runs updatedb (locate update utility) twice a day.

Another task that is suitable for automation with cron is to copy files from the master server to a large number of slave servers. Rsync is a modern utility that distributes and synchronizes files across multiple systems. Many network administrators use cron and rsync to copy the master copy of the website to each server in the server group.@midnight www rsync -avz /var/www/site slave1:/var/www

At midnight every day, rsync copies/var/www/site (-avz) to/var/WWW on slave1.

Use the command line utility mail and shell pipeline operator (|) to send the task output to one or more Members in the team.

@weekly root df --print-type --local -h |& mail -s "Weekly df report" andy bob

Here, the DF output is sent to users Andy and Bob by email every week, so that they can monitor disk usage.

Conclusion

Tutorial conclusion

Whether you use Unix
The system also manages the systems of hundreds of users. Automatic maintenance tasks help save time, reduce errors, and keep the machine running continuously. Cron is in UNIX
The key components for automation in the system can be used by computers to work for you, rather than waiting for them.

Cron helps you complete your work more easily. Now you can have a good sleep.

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.