The interpretation and use of Linux crontab

Source: Internet
Author: User

Linux timed execution crontab crontab-e
Enter a VI editing interface

In the last line add

*/30 * * * * netstat >/tmp/net.log

Indicates that the netstat command is executed every 30 minutes and the execution results are stored in Net.log.

Crontab is a handy program to perform a task on a unix/linux system on a timed (cyclic) basis.

Using the Cron service, view the Cron service status with service Crond status, and if not, service Crond start starts it,

The cron service is a timed execution service that can be added or edited with the crontab command to perform tasks that need to be timed:

Crontab-u//Set a user's Cron service, which is usually required by the root user when executing this command

CRONTAB-L//list details of a user cron service

Crontab-r//Delete a cron service with no users

CRONTAB-E//Edit a user's cron service

For example, root to view your cron settings: Crontab-u root-l

Again, for example, Root wants to delete Fred's cron settings: Crontab-u fred-r

When editing the Cron service, the edited content has some formatting and conventions, input: Crontab-u root-e

Enter VI edit mode, the content of the edits must conform to the following format: */1 * * * * ls >>/tmp/ls.txt

Edit the/etc/crontab file and add a line at the end: 5 * * * Root init 6 This configures the system to automatically restart every morning at 5 o ' 30.

You need to set Crond as a service that starts automatically after system startup, and in/etc/rc.d/rc.local, at the end, add

Service Crond Start

If you also need to load additional services on the system boot Ten, you can continue to add the start command for other services.

For example: Service mysqld start

Basic usage:

1. Crontab-l

List the current crontab tasks

2. crontab-d

Delete the current crontab task

3. CRONTAB-E (solaris5.8 above is crontab-r)

Edit a crontab task, Ctrl_d end

4. crontab filename

A task list file that is crontab as filename and loaded into

Format of crontab file:

The rows in the crontab file consist of 6 fields separated by a space or TAB key between the different fields. First 5 fields specify when the command will run

Minutes (0-59)

Hours (0-23)

Date (1-31)

Month (1-12)

Day of the week (0-6, of which 0 stands for Sunday)

The 6th field is an example of a string to execute at an appropriate time:

#MIN HOUR Day MONTH DAYOFWEEK COMMAND

#每天早上6点10分

6 * * * Date

#每两个小时

0 */2 * * * DATE (Solaris 5.8 does not seem to support this type of notation)

#晚上11点到早上8点之间每两个小时, 8 in the morning.

0 23-7/2,8 * * * Date

#每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点

0 4 * mon-wed date

#1月份日早上4点

0 4 1 Jan * Date

Add: When using crontab, it is important to note that the environment variables that are accessible in the run script are not necessarily consistent with the environment variables in the current test environment, and it is safer to set the environment variables yourself in a running script (export)

(1) First build a file Crond.txt as follows, restart 5:36 A.M. every day

5 * * * reboot

(2) Upload to/opt directory

(3) Run the command

Crontab/opt/crond.txt

Crontab-l

Let the configuration file take effect: If you let the configuration file take effect, you also have to restart Cron, remember, since each user under the Cron configuration file after modification. Also restart the cron server.

In Fedora and redhat, we should use;

[Email protected] ~]#/etc/init.d/crond restart

If you let Crond run at boot time, you should change its operating level;

[Email protected] ~]# chkconfig--levels Crond on

Service Crond Status View Cron service state, if not started, service Crond start it, cron service is a scheduled service, you can add or edit tasks that need to be performed by Crontab command

Each line of the crontab file consists of six fields (minutes, hours, day of month, month, day of the Week, command), separated by spaces or tabs, where:

Minutes: Minute field, the range of values is 0 to 59

Hours: Hour field, the range of values is 0 to 23

Day of Month: date, the range of values is 1 to 31

Month: The range of values is 1 to 12

Day of week: Week, value range is 0 to 6, Sunday value is 0

Command: Commands to run

If a field is *, it indicates that the command can be executed within the range of all possible values for that domain.

If a field is a two number separated by hyphens, the command can be executed within the range of two digits (including two numbers themselves).

If a field is made up of a series of values separated by commas, the command can be executed within the range of these values.

Both fields are valid if the date and weekday fields have values.

Write a file to start the automatic backup process.

Cd/opt

Touch Reboot.txt

Add something to the Reboot.txt:

0 4 * * * reboot

Crontab/opt/reboot.txt

Edit timed operations with CRONTAB-E, such as by adding a downstream command:

Use the crontab-l command to view

Note: The service needs to be started (added in rc.local)

Restart Crond Task

/etc/init.d/cron restart (under Ubuntu) the first system that is managed in RPM packages such as Fedora or redhat;

[[email protected] ~]#/etc/init.d/crond start

[[email protected] ~]#/etc/init.d/crond stop

[Email protected] ~]#/etc/init.d/crond restart

/etc/rc.d/init.d/crond restart

Introduction to Commands

The crontab-operates the daemon for each user and the schedule for that execution.

Partial parameter description

crontab file [-u user]-replaces the current crontab with the specified files.

Crontab-[-u user]-replaces the current crontab with standard input.

crontab-1[user]-lists the user's current crontab.

Related Article

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.