How to install Crontab in CentOS

Source: Internet
Author: User

How to install Crontab in CentOS

Crontab commands are common in Unix and Linux operating systems and are used to set periodically executed commands. This command reads commands from the standard input device and stores them in the crontab file for later reading and execution. Generally, commands stored in crontab are activated by the daemon. Crond is often run in the background and checks every minute Whether a scheduled job needs to be executed. This type of job is generally called cron jobs.

I. Installation

[Root @ CentOS ~] # Yum-y install vixie-cron
[Root @ CentOS ~] # Yum-y install crontabs

Note:
Vixie-cron is the main program of cron;
The crontabs package is used to install, uninstall, or list the tables used to drive the cron daemon.

Ii. Configuration

Cron is a built-in service of linux, but it does not automatically get up. You can start or close this service using the following methods:
Service crond start // start the service
Service crond stop // close the service
Service crond restart // restart the service
Service crond reload // reload the configuration
Service crond status // view the crontab service status

Add boot auto start to CentOS: chkconfig -- level 345 crond on

The main configuration file of cron is/etc/crontab, which includes the following lines:

SHELL =/bin/bash
PATH =/sbin:/bin:/usr/sbin:/usr/bin
MAILTO = root
HOME =/

# Run-parts
01 *** root run-parts/etc/cron. hourly
02 4 *** root run-parts/etc/cron. daily
22 4 ** 0 root run-parts/etc/cron. weekly
42 4 1 ** root run-parts/etc/cron. monthly

The first four rows are variables used to configure the running environment of the cron task.
The SHELL variable value tells the system which shell environment to use (bash shell in this example );
The PATH variable defines the PATH used to execute commands.
The cron task output is mailed to the username defined by the MAILTO variable.
If the MAILTO variable is defined as a blank string (MAILTO = ""), the email will not be sent.
The HOME variable can be used to set the HOME directory used for executing commands or scripts.

Restrict the use of cron:

The/etc/cron. allow and/etc/cron. deny files are used to restrict the use of cron.
The two control files are in the format of one user per line.
Spaces are not allowed for both files.
If the control file is modified, the cron daemon (crond) does not have to be restarted.
The control file is read every time you add or delete a cron task.

Regardless of the rules in the control file, root can always use cron.

If the cron. allow file exists, only the listed users are allowed to use cron, And the cron. deny file is ignored.
If the cron. allow file does not exist, all users listed in cron. deny are forbidden to use cron.


Iii. crontab command

Function: sets a timer.

Syntax: crontab [-u <User Name>] [configuration file] or crontab [-u <User Name>] [-elr]
Explanation: cron is a resident service that provides the timer function, allowing users to execute preset commands or programs at specific times. You can use the timer function as long as you edit the timer configuration file. The configuration file format is as follows: Minute Hour Day Month DayOFWeek Command

Parameters:
-E: edit the timer settings of the user.
-L list the timer settings of this user.
-R: Delete the timer settings of this user.
-U <User Name> specifies the user name to set the timer.

Format:
* *** Command
Hour, day, month, and week commands

The 1st column indicates minute 1 ~ 59. Each minute is represented by * or */1.
The first column indicates the hour 1 ~ 23 (0 indicates 0 points)
The 3rd column indicates the date 1 ~ 31
The 4th column indicates the month 1 ~ 12
The Identification Number of column 5th is from day of the week to day ~ 6 (0 indicates Sunday)
6th columns of commands to run

Example:

30 21 ***/usr/local/etc/rc. d/lighttpd restart
The preceding example indicates restarting apache at every night.

45 4, 10, 22 **/usr/local/etc/rc. d/lighttpd restart
The preceding example indicates that apache is restarted at on the 1st, 10th, and 22th every month.

10 1 ** 6, 0/usr/local/etc/rc. d/lighttpd restart
The preceding example indicates that apache is restarted at every Saturday and Sunday.

0, 30 18-23 ***/usr/local/etc/rc. d/lighttpd restart
The preceding example indicates that apache is restarted every 30 minutes between and every day.

0 23 ** 6/usr/local/etc/rc. d/lighttpd restart
The preceding example indicates that apache is restarted at every Saturday.

**/1 ***/usr/local/etc/rc. d/lighttpd restart
Restart apache every hour

* 23-7/1 ***/usr/local/etc/rc. d/lighttpd restart
Restart apache every hour between PM and PM.

0 11 4 * mon-wed/usr/local/etc/rc. d/lighttpd restart
Restart apache on November 4 and every Monday to Wednesday.

0 4 1 jan */usr/local/etc/rc. d/lighttpd restart
Restart apache at on January 1, January 1

*/30 */usr/sbin/ntpdate 210.72.145.44
Synchronization time every half hour

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.