Linux Timed Task System Cron Getting Started

  

cron is a timed execution tool in Linux that can run jobs without human intervention. Since Cron is a built-in service for Linux, but it does not automatically get up, you can start and shut down the service in the following ways:

/sbin/service crond start //Start the service

/sbin/Service crond stop //close the service

/sbin/service crond restart //restart the service

/sbin/service crond reload //reload the configuration


You can also start this service automatically when the system is started:

At the end of the /etc/rc.d/rc.local script:

/sbin/Service crond start

Cron this service is now in the process, we can use this service, Cron service provides the following interfaces for everyone to use:

1. Directly use the crontab command Edit

The cron service provides the crontab command to set the cron service. The following are some parameters and descriptions of this command:

crontab -u //Set a user's cron service, generally The root user needs this parameter when executing this command

crontab -l //lists the details of a user cron service

crontab -r //delete the cron service without a user

crontab -e //Edit a user's cron service

For example, root to view their own cron settings: crontab -u root -l

And for example, root wants Delete fred's cron settings: crontab -u fred -r

When editing the cron service, there are some formatting and conventions for editing the contents. Enter: crontab -u root -e

Enter vi edit Mode, the edited content must conform to the following format: */1 * * * * ls >> /tmp/ls.txt

The first part of this format is the setting of time, the latter part Is the command to be executed, if there are too many commands to be executed, you can write these commands into a script, and then directly call this script here, remember to write the full path of the command when calling. The setting of time has certain conventions. The first five * numbers represent five numbers. The range and meaning of the numbers are as follows:

minutes (0-59)

hours (0 -23)

Date (1-31)

Month (1-12)

Week (0-6)//0 for Sunday

In addition to the numbers, there are a few special symbols that are "*","/" and "-",",",* represent all numbers in the range of values, "/" Representing each meaning, "*/5" means that every 5 units, "-" represents a number from a number to a number, "," separates several discrete numbers. Here are a few examples to illustrate the problem:

Every day at 6am

0 6 * * * echo "Good morning." >> /tmp/test.txt //Note Simple echo, you can't see any output from the screen, because cron emails any output to the root mailbox.

Every two hours

0 */2 * * * echo "Have a break now." >> /tmp/test.txt

Night Every two hours between 11:00 and 8:00 am, 8:00 am

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

The 4th of each month and the Monday of each week until 11am on Wednesday

0 11 4 * 1-3 command line

January 1 at 4 am

0 4 1 1 * command line

After editing a user's cron settings, cron is automatically generated under /var/spool/cron A file with the same name as this user, this user's cron information is recorded in this file, this file can not be directly edited, can only be edited with crontab -e. After cron starts, it reads this file every time it is read, and checks if it wants to execute the commands inside. Therefore, you do not need to restart the cron service after this file is modified.

2. Edit /etc/crontab file configuration cron

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

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO= Root //If there is an error, or there is data output, the data is sent to this account as an email

HOME=///The path the user runs, here is the root directory

# run-parts

01 * * * * root run-parts /etc/cron.hourly //execute scripts in /etc/cron.hourly every hour

02 4 * * * root run-parts /etc/cron.daily //Execute the script in /etc/cron.daily every day

22 4 * * 0 root run-parts /etc/cron.weekly //execute /etc/cron every week. Scripts in weekly

42 4 1 * * root run-parts /etc/cron.monthly //execute the scripts in /etc/cron.monthly every month

every attention" Run-parts" This parameter, if you remove this parameter, you can write a script name to run later, not the folder name.



Copyright © Windows knowledge All Rights Reserved