(1) at 一次性的计划任务
语法:
# at [参数] [时间]
at> 执行的指令
退出at命令 ctrl+d
[root@localhost ~]# at now
at> mkdir te
at> cp /etc/passwd /te
at> <EOT>
[root@localhost ~]# atq 查询at任务
[root@localhost ~]# atrm 删除at任务
at会把任务放到/var/spool/at目录下。
(2) crontab 周期性的计划任务
语法格式:
[root@localhost ~]# vim /etc/crontab
# Example of job definition:
# .---------------- minute ( - )
# | .------------- hour ( - )
# | | .---------- day of month ( - )
# | | | .------- month ( - ) OR jan,feb,mar,apr ...
# | | | | .---- day of week ( - ) (Sunday= or ) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed[root@localhost ~]# crontab -e -u username #为username创建任务
[root@localhost ~]# crontab -l -u username #列出任务
[root@localhost ~]# crontab -r -u username #删除任务* * * * * /bin/echo hello
*/ * * * * /bin/echo hello #每隔一分钟
(3) anacron
也是Linux系统中的调度工具,与cron很类似(可每天、每周或每月周期性地执行),但也有不同:
– 执行cron的系统必须随时保持启动,因为如果在指定的时间主机没有正常启动,则调度的任务就无法执行。
– anacron如果在指定的时间没有成功执行,则会在一段时间进程控制与管理之后再次执行,所以顺利执行的概率较高。
所有的任务都设置在/etc/anacrontab文件中,在这个文件中的每一行都表示一个任务(注释与环境变量例外),它们的格式如下:
时间间隔 等待时间 任务标识 命令
• 时间间隔:执行任务的时间间隔,单位为天。
• 等待时间:在时间间隔到期后,如果任务没有顺利执行,则会等待此处设置的时间,然后再次尝试执行。
• 任务标识:有关此任务的说明,它可包含任何非空格的字符(/除外),通常都用在anacron信息中,或是此任务的时间戳文件名。
• 命令:实际执行的任务。
当任务完成后,anacron会将此日期记录在/var/spool/anacron目录的Timestamp文件中,默认的Timestamp文件有三个:cron.daily,cron.monthly和cron.weekly
[root@localhost ~]# vim /etc/anacrontab
# /etc/anacrontab: configuration file for anacron# See anacron() and anacrontab() for details.SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=
# the jobs will be started during the following hours only
START_HOURS_RANGE=-#period in days delay in minutes job-identifier command
cron.daily nice run-parts /etc/cron.daily
cron.weekly nice run-parts /etc/cron.weekly
@monthly cron.monthly nice run-parts /etc/cron.monthly