Linux System timing Tasks

Source: Internet
Author: User

first, the timing task Crond Introduction


  1. Crond is a service or software that is used on a Linux system to perform regular commands or to specify program tasks.

  2. Crond by default (per minute), checks the system for scheduled tasks that need to be performed, and if so, performs this scheduled task according to predefined rules. Just like the alarm clock in life. Note, however, that Crond is powerless for a second-level task (scheduled tasks in seconds) and needs to write a daemon (shell loop) script or use quartz to implement a second-level task.

  3. In Linux, the tasks performed by the cycle are typically handled by the cron Daemon (ps-ef|grep cron). The cron configuration file is called "crontab" and is shorthand for "cron table". Cron reads one or more configuration files that contain the command line and its invocation time.

  4. Service Crond Start//Startup services
    Service Crond stop//Shutdown services
    Service Crond restart//Restart services
    Service Crond Reload//Reload Configuration
    Service Crond Status//view services statuses

Second, Linux scheduled task classification:

    1. Periodic tasks performed by the system itself: such as polling system logs, backing up system data, cleaning up system caches, and so on, these timed tasks do not require human intervention. configuration file for System task scheduling:/etc/crontab.

/etc/crontab Configure the basic format:

Shell=/bin/bash #指定了系统要使用哪个shell

Path=/sbin:/bin:/usr/sbin:/usr/bin #指定了系统执行 the path to the command,

Mailto=root #指定了crond的任务执行信息将通过电子邮件发送给root用户, if the value of the MAILTO variable is null, it means that no task execution information is sent to the user.

home=/#HOME变量指定了在执行命令或者脚本时使用的主目录

Basic format:
* * * * * command
Time-sharing Weekly command
The 1th column represents minutes 0~59 per minute *

The 2nd column represents hours 0~23 per hour *

The 3rd column represents the date 1~31 per day *

The 4th column represents the month 1~12 per month *

5th Column Identification Number Week 0~6 (0 = Sunday)

6th List of commands to run


/n: Every nth unit of time (minutes, hours, months, weeks)

17-19: Indicates a range

,: Indicates a different point in time


Essentials When writing timed tasks:

1. Increase the frequency of scheduled task commissioning. Personal development environment--Office test environment->IDC Room Test->IDC officially launched

2. Debug tasks through script log output.

3. Note the timing task failure (JAVA) caused by environment variables.

4, through the Scheduled task service log debugging tasks. (Var/log/cron)

5, the System task in order to use absolute path.

6, the Script permission problem to add/bin/bash.

7. The time variable problem is escaped with \%, preferably with a script.

8. Timed Tasks plus annotations

9. Use a script instead of a command line timed task.

10, avoid unnecessary program command output.



For example:

* * * * */usr/local/etc/rc.d/lighttpd restart

The above example shows that 21:30 restarts Apache per night.

2. User-performed tasks: tasks that a system administrator or user will perform on a regular basis. The user regularly perform work, such as user data backup, timed email reminders, and so on. Users can use the Crontab tool to customize their own scheduled tasks. configuration file for user task scheduling: /var/spool/cron/file name ( whose file name is the same as the user name, typically a user has only one crontab file).

Three, crontab command detailed


  1. Command function: With the crontab command, we can execute specified system instructions or shell script scripts at a fixed interval of time. The units of the time interval can be any combination of minutes, hours, days, months, weeks, and more. This command is very useful for periodic log analysis or data backup.

  2. Command format: crontab [-u user] [file] crontab [-u user] [-e |-l |-r]

  3. Command parameters:

    -u User: Used to set a user's crontab service, for example, "-U-Tom" means to set Tom User's Crontab service, this parameter usually has the root user to run.

    -E: Edits the contents of a user's crontab file. If you do not specify a user, the crontab file for the current user is edited.

    -L: Displays the contents of a user's crontab file, or displays the contents of the current user's crontab file if no user is specified.

    -r: Deletes a user's crontab file from the/var/spool/cron directory and, if no user is specified, deletes the current user's crontab file by default.

    -I: Give a confirmation prompt when deleting a user's crontab file.





Linux System timing Tasks

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.