Let you learn how to schedule tasks in Linux

Source: Internet
Author: User
Article Title: let you learn how to schedule 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.

In the past year, Microsoft's windows 7 has become popular, but Linux systems have become increasingly popular. Do you know about Linux systems? Are you an application of Linux? This article describes Linux scheduled tasks in detail and plays a certain role in learning Linux scheduled tasks.

Linux has three types of scheduled tasks:

At: specifies a time to execute a task (applicable to one or more tasks, not required once executed)

Cron: automatically executes tasks based on one schedule (one or more tasks are used for periodic execution)

System-level scheduled tasks and their extensions anacron: automatically execute tasks after a specified interval is missed

I. at service: arranging a task for future execution requires an atd system background process.

# At now + 2 minutes press ENTER

> Enter the command to be executed

> Ctrl + d end input

Atq; query the current waiting task

If you use atq for query, the running task disappears. This is the focus of at scheduled tasks, only run once

Atrm: delete a waiting task

After a scheduled task is started, you can use the atrm command to delete it if you do not want to start the scheduled task.

Format: atrm Task Number

The command is followed by the scheduled task number. If it is not followed, all the scheduled tasks of the user will be deleted.

You can also go to the/var/spool/at directory to delete the scheduled task. All the files of the scheduled task are saved in this directory, you can use the rm-f file name to delete a scheduled task (delete it as a file because it is saved in this directory as a file)

In general, super users can use this command. For other users, it depends on two files:/etc/at. allow and/etc/at. deny.

If you want to prevent a user from using the scheduled task, you can directly write the user name into it. Only one row can be written.

Write both allow and deny.

Ii. cron Service

The process and control of cron have been briefly introduced above. Next let's take a look at how cron is implemented.

Compared with at, cron can periodically execute a command, but at can only be executed once.

Cron's background process name is crond, and cron is also a system V service, so we can use service crond start | stop to start and close this service, you can also use chkconfig or ntsysv to enable the cron service by default. These commands have been mentioned before.

# Crontab-e edit the cron table of the current user

# Crontab-l view the cron table of the current user

# Crontab-r Delete the cron process of the current user

# Crontab-u the user name controls the cron table as a user

Another important point is that after a scheduled task is created, it is stored in the/var/spool/cron directory.

When you use crontab-e to edit the current user's cron table, a vi file will appear, in the cron format. It is divided into two columns, with time on the left and running command on the right. Time is composed of five parts.

Example:

* *** Wall hello everyone

The five asterisks represent minute hour day-of-month-of-year day-of-week, and wall hello everyone is the command content. The above consciousness is to broadcast hello everyone every week every hour every month. You can define the specific time. If you want to send messages every two minutes, use */2 to replace the first *. It can also be expressed by specific time.

Crontab? E. Do not add the user name to the edited task.

The first * Asterisk represents the minute of the hour: The minute value ranges from 0 to 59.

The second asterisk indicates the hour of the day. The hour value ranges from 0 to 23.

The third * Asterisk represents the day of the month: day-of-month range from 1 to 31

The fourth * Asterisk represents the month of the year: month-of-year range from 1 to 12

The fifth asterisk indicates the day of the week: day-of-week ranges from 0 to 6, where 0 indicates Sunday.

User name: that is, the user who executes the program. This can be omitted;

Command: The executed command and parameters.

What if I want to execute the ls var/spool/cron task at 25 o'clock on January 1, September 10?

25*10 9 * ls var/spool/cron

*/1 *** 0 wall Hello redking.blog.51cto.com

*/Indicates the number of minutes of execution ~

After a cron task is created, you can use crontab-l to view the task, or you can view it in the/var/spool/cron/directory. The file name is the cron task of the corresponding user.

You can use crontab-r to delete the cron of the current user.

What is the difference between the scheduled run of vim/etc/crontab and crontab-e writing?

Vim/etc/crontab: crontab defined at the system level. The owner and group of/etc/crontab are root.

Crontab-e: Custom crontab, which can be written by all users.

The two methods record different locations, one in/etc/and the other in/var. Are called by cron services ~

Iii. System-level scheduled tasks and anacrontab Extension

Anacrontab is the extension file of the system scheduled task: the task is automatically executed after a specified interval is missed.

This is a system task that has been set up, cleaned up the system garbage, or automatically executed by some scripts. Generally, let's just do it and don't change it.

The configuration file is/etc/anaconrtab.

#/Etc/anacrontab: configuration file for anacron

# See anacron (8) and anacrontab (5) for details.

SHELL =/bin/sh

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

MAILTO = root

##### Format: period (frequency. Days) delay (latency, minutes) job-identifier command

1 65 cron. daily run-parts/etc/cron. daily

7 70 cron. weekly run-parts/etc/cron. weekly

30 75 cron. monthly run-parts/etc/cron. monthly

SHELL: The interpreter for running scheduled tasks. The default value is bash.

PATH: environment variable for executing commands

MAILTO: the departing user of the scheduled task

HOME: the HOME directory is/

Run-parts is a script in/usr/bin/run-parts. It is used to execute all scripts/programs in a directory.

Run-parts/etc/cron. hourly: Execute all scripts/programs under/etc/cron. hourly.

Run-parts

The first line means: Check whether the cron. daily file is executed after 65 minutes of power-on every day. If the cron. daily file is not executed today, execute it.

The second line indicates that the cron. weekly file is executed 70 minutes after the host is started every seven days. If the cron. weekly file is not executed within one week, the cron. weekly file is executed.

Similar to at, it is controlled by the/etc/cron. deny configuration file, which is written to allow and deny at the same time. The above is the Linux scheduled task.

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.