Implement scheduled tasks under CentOS

Source: Internet
Author: User

Today, in a project, I encountered a fault: The system is conducting a stress test on the Weblogic-based OA system, and the number of concurrent connections is very small (much lower than the normal number ), the system uses the red-flag DC 4.1 for anteng 2 and apache for web Service forwarding.
After a series of checks, it was found that the previous two consecutive days of stress testing, resulting in 20 GB of space in the/var directory where logs are placed; as a result, new logs need to constantly overwrite old logs and frequent disk reads and writes, resulting in excessive IO usage. Therefore, the number of concurrent connections cannot meet the requirements. Clear the log file.
Considering that similar problems may occur in the future (of course, in reality, it is impossible to achieve the test results in a few days), we hope to use the method of regularly deleting logs. Here we take into account the need to use linux scheduled tasks, also known as routine commands.
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:

Reference ◆/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 TCPWrapper method.

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, you can use the following method if you do not need to use a directory:
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) saves the scheduled tasks edited by crontab-e under/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.