Linux beginner-Delay and timed tasks

Source: Internet
Author: User
Tags set time

Linux Beginner-delay and timed tasks

In the study of the Linux system, the South will encounter the tasks that need to be delayed and the tasks that need to be timed, just like the alarm clock of the mobile phone, it is necessary to use the system delay of the Linux system and the setting of the scheduled task, and the following will introduce the setup and use of the delay and scheduled tasks.

1. System Delay Task

A system delay task is a task set that the system task will take at a specified time or after a certain amount of time, and the tasks initiated by this command are one-off, unlike scheduled tasks.

Generally the command to initiate a delay task is "at", for example, under the directory "/mnt" There are 6 files, you need to delay emptying this directory, enter "at time" press ENTER after entering "rm-fr/mnt/*", the task input is completed, press "Ctrl+d" to complete the command. It can be seen from the graph that this command was completed after the set time.

The "at" command can also include parameters to implement different functions, commonly used parameters such as:

Enter "ATQ" or "at-l" to see the deferred tasks that have been set up.

Enter "ATRM task number" or "at-d task number" to delete the delayed task set.

There are other parameters, for example, enter the AT-C task number to view the contents of the command to be executed by the deferred task.

"At Now+xmin" can set the command to take place after a delay of X minutes. The "At-f file" can delay execution of the command content in the file. For example, write "Mkdir/mnt/test" in the file and execute it after one minute.

When you use the "at" command, you will find that both the normal user and the Superuser can initiate a delay command on the host, as shown, however, in some cases, you need to qualify the execution permissions of the deferred command.

There are two cases in which the execution rights of deferred commands are qualified, one is to disallow some users to execute deferred commands, and one is to allow only some users to execute deferred commands, and then to use the blacklist and whitelist of command execution.

The blacklist of command execution is not to allow some users to execute, this need to write the user's name under the file "/etc/at.deny", for example, the name of the student user is written to this file, the student user can no longer execute the delay command. But there is no file in which Tom users are not affected.

The white List of command execution is naturally to allow some users to execute the delay command, it is worth noting that in the system, the white list file does not exist, it is necessary to establish a "/etc/ra.allow" file, once the file appears, then all users will no longer be able to execute the delay command, for example, After the file is established, no further blacklist of Tom users can execute the delay command. Write "Student" in this file, then the student user can use the delay command.

2. Scheduled Tasks

Timed tasks are similar to alarms and are permanent and can be executed in a loop. The program of the timed task is crond and scanned every minute of the system.

There are two ways to initiate scheduled tasks in a general Linux system.

2.1. The first type of initiation is accomplished by command. There are three commonly used commands and their parameters, "Crontab-u username-e" means to set a timed task to run as username, "Crontab-u username-l" indicates the scheduled task of viewing settings, "Crontab-u Username-r "represents a scheduled task to delete a setting. For example, set up a task that performs a "touch/mnt/file{1..5}" at "22:30" to view and delete this scheduled task.

2.2, the second way to launch is through the editing of files to achieve. Write commands in the file "/etc/cron.d/filename", such as entering commands in this file "* * * * * root touch/mnt/root{1..5}" and "* * * * * * Tom touch/mnt/tom{1..5}".

2.3, the execution of the scheduled task power set. Similar to deferred task execution power setting, the blacklist setting of timed task execution power is to write "/etc/cron.deny" file, white list is to write "/etc/cron.allow" file, white list file also does not exist, need to establish itself.

2.4 Format the time when the task is set. As you can see above, you need to set the time when setting up a scheduled task, which is "* * * * *", the format of the time is:

*             *            *            *           *

Timeshare Month Week

The usual time formats and meanings are:

* * * * * every Minute

* * */2 * * every two days

* 08-17/2 * * * * Eight a.m. to five o'clock in the afternoon every two hours.

* * 01,15 * 6 per month, number 1th, 15th and Saturday

05 * * * * five minutes per hour

05 02 * * * Daily 2:05 A.M.

0 08 01 * * Every month, 8:30 A.M.

00 07 25 12 * December 25 7:00 A.M.

30 16 * * 5 each Friday 16:30

*/5 * * * * every five minutes

*/10 9-17 1,15 * * Every 17 minutes (10 points will not run) from 9 to 17 at 1th and 15th per month

0 0 1 Jan 0 January 1 of 0:0 and January each month Sunday (not only January 1 where Sunday)

3, the system temporary file management

The system is using the service is, will generate temporary files, these files after the service is completed or after a period of time is not used, it is necessary to the system generated temporary file management, timely removal of unnecessary files.

When cleaning the file, you need to first set the file's existence in the system time, the setting is done by writing "/usr/lib/tmpfiles.d/*.conf" file, where "*" is the name of the file, you can set it yourself, but you must end with ". conf". For example, in this file write "D/mnt/we 1777 root root 10s", where "D" is the file type, "/mnt/we" is the cleanup directory, "1777" is the execution permissions, "root" and "root" is the file owner and all the group of files, "10s" is the file existence time, combined, said the above content "in the/mnt directory has a everyone and all groups are root and the permissions of the directory of 1777, we, the directory of files can exist for 10s". This we directory does not exist in the system.

With "Systemd-tmpfiles--create/usr/lib/tmpfiles.d/we.conf", you can create a temporary file directory. "Systemd-tmpfiles--clean/usr/lib/tmpfiles.d/we.conf", you can clean up temporary files in this directory. It is important to note that only files that have expired files can be cleaned up. As shown, after a file has been created, only 10s can be cleaned up.

The above system temporary file Management Cleanup command requires its own input, can be used in conjunction with the scheduled task, so that the automatic cleanup of temporary files. For example, you need to create a "quit" directory under "/mnt" where files can exist in 2min, but the system will clean up this directory every minute. For convenience, these steps can be done with a script, as shown in. Enter "Watch-n 1 ls-rl/mnt" To see the changes in this directory.

After using the script file, a "Quit" directory appears under the "/mnt" directory, and 5 files are created in "quit" Directory at "4:49:53", and all five files are cleaned in "4:52:02".

The above is the common in Linux delay tasks, scheduled tasks and system temporary files management, and a little application.

Linux beginner-Delay and timed tasks

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.