Linux timed Execution Task method __linux

Source: Internet
Author: User
Tags tmp file cron script

Http://soft.chinabyte.com/os/237/11428737.shtml

(1) at command

If we just want to make a particular task run once, then we need to use the at monitor.

Setting the AT command is simple, indicating the time of the scheduled run, and then when it will run. At a similar print process, the task is placed in the/var/spool/at directory and runs at a specified time. The AT command is the equivalent of another shell, which, when run at the time command, sends a command to enter any command or program. The at now + Time command can be used to indicate the task.

Suppose you work with a large database to process data when someone is not using the system, like 3:10 A.M.. Then we should build the/home/kyle/do_job script Management database and plan to process the results in the/home/kyle/do_job file. The normal way to do this is to start the following command:

# at 2:05 tomorrow

At>/home/kyle/do_job

At> Ctrl+d

The time representation method in

-----------------------------------------------------------------------

Time examples Show

-----------------------------------------------------------------------

Minute at now + 5 minutes task runs in 5 minutes

Hour at now + 1 Hour task runs after 1 hours

Days in Now + 3 day task runs after 3 hours

Weeks at now + 2 Weeks task runs after two weeks

Fixed at midnight task runs at midnight

Fixed at 10:30pm task at 10:30 in the evening

Note: Be sure to check that the ATQ service is started, some operating systems may not be started by default, Linux defaults to not start, and Ubuntu defaults to boot. Check whether to start, with service ATD Check grammar, with Service ATD status Check ATD State, with Service ATD start ATD Service.

To view the specific contents of at execution: generally located under the/var/spool/at directory, open with VI, in the last part of your execution program

(2) Crontab

Cron is a regular execution tool under Linux that can run jobs without human intervention. Because Cron is a built-in service for Linux, it does not automatically get up and you can start and close this service in the following ways:

  /sbin/service crond start //启动服务
  /sbin/service crond stop //关闭服务
  /sbin/service crond restart //重启服务
  /sbin/service crond reload //重新载入配置
  /sbin/service crond status //查看服务状态

(Crond or cron looking at the actual situation

Service Crond Start #启动服务
Service Crond Stop #关闭服务
Service Crond Restart #重启服务
Service Crond Reload #重新载入配置

)

You can also start this service automatically when the system is started:

At the end of the/etc/rc.d/rc.local script, add:

/sbin/service Crond Start

Now that the Cron service is in the process, we can use this service, and the Cron service provides the following interfaces for everyone to use:

1. Edit directly with crontab command

The Cron service provides the crontab command to set the Cron service, and here are some of the parameters and instructions for this command:

Crontab-u//Set a user's Cron service, which is required by the general root user when executing this command

CRONTAB-L//list details of a user's cron service

Crontab-r//Delete a user's cron service

CRONTAB-E//Edit a user's cron service

For example, root view your cron settings: Crontab-u root-l

Again for example, Root wants to delete Fred's cron settings: Crontab-u fred-r

Basic format:

* * * * command

Time-sharing and Lunar Week command

The 1th column represents minutes 1~59 per minute with * or */1

The 2nd column represents the hour 1~23 (0 for 0 points)

The 3rd column represents the date 1~31

The 4th column represents the month 1~12

The 5th list of the week 0~6 (0 for Sunday)

6th column the command to run

Some examples of crontab files:

#每晚的21:30 reboot Apache.

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

#每月1, 10, 22nd

4 1,10,22 * */USR/LOCAL/ETC/RC.D/LIGHTTPD restart

#每天早上6点10分

6 * * * Date

#每两个小时

0 */2 * * * Date

#晚上11点到早上8点之间每两个小时, 8 in the morning.

0 23-7/2,8 * * * Date

#每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点

0 4 * mon-wed date

#1月份日早上4点

Http://j3j5.com/post-41.html


If you need to implement Task Scheduler on Linux platform, you can write a cron script to implement a task at a certain frequency Linux lacks the capital to start the Crond process, the Crond process does not require user startup, shutdown.  The Crond process reads the dispatch task and executes it, and the user simply writes the corresponding dispatch script to the cron dispatch configuration file. Cron's dispatch files are as follows: Crontab cron.d cron.daily cron.hourly cron.monthly cron.weekly

Execute method One: If the task you are using is not performed in hourly monthly weekly, you can write the corresponding crontab to the crontab or CRON.D directory.
Example: Execute a script every other minute/opt/bin/test-cron.sh can create a new script in CRON.D echo-date.sh content is

1 */1 * * * * root/opt/bin/test-cron.sh

To run a task at a specified time
You can also use the AT command to run tasks at a specified time such as: At-f test-cron.sh-v 10:25, where-f specifies the script file,-v specifies that the runtime first export the Contab configuration with Contab-l, and then edit the Contabs.tmp File. Add a row in the format: minutes, days, months, weeks, 3 * * 0,6 Hello is the Saturday, Sunday 3:10 to execute the Hello program. 4 * * 4-6 Hello is the Hello program from the 4 point 15 point from Thursday to Saturday. The new configuration is then imported with the Contab contabs.tmp command.  It is generally not recommended to directly modify the related configuration files under/etc/. How to start a cron process:/etc/init.d/crond start starts the cron Process Setup command: Chkconfig--add crond
Implementation Method II: To add cron to the startup script: # rc-update Add vixie-cron defaultCrontab-l #查看你的任务 crontab-e# Edit your task crontab-r# Delete the contents of the user's crontab

Two examples: system cron settings:/etc/crontabBy/etc/crontab files, you can set the tasks that the system performs on a regular basis, and of course, to edit this file, you must have root permission 0 7 * * * Root mpg123 ~/wakeup.mp3 time-sharing and Lunar weekExample:
1 0 4 * * 0 root emerge--sync && emerge-ud World #每周日凌晨4点, update system
2 0 2 1 * * Root rm-f/tmp/* #每月1号凌晨2点, clean/tmp files
3 0 8 6 5 * Root mail Robin

If, I want to execute a command every 2 minutes, or I want to execute the command at 6, 12, and 18 every day, and so on, you can set it up by "/" and ",":
01 */2 * * * * * * Root ... ... #每两分钟就执行 ...... ......
02 0 6,12,18 * * * * * Root ... ... #每天6点, 12, 18 o '-........
03 Every two hours
04 0 */2 * * * echo "have a break now." >> /tmp/test.txt
05 Between 11 o'clock and 8 in the morning, every two hours, eight in the morning.
06 0 23-7/2,8 * * * echo "Have a good dream:)" >> /tmp/test.txt
07 Number 4th per month, 11 in the morning of Monday to Thursday three.
08 0 4 * 1-3 command line
09 January 1 morning, 4.
10 0 4 1 1 * command line

Harvest: You can put a lot of things to do in it, to simplify the workload, such as every Monday to check the status of the server, view the report, kill some of the process and so on ...

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.