Linux 可以自动运行任务,并带有自动任务实用程序:cron、anacron、at、batch。
cron 作业可以每分钟运行一次。
如果系统关闭,将跳过计划的 cron 作业。
anacron 每天只能运行一次作业。
计划的作业会被记住并在下次系统启动时运行。
crond 守护进程在多个文件和目录中搜索预定作业:
1. /var/spool/cron/ 2. /etc/anacrontab 3. /etc/cron.d
at 和 batch
at 和批处理实用程序用于调度一次性任务。
at 命令在特定时间执行任务。
批处理命令在系统平均负载低于 0.8 时执行任务。
atd 服务必须正在运行才能运行或者批处理作业
在命令语法:
# at time
时间参数接受多种格式:
HH:MM MMDDYY,MM/DD/YY or MM.DD.YY month-name day year midnight: At 12:00 AM teatime: At 4:00 PM now + time -- here time can be minutes, hours, days or weeks
批处理命令 batch语法:
# batch (at> promp is displayed)
/etc/at.allow 和 /etc/at.deny 文件限制用户访问 at。
如果两个文件都不存在,则只有 root 可以使用 cron。
更多: zhilu jiaocheng
Crontab 实用程序
非 root 用户也可以使用 crontab 实用程序配置 cron。
用户定义的 crontab 存储在 /var/spool/cron/[username] 中。
创建或者编辑 crontab 条目:
# crontab -e
要列出用户定义的 crontab 中的条目:
# crontab -l
配置 anacron 作业
anacron 作业在 /etc/anacrontab 中定义。
工作由以下定义:
Period in days : frequency of execution in days Delay in minutes - Minutes to wait before executing the job job-identifier - A unique name used in logfiles command : a shell script or command to execute
示例 anacron 文件:
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=45 # the jobs will be started during the following hours only START_HOURS_RANGE=3-22 #period in days delay in minutes job-identifier command 1 5 cron.daily nice run-parts /etc/cron.daily 7 25 cron.weekly nice run-parts /etc/cron.weekly @monthly 45 cron.monthly nice run-parts /etc/cron.monthly
其他 cron 目录和文件
/etc/cron.d
- 包含与 /etc/crontab 语法相同的文件 - 只能由 root 权限访问
/etc 中的其他 cron 目录:
cron.hourly cron.daily cron.weekly cron.monthly
这些目录中的脚本每小时、每天、每周或者每月运行一次,具体取决于目录的名称。
/etc/cron.allow 和 /etc/cron.deny 文件限制用户对 cron 的访问。
如果两个文件都不存在,则只有 root 可以使用 cron。
配置 cron 作业
cron 作业在 /etc/crontab 中定义。
crontab 条目的格式如下:
Minutes Hours Date Month Day-of-Week command
where: Minutes = [0 to 59] Hours = [0 to 23] Date = [1 to 31] Month = [1 to 12] Day-of-Week = [0 to 6] 0=Sunday - 6=Saturday command = a script file or a shell command. Other special characters can be used: - An asterisk (*) can be used to specify all valid values. - A hyphen (-) between integers specifies a range of integers. - A list of values separated by commas (,) specifies a list. - A forward slash (/) can be used to specify step values.
日期:2020-09-17 00:14:49 来源:oir作者:oir