A detailed explanation of the usage of CRON commands in Linux

Source: Internet
Author: User
Tags current time memory usage ssh root directory

There is a command in Linux that can perform system tasks on a regular basis. This is the Crond service. The following is an introduction to the use of the crontab command.

Linux task scheduling is mainly divided into the following two categories:

Edit/etc/crontab File Configuration cron

The cron service does not only have to read all the files in the/var/spool/cron every minute, but also read the/etc/crontab, so we can configure the file to do something with the cron service. The crontab configuration is for a user, and editing/etc/crontab is a task for the system. The file format for this file is:

1, the system implementation of the work: the system periodically to perform the work, such as backup system data, clean cache

2, Personal work: a user to do the work on a regular basis, such as every 10 minutes to check the mail server for new letters, these work can be set by each user

Crontab is a timed task trigger under a UNIX system whose user permissions are recorded in the following two files:

File meaning

/etc/cron.deny users listed in this file are not allowed to use the crontab command

/etc/cron.allow users listed in this file are allowed to use the crontab command

/var/spool/cron/is a crontab file for all users

The format of the crontab command is: crontab–l|-r|-e|-i [username], whose parameter meaning is as table one:

Example of parameter name meaning

-L Displays the contents of the user's crontab file crontab–l

-I prompts Crontab-ri before deleting a user's crontab file

-R Deletes the user's crontab file from the crontab directory Crontab-r

-e Edit user's crontab file crontab-e

The crontab file created by the user is stored in/var/spool/cron and has the same filename as the user name.

Its format is divided into six paragraphs, the first five paragraphs for the time set paragraph, the sixth paragraph is the command segment to be executed,

The format is as follows: * * * * * *

The meaning of its time period is as table two:

Scope of meaning of paragraph

The first paragraph represents the minute 0-59

The second paragraph represents the hour 0-23

Representative of the third paragraph date 1-31

The fourth paragraph represents the month 1-12

The fifth paragraph represents the day of the week, 0 represents Sunday 0-6

Example: If the contents of the user's crontab file are: * * * * echo its dinner time, the system's 19:29 per day displays ' its dinner time '

Example (create a cron full process that will enter the current time in test.txt every minute):

1, to the ordinary user login Linux system (I use CentOS4.1)

2,

The code is as follows:

$crontab –e

Description: The system Default editor is vim, if not please add the following shell:

The code is as follows:

$EDITOR =vi

$export EDITOR

3, input

The code is as follows:

*/1 * * * Date >> $HOME/test.txt ", Save and exit VIM

4,

The code is as follows:

$SU Root

5,

The code is as follows:

$CD/ETC/INIT.D

6,

The code is as follows:

./crond Restart

Let's take a look at a few specific examples:

0 */2 * * * */sbin/service httpd restart means to restart Apache every two hours

7 * * * */sbin/service sshd start with SSH service 7:50 every day

* * * * */sbin/service sshd stop means shut down SSH service 22:50 every day

0 0 1,15 * * fsck/home 1th and 15th monthly check/home disk

1 * * * * * */home/bruce/backup The first part of the hour to execute/home/bruce/backup this file

* * 1-5 find/home "*.xxx"-mtime +4-exec rm {}; Every Monday to Friday 3 o'clock, in the directory/home, locate the file named *.xxx and delete the file 4 days ago.

6 */10 * * ls means 1, 11, 21, 31st of every month Yes 6:30 execute the LS command once

Between 11 o'clock and 8 in the morning, every two hours, eight in the morning.

0 23-7/2,8 * * * echo "Have a Good Dream:)" >>/tmp/test.txt

7, edit cron profile/etc/crontab, as follows:

The code is as follows:

Shell=/bin/bash

Path=/sbin:/bin:/usr/sbin:/usr/bin

Mailto=root//If there is an error, or if there is data output, the data is sent to this account as mail

home=///user Run path, this is the root directory

# Run-parts

* * * * * root run-parts/etc/cron.hourly//hourly execution of scripts within/etc/cron.hourly

4 * * * Root run-parts/etc/cron.daily//daily execution of/etc/cron.daily scripts

4 * * 0 root run-parts/etc/cron.weekly//per week execute/etc/cron.weekly script

4 1 * * Root run-parts/etc/cron.monthly//monthly to execute/etc/cron.monthly script

Attention to the "run-parts" This parameter, if you remove this parameter, then you can write a script to run the name, not the folder name.

8. If a cron task needs to be performed according to schedule instead of hourly, daily, weekly, or monthly execution, it can be added to the/ETC/CRON.D directory. All files in the directory use the same syntax as in/etc/crontab.

The code is as follows:

# Record the memory usage of the system every Monday

# at 3:30AM in the File/tmp/meminfo

3 * * Mon cat/proc/meminfo >>/tmp/meminfo

# Run custom Script the ' the ' of every month at 4:10am

4 1 * */root/scripts/backup.sh

9. Quick Dispatch Task

We are already very familiar with using the cron daemon to execute a scheduled command.

Cron is an advanced command of Linux system management that is used to plan scheduled tasks such as backups or anything that specifies time or interval.

However, do you know that the AT command allows you to schedule a task or command at a specified time? The AT command can specify the time to execute the specified content.

For example, you intend to execute the uptime command at 11:2 A.M., and you only need to do this:

The code is as follows:

$ at 11:02

Uptime >>/home/$USER/uptime.txt

Ctrl+d

Check if the AT command is set successfully, using:

The code is as follows:

$ at-l

At supports scheduling multiple commands, such as:

The code is as follows:

$ at 12:30

Command–1

Command–2

...

Command–50

...

Ctrl + D

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.