Implement scheduled tasks in Linux

Source: Internet
Author: User
Article Title: Implement scheduled tasks in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

1. Cyclic scheduled tasks

In linux, there are two types of scheduled tasks: atd and crond. Among them, the at command used by the atd service can only be executed once, while the crontab command used by the crond Service performs cyclically, that's why crond meets our requirements.

Crontab supports two statuses: 1. Writing scheduled tasks directly; 2. Executing scheduled tasks in a directory.

2. Available users

By default, users that can log on to the system can use crontab to define scheduled tasks. However, you can use the following two files to restrict permissions:

/Etc/cron. allow:

Write the account that can use crontab into it. If the account is not in this file, crontab cannot be used;

/Etc/cron. deny:

You can use crontab to write accounts that are not allowed to use crontab. If the account is not recorded in this file, you can use crontab.

Similar to the method defined by TCPWrapper.

3. directly use crontab to edit a scheduled task:

Command:

Crontab [-u username] [-l |-e |-r]

Parameters:

-U: Use-u to help other users create/remove crontab;

-E: Edit crontab content

-L: View crontab content

-R: Remove all contents of the crontab (all content. If you only delete one content, use-e to edit it)

Content format:

* *** Command

The first five signs (*) indicate minute, hour, day, month, and week, for example:

Meaning minute Hour Date month week

Number range: 0-59 0-23 1-31 1-12 0-7

* Indicates the meaning accepted at any time.

* Use spaces to separate numbers. If a range is used, use-signs to connect them. If they are separated for a few times, use a number to represent them.

In addition, the command must be executed by the user who has the permission to write the scheduled task and use the absolute path.

For example:

# Crontab-e

59 23 1 5 * mail linuxing

The content of/home/test.txt is sent to the linuxing user by email every November 23, May 1.

*/5 */opt/test. sh

Run the/opt/test. sh script every five minutes.

0, 3, 6 */usr/local/bin/test. sh

Run the/usr/local/bin/test. sh command every 3 and six o'clock.

0 8-12 ***/root/backup. sh

Run/root/backup. sh at every hour between and.

4. Execute scheduled tasks in directory-based mode

The scheduled tasks of the system have been defined in/etc/crontab, And the directory-based approach is used. The system regularly reads the file and executes the command according to the definition in it.

You can use vi to directly write the/etc/crontab file. The format is as follows:

# Cat/etc/crontab

SHELL =/bin/bash # shell used

PATH =/sbin:/bin:/usr/sbin:/usr/bin # predefined PATH

MAILTO = root # send an Email to this user if any problem occurs

HOME =/# HOME Directory

# Run-parts

01 ***** root run-parts/etc/cron. hourly # directory per hour

02 4 *** root run-parts/etc/cron. daily # daily

22 4 ** 0 root run-parts/etc/cron. weekly # every Sunday

42 4 1 ** root run-parts/etc/cron. monthly #1 of each month

Hours, days, months, and weeks executor identity command

We can see that the definition of the preceding five parameters is the same as that of the direct editing scheduled task. The user-defined execution and run-parts parameters are added.

Run-parts is followed by the directory name, for example:/etc/cron. hourly, indicating that/etc/cron is reached every hour. all executable files under the execution directory in the hourly directory; of course, the directory can be customized.

If you need to add scheduled tasks to the system, you only need to add execution files to the corresponding directory. For example, I need to perform the updatedb operation every day, then, I only need to link the execution command of/usr/bin/updatedb to/etc/cron. the daily directory is enough.

Similarly, if you do not need to use a directory, you can also use the following method:

02 01 *** root/test. sh

That is, without the run-parts command, the absolute path of the command will be followed directly.

5. Notes

If the scheduled task does not take effect after you use crontab to edit the scheduled task or directly modify the/etc/crontab file, restart the crond service: service crond restart.

When writing the/etc/crontab file, do not miss the user who specified the execution plan task. This is different from editing with crontab-e directly.

A user (such as root) uses crontab-e to edit the scheduler tasks and stores them in/var/spool/cron/root. But it is best not to edit it directly, because during crond execution, the required temporary file will be created in the/tmp directory, and direct editing will affect this, or even cause errors.

Each task executed by cron is recorded in the/var/log/cron log file. You can view the command execution status from this file.

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.