Linux下定时任务和例行任务


定时任务
新建一个定时任务
1. at Time     #执行at命令
    #Time的格式有:HH:MM YYYY-MM-DD

                                      HH[pm;am] [Month] [Day]
                                      HH[pm;am] + number[hours;days;weeks]
                                      HH:MM
2. 执行此命令后进入指令列下达模式,即输入要执行定时任务的命令,输入完成后按ctrl+D退出,at会输出定时任务编号jobId和执行时间。
可以使用atq查询自己所有的定时任务,使用atrm jobId删除一个定时任务


例行任务
新建一个例行任务
1. crontab -e #进入例行任务编辑模式,使用vi作为编辑器
2. 编写例行任务,如:
    0 12 * * * mail test < /home/test/test.txt
    1,5,6 12 * * * ls /
    * /5 * * * * /home/test/test.sh 

 #分时日月周 |========指令列===============|

使用crontab -l类出自己所有的例行任务,使用crontab -r删除自己所有的例行任务。如要删除某一个任务,使用crontab -e删除对应行就行了。


使用/etc/crontab
我们可以直接编辑/etc/crontab文件来管理例行任务,内容如下
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root    cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
其中test -x /usr/sbin/anacron,为要执行的命令,run-parts  /etc/cron.daily 表示/etc/cron.daily目录下的可执行文件都要执行一次。

相关内容