Adding scheduled tasks under linux

The method of timed execution of tasks under linux 

In Linux , you should first enter crontab -e, then there will be a vi editing interface, and then enter 0 3 * * 1 /clearigame2 content into it: wq save and exit.

 

In LINUX, the tasks executed periodically are generally handled by the cron daemon [ps -ef|grep cron]. cron reads one or more configuration files that contain command lines and when they are invoked.

The configuration file for cron is called "crontab", short for "cron table".

1. Cron looks for configuration files in 3 places:
1. /var/spool/cron/ This directory stores the crontab tasks of each user including root, and each task is named after the creator, such as the crontab built by tom The file corresponding to the task is /var/spool/cron/tom.
Generally, a user has at most one crontab file.

Second, /etc/crontab This file is responsible for arranging the crontab for maintaining the system and other tasks formulated by the system administrator.

3. /etc/cron.d/ This directory is used to store any crontab files or scripts to be executed.

4. Permissions
crontab permissions Go to /var/adm/cron/ and see if the files cron.allow and cron.deny exist. The
usage is as follows: 
1. If the two files do not exist, only the root user can use the crontab command. 
2. If cron.allow exists but cron.deny does not exist, only the users listed in the cron.allow file can use the crontab command. If the root user is not included, the root user cannot use crontab either. 
3. If cron.allow does not exist and cron.deny exists, only the users listed in the cron.deny file cannot use the crontab command, and other users can use it. 
4. If both files exist, users who are listed in the cron.allow file and not listed in cron.deny can use crontab. If both files have the same user,
check whether the cron.allow file has the same user. This user prevails, if there is this user in cron.allow, you can use the crontab command.

 

5. cron service
  cron is a timing execution tool under linux, which can run jobs without manual intervention.
  /sbin/service crond start //Start the service
  /sbin/service crond stop //Close the service
  /sbin/service crond restart //Restart the service
  /sbin/service crond reload //Reload the configuration
  /sbin/service crond status // View service status 

在crontab文件中如何输入需要执行的命令和时间。该文件中每行都包括六个域,其中前五个域是指定命令被执行的时间,最后一个域是要被执行的命令。
    每个域之间使用空格或者制表符分隔。格式如下: 
  minute hour day-of-month month-of-year day-of-week commands 
    合法值 00-59 00-23 01-31 01-12 0-6 (0 is sunday) commands(代表要执行的脚本)
    除了数字还有几个个特殊的符号就是"*"、"/"和"-"、",",*代表所有的取值范围内的数字,"/"代表每的意思,"/5"表示每5个单位,"-"代表从某个数字到某个数字,","分开几个离散的数字。

 

几个例子: 
每天早上6点 
0 6 * * * echo "Good morning." >> /tmp/test.txt //注意单纯echo,从屏幕上看不到任何输出,因为cron把任何输出都email到root的信箱了。

每两个小时 
0 */2 * * * echo "Have a break now." >> /tmp/test.txt  

晚上11点到早上8点之间每两个小时和早上八点 
0 23-7/2,8 * * * echo "Have a good dream" >> /tmp/test.txt

每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点 
0 11 4 * 1-3 command line

1月1日早上4点 
0 4 1 1 * command line SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root //如果出现错误,或者有数据输出,数据作为邮件发给这个帐号 HOME=/ 

每小时执行/etc/cron.hourly内的脚本
01 * * * * root run-parts /etc/cron.hourly
每天执行/etc/cron.daily内的脚本
02 4 * * * root run-parts /etc/cron.daily 

每星期执行/etc/cron.weekly内的脚本
22 4 * * 0 root run-parts /etc/cron.weekly 

每月去执行/etc/cron.monthly内的脚本 
42 4 1 * * root run-parts /etc/cron.monthly 

注意: "run-parts"这个参数了,如果去掉这个参数的话,后面就可以写要运行的某个脚本名,而不是文件夹名。   

每天的下午4点、5点、6点的5 min、15 min、25 min、35 min、45 min、55 min时执行命令。 
5,15,25,35,45,55 16,17,18 * * * command

每周一,三,五的下午3:00系统进入维护状态,重新启动系统。
00 15 * * 1,3,5 shutdown -r +5

每小时的10分,40分执行用户目录下的innd/bbslin这个指令: 
10,40 * * * * innd/bbslink 

每小时的1分执行用户目录下的bin/account这个指令: 
1 * * * * bin/account

每天早晨三点二十分执行用户目录下如下所示的两个指令(每个指令以;分隔): 
20 3 * * * (/bin/rm -f expire.ls logins.bad;bin/expire$#@62;expire.1st)  

每年的一月和四月,4号到9号的3点12分和3点55分执行/bin/rm -f expire.1st这个指令,并把结果添加在mm.txt这个文件之后(mm.txt文件位于用户自己的目录位置)。 
12,55 3 4-9 1,4 * /bin/rm -f expire.1st$#@62;$#@62;mm.txt 

 

at命令实现定时任务
  假如我们只是想要让特定任务运行一次,那么,这时候就要用到at监控程序了。
    at类似打印进程,会把任务放到/var/spool/at目录中,到指定时间运行它 。at命令相当于另一个shell,运行at time命令时,它发送一个个命令,可以输入任意命令或者程序。

    at命令执行流程如下

  # at 2:05 tomorrow

  at>/home/kyle/do_job

  at> Ctrl+D

  AT Time中的时间表示方法

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

  时 间 例子 说明

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

  Minute    at now + 5 minutes   任务在5分钟后运行

  Hour      at now + 1 hour      任务在1小时后运行

  Days      at now + 3 days      任务在3天后运行

  Weeks     at now + 2 weeks     任务在两周后运行

  Fixed     at midnight          任务在午夜运行

  Fixed     at 10:30pm           任务在晚上10点30分

  注意:linux默认为不启动,而ubuntu默认为启动的。检查是否启动,用service atd检查语法,用service atd status检查atd的状态,用service atd start启动atd服务。

  查看at执行的具体内容:一般位于/var/spool/at目录下面, 用vi打开,在最后一部分就是你的执行程序



参数详解
-V : 印出版本编号 
-q : 使用指定的伫列(Queue)来储存,at 的资料是存放在所谓的 queue 中,使用者可以同时使用多个 queue,而 queue 的编号为 a, b, c... z 以及 A, B, ... Z 共 52 个 
-m : 即使程序/指令执行完成后没有输出结果, 也要寄封信给使用者 
-f file : 读入预先写好的命令档。使用者不一定要使用交谈模式来输入,可以先将所有的指定先写入档案后再一次读入 
网络应用


-l : 列出所有的指定 (使用者也可以直接使用 atq 而不用 at -l) 
-d : 删除指定 (使用者也可以直接使用 atrm 而不用 at -d) 
-v : 列出所有已经完成但尚未删除的指定 

删除任务
atrm 2


三天后的下午 5 点锺执行 /bin/ls : 
at 5pm 3 days /bin/ls 

三个星期后的下午 5 点锺执行 /bin/ls : 
at 5pm 2 weeks /bin/ls 

明天的 17:20 执行 /bin/date : 
at 17:20 tomorrow /bin/date 

the last minute of the last day of 1999 prints the end of world ! 
at 23:59 12/31/1999 echo the end of world !

 

 

http://blog.csdn.net/hi_kevin/article/details/8983746

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326315100&siteId=291194637