One Linux command per day (crontab) command

Source: Internet
Author: User

The crontab command is used to submit and manage tasks that the user needs to perform periodically, similar to the Scheduled tasks under Windows.

(1) Usage:

Usage: crontab [-u user] File

crontab [-u user] [-e |-l |-r]

(2) Function:

Features: when the operating system is installed, the Service tool is installed by default, and the Crond process is automatically started, and the Crond process periodically checks to see if there are any tasks to perform, and automatically executes the task if there are tasks to perform.

Executes the specified system instruction or shell script scripts at a fixed interval of time. The units of the time interval can be any combination of minutes, hours, days, months, weeks, and more. This command is very useful for periodic log analysis or data backup.

(3) Option parameters:

1)-l list the current timer settings to see the program running at timed

2)-e Edit the user's timer settings, set the timer

3)-R Delete the user's timer settings

4)-u< User name > Specify the user name to set the timer

(4) Example: (This command is not used very often, in this brief introduction)

1) Execute command every 1 minutes

* * * * * command

2) Execute command every five minutes

*/5 * * * * command

3) Execute this command at 17:00 per day

xx  * *  command

4) Execute the command every three hours at the time of the 8:02,11:02,14:02,17:02,20:02, at 8 to 20 .

2 8-3 * * * command

Several uses of Crondtab will be discussed later:

* Create a new crontab file;

* List crontab files;

* Edit Crontab file;

* Delete crontab files;

Recover lost crontab files;

(5) Other:

1. What is the scheduled task for Windows?

In Windows, the system has an important "scheduled task" feature, by setting up "Scheduled Tasks", you can plan for what you need to do at a certain time of day or day, to the agreed time, no matter what work you do on your computer, the system will alert you or start the task program you set up.

Introduction to 2.crontab:

The crontab command is commonly used in Unix-and Unix-like operating systems to set instructions that are executed periodically. The command reads the instruction from the standard input device and stores it in a "crontab" file for later reading and execution. The word derives from the Greek language Chronos (χρνο), which was originally meant to be time. Often, crontab stored instructions are activated by the daemon, Crond often run in the background, and every minute checks whether a scheduled job needs to be performed. This type of work is generally called cron jobs.

3.crontab file:

The crontab file contains a series of jobs and instructions sent to the cron daemon. Each user can have their own crontab file, while the operating system holds a crontab file for the entire system, which is usually stored in subdirectories under/etc or/etc, and this file can only be modified by the system administrator. Each line of the crontab file follows a specific format, separated by a space or tab into several realms, each of which can place a single or multiple numeric values.

task scheduling under 4.Linux

The task scheduling under Linux is divided into two categories: System task scheduling and user task scheduling.

System task scheduling: The work to be performed by the system periodically, such as writing cache data to hard disk, log cleanup, etc. In the/etc directory there is a crontab file, this is the System Task Scheduler configuration file:

[Email protected]/]# cat/etc/Crontabshell=/bin/Bashpath=/sbin:/bin:/usr/sbin:/usr/Binmailto=root# for details see Mans4crontabs# Example of Job definition:#.----------------Minute (0- -)# | .-------------Hour (0- at)# |  | .----------Day of Month (1- to)# |  |  | .-------Month (1- A) OR jan,feb,mar,apr ... #|  |  |  | .----Day of Week (0-6) (sunday=0Or7) OR sun,mon,tue,wed,thu,fri,sat#|  |  |  | |# * * * * * * user-name command to be executed

The first three rows are the environment variables that are used to configure the Crond task to run. The first line of the shell variable specifies which shell the system will use, this is bash, and the second line of the path variable specifies the path to the system execution command, and the third line mailto variable specifies that Crond's task execution information will be emailed to the root user. If the value of the mailto variable is null, it means that the task execution information is not sent to the user.

*: Indicates any moment   1-5/N: Indicates that each n unit executes once, as in the second paragraph, */1, which indicates that the command is executed every 1 hours. Can also be written as1-1.

User Task scheduling: Users to perform regular work, such as user data backup, scheduled email reminders and so on. Users can use the Crontab tool to customize their own scheduled tasks. All user-defined crontab files are saved in the/var/spool/cron directory. Its file name is the same as the user name.

[[Email protected]/]# CD/var/spool/0
/etc/cron.deny  

/var/spool/cron/ directory where all users crontab files are stored, named after the user name

5.crond Service

1) Install Crontab:

Yum Install Crontabs

Service Operation Instructions:

// Start the service // Close Service // Restart Service // Reload Configuration

View crontab Service Status: Service Crond Status

[email protected] sunjimeng]# service Crond statusredirecting to/bin/systemctl Status Crond.servicecrond.service-Command Scheduler loaded:loaded (/usr/lib/systemd/system/crond.service; enabled) Active:active (running) since five .- to- -  at: One: -PDT;1Weeks5Days ago Main PID:1303(Crond) CGroup:/system.slice/crond.service└─1303/usr/sbin/crond-N5 Month -  at: One: -Localhost.localdomain systemd[1]: Started Command scheduler.5 Month -  at: One: -Localhost.localdomain crond[1303]: (CRON) INFO (Random_delay ... May -  at: One: -Localhost.localdomain crond[1303]: (CRON) INFO (Running with ... Hint:some lines were ellipsized, use-L to showinchFull.

To start the crontab service manually: Service Crond Start

/bin/systemctl Start  Crond.service

To see if the crontab service is set to boot, execute the command: Ntsysv

[Email protected] sunjimeng]# NTSYSV

After entering the above command, CentOS pops up the following page:

Add to boot auto start: Chkconfig–level Crond On (introduction later)

One Linux command per day (crontab) command

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.