Ubuntu uses crontab to achieve work scheduling

Source: Internet
Author: User
Tags name database list cron jobs crontab syntax

Cron is one of the most useful tools in a Linux system, and a cron job is a job that is scheduled to execute when a specified time arrives. The most common Automated system management and automatic maintenance work, such as a daily notification of a scheduled backup, or a scheduled cleanup of the/tmp/directory. There are also many Web applications that need to perform timed jobs. Time to eat. Send an e-mail informing you that you can have dinner and inform you that the game has started.common routine work on Linux

1. Rotation of the login file (log rotate):
Linux takes the initiative to record all the information that happens to the system, which is the login file. Because the system will always record the login information, so the login file will be more and more large! We know that large files not only account for capacity but also can cause read and write performance, so timely to move the log data, so that the old data and new data are stored separately, the comparison can effectively record the login information. This is the task of log rotate! This is also the necessary routine task of the system;

2. Log-in file analysis Logwatch tasks:
If the system has software problems, hardware errors, security problems, and so on, most of the error information will be recorded in the log file, so one of the important tasks of the system administrator is to analyze the login file. But you can't manually view the login file through vim and other software, because the data is too complicated! Our CentOS provides a program "Logwatch" to proactively analyze login information, so you will find that your root will always receive a letter titled Logwatch, which is normal! You'd better be able to read the contents of the letter too!

3. Create a Locate database:
In the seventh chapter we talk about the Locate command, we know that the command is through the existing file name database to make the system file name query. Our file name database is placed in the/var/lib/mlocate/. The question is, how does this database automatically escalate? Hey! This is the result of the routine work of the system! The system will take the initiative to carry out UpdateDB Oh!

Creation of the 4.whatis database:
Similar to the locate database, Whatis is also a database, the Whatis is a query command related to the man page, but to use the Whatis command, you must have a Whatis database, and this database is automatically run through the system's routine work scheduling Miles!

5.RPM creation of software login files:
RPM is a mechanism for software management. Because the system may often change the software, including the new installation of software, non-recurrent upgrade, etc., will cause the difference in the software file name. In order to facilitate future tracking, the system also helps us to sort out the records of the file! Sometimes the system also helps to re-build the RPM database by scheduling.

6. Remove the Slow archive:
Some software will produce some slow archives during operation, but when the software is turned off, these slow archives may not be actively removed. Some slow archives have time, if more than a period of time, the slow archive will not be effective, at this time to remove these slow archive is an important work! Otherwise the disk capacity will be consumed. The system uses a routine work schedule to run a command called Tmpwatch to remove these slow archives!

7. Analysis behavior related to network services:
If you have installed similar WWW server Software (a software called Apache), your Linux system will usually proactively analyze the software's login files. At the same time, some of the credentials and authentication network information is outdated, our Linux system will also be very friendly to help you do automatic check!

Cron Job Description1./etc/cron.deny and/etc/cron.allow

For security issues, we can restrict the use of crontab user account!

will be able to use Crontab's account to write to it, if not in this file users can not use crontab;

You will not be able to use Crontab's account to write to it, if not recorded in this file users, you can use the crontab.

2.crontab Syntax
crontab [-u username] [-l|-e|-r] options and parameters:  -u: Only root can do this task, that is, to help other users create/remove crontab work scheduling;-e  : Edit crontab Work Content-L  : Check the work of Crontab-R  : Remove all the crontab's work, and if you want to remove only one item, use-E to edit it. 

(1) Basically all Linux distributions are pre-installed with cron tools by default. Even if Cron is not preinstalled, it is simple to execute a command to install it manually:

[Email protected]:~# apt-get Install cron

(2) Then check the status of the Cron service, which should run in the background by default. If it does not start, you can start the service manually.

[Email protected]:/var/log$ sudo service cron status
Cron start/running, Process 1024
[Email protected]:~# service cron Start

[Email protected]:~# service cron Stop

(3) using crontab

1. A list of cron jobs

List cron jobs scheduled by the current user: [Email protected]:~# crontab–l

If you want to see other users ' cron jobs, you can use the command: [email protected]:~# crontab–l–u username This lists the cron jobs for the specified user.

2. Edit cron Job [email protected]:~# crontab-e

3, remove cron job [email protected]:~# crontab–r or [email protected]:~# Crontab–ir will ask whether to delete

(4) planning tasks with crontab

File content Syntax

The meaning of the field is:

Representative meaning Minutes Hours Date Month Week
Number Range 0-59 0-23 1-31 1-12 0-7

Time-Sharing Week command

2 2 * * mail [email protected]

* (asterisk) indicates acceptance of any time

, (comma) * 3,6 * * * indicates that the position 3 and 6 are OK

-(minus) * 3-6 * * * represents 3-6 of this period of time

/n (slash) that n represents a number, meaning "Every n unit interval", for example, every five minutes, then: */5 * * * * command

Cron has two configuration file types that are used to dispatch automation tasks.

1) System level

Configuration file for the system:/etc/crontab

You just have to edit/etc/crontab this file! There is a need to pay special attention to Oh! That is crontab-e this crontab is actually/usr/bin/crontab this run file, but/etc/crontab is a "plain text file" Oh! You can edit this file as root!

Basically, the minimum detection limit for the cron service is "minutes", so "cron reads the contents of/etc/crontab and/var/spool/cron every minute", so as soon as you finish editing the/etc/crontab file, and After storing it, the cron configuration will automatically run!

Note: Restart required after modification: service crontab restart

/etc/crontab's content
[Email protected] ~]#Cat/etc/crontabshell=/bin/bashWhat kind of shell interface is used by <== Path=/sbin:/bin:/usr/sbin:/usr/bin<== Run File search path Mailto=root <== If there is an additional stdout, send the data to whom by email home=/
         <== default home directory for this shell # run-parts01 * * * * * root run-parts/etc/cron.hourly 
         <== Hourly 4 * * * Root run-parts/etc/c ron.daily <== 4 * * 0 root run-parts/etc/cron.weekly <== every Sunday 4 1 * * Root run-parts/etc/cron.monthly <== per month 1th time-of-day weekly runner identity command string

Ubuntu calls the Run-parts command to run all scripts in four directories at timed intervals.
The script under/etc/cron.hourly is run every hour and runs at 17 ticks per hour.
The scripts under/etc/cron.daily are run once a day and run 6:25 every day.
Scripts under/etc/cron.weekly are run once a week and run at 6:47 on the 7th day of the week.
The script under/etc/cron.monthly is run once a month and runs at 6:52 every month at 1th.
The above execution time can be modified by oneself

2) User-level

The crontab syntax is as follows:

[Email protected]:~# crontab–e

Write the following script



mailto= "[Email protected]"

#This is a comment

* * * * * echo ' test cron job to execute every minute '

: wq! Save and exit
[Email protected]:/var/log$ sudo ls-al/var/spool/cron/crontabs/
Drwx-wx--t 2 root crontab 4096 October 11 18:36.
Drwxr-xr-x 5 root root 4096 October 2 23:10..
-RW-------1 root crontab 1114 October 18:34 Root
-RW-------1 yeqing crontab 1115 October 18:36 yeqing

3. can wake up work tasks during downtime

For example, if you shut down every night and wait until the day to start your Linux host, the default schedule is 4:02am every day, hmm! So no one is doing a routine work on a bunch of systems! What can I do about it? You're going to have to anacron this guy now!

What is Anacron

Anacron is not used to replace crontab, Anacron exists for the purpose of the above mentioned, in the processing of non-24 hours of the Linux system has been started the operation of the crontab! So anacron can not specify when to run a task, but in days or immediately after the start of the Anacron action, he will be to detect the outage period should be carried out but did not perform the crontab task, and run the task once again, Anacron will automatically stop.


As you can see, it's easy to automate tasks with crontab, and it can perform tasks by minute, hour, week, month, and week. In addition to this, Linux has an at command, which is intended to handle only one-time tasks and requires the ATD service to run first.

Secondly, we should pay attention to the problem of environment variables. Sometimes we create a crontab, but this task cannot be executed automatically, but it is not a problem to perform this task manually, which is usually caused by not configuring environment variables in the crontab file. When you define multiple dispatch tasks in a crontab file, you need to make a special note of the settings of the environment variables, because when we perform a task manually, it is in the current shell environment, the program can certainly find the environment variable, and the system automatically executes the task schedule, it will not load any environment variables, therefore, You need to specify all the environment variables that are required for the task to run in the crontab file, so that the system does not have a problem when it executes the Task Scheduler.

Also pay attention to cleaning up the mail logs of the system users. Each task is scheduled to execute, the system will send the task output information in the form of e-mail to the current system users, so the cumulative, log information will be very large, may affect the normal operation of the system, so it is important to redirect each task.

Finally, note that the newly created cron job will not execute immediately, at least 2 minutes. If you restart the Cron service, it will be executed immediately.


Automating jobs with Cron in Ubuntu 14.04

16th chapter, routine work scheduling (crontab)

Ubuntu uses crontab to achieve work scheduling

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.