计划任务和周期任务mail,at,batch,atq, atrm, cron, crontab

Linux上的任务计划:

任务计划:

在未来时间点一次性执行某任务:at,batch

周期性执行某任务:crontab

电子邮件服务:

查看邮件服务是否启动:netstat -tlnup |grep "25" //25 port  smtpservice normal

默认收发邮件仅能在本机上的用户间进行,且只能通过回环接口上的地址进行

smtp:simple mail transmission protocol

/var/spool/mail/USERNAME

pop3: postoffice protocol

imap4: internetmail access protocol

MUA:mail命令

mail命令:

mail:进入交互式收发邮件接口;

mail-s ‘SUBJECT‘ [email protected]

mail-s ‘SUBJECT‘ [email protected] < /PATH/FROM/SOMEFILE

COMMAND| mail -s ‘SUBJECT‘ [email protected]

at命令:

承载未来时间运行的某作业:

The at commandschedules a command to be run once at a particular time.

This can be anycommand that you normally have permission to run; anything from a simplereminder message, to a complex script.

You start byrunning the at command at the command line, passing it the scheduled time asthe option.

It then placesyou at a special prompt, where you can type in the command (or series ofcommands) to be run at the scheduled time.

When you‘redone, press Control-D on a new line, and your command will be placed in thequeue

支持使用作业队列:默认为a队列;

Ctrl+d 提交/确认

at [option]...TIME

TIME:

(1)绝对时间

HH:MM,

MMDD[CC]YY,MM/DD/[CC]YY, DD.MM.[CC]YY or [CC]YY-MM-DD

tomorrow

2:30PM 21.10.14   "2:30 PM October 212014"

2:30PM 10/21/2014 "2:30 PM October 21 2014"

(2)相对时间

now+#UNIT

minute,hour, day, week

now+ 30 minutes "10:30 AM October 18 2014"

(3)模糊时间

midnight“12:00:AMXXXXX”

noon     “12:00:PM XXXXX”

teatime  "4:00 PM October 18 2014"

atnow + 10 years

...andthen enter a command at the at> prompt, press enter, and type Control-D, youwill be mailed the results of your command ten years from now

Usingatq To View Your at Queue

Thisinformation is, from left to right: job number, date, hour, year, queue, andusername.

atq will only list jobs that belong toyou — unless you are the super user, in which case it will list the jobs of allusers. So to list all at jobs currently queued on the system, type this command(if you have superuser privileges):

sudo atq

常用选项:

-qQUEUE:at作业队列;

-f/PATH/FROM/SOMEFILE:从指定的文件中读取要运行的作业;

-l:查看作业队列中的等运行作业列表;相当于使用atq命令;

-cAT_JOB_NUM: 查看待运行作业的内容;

-d:删除指定作业;相当于atrm

实例:

at 9:30 PM Tue

warning:commands will be executed using /bin/sh

at> echo"Well gosh golly, it‘s 9:30 PM on Tuesday."

at> ^D

job 1 at Tue Nov16 09:30:00 2014

Note: The outputof your specified command will be mailed to you. You can read this mail withthe mail program, or a program like pine (or the modern version of pine, calledalpine). You can download these programs if you don‘t have them, or installthem with your package manager; for example, on Ubuntu, which uses the APTpackage management system, you can install them using the apt-get command,specifically: sudo apt-get install mail or sudo apt-get install alpine.

batch:

由系统自行选择在资源较为空闲时运行指定的任务;batchexecutes commands when system load levels permit; in other words, when the loadaverage drops below 1.5, or the value specified in the invocation of atd.

Files

/var/spool/cron/atjobs

/var/spool/cron/atspool

/proc/loadavg

/var/run/utmp

/etc/at.allow

/etc/at.deny

Examples

at -m 01:35 <my-at-jobs.txt

Runthe commands listed in the ‘my-at-jobs.txt‘ file at 1:35 AM. All output fromthe job will be mailed to the user running the task. When this command has beensuccessfully entered you should receive a prompt similar to the example below:

commandswill be executed using /bin/sh

job1 at Wed Dec 24 00:22:00 2014

at -l

Thiscommand will list each of the scheduled jobs in a format like the following:

1          Wed Dec 24 00:22:00 2003...this isthe same as simply running the command atq.

crontab:周期性任务计划

Thecrontab is a list of commands that you want to run on a regular schedule, andalso the name of the command used to manage that list. crontab stands for"cron table," because it uses the job scheduler cron to executetasks; cron itself is named after "chronos," the Greek word for time.

守护进程:crond

周期性任务有两类:

(1) 系统cron任务;没有默认运行用户身份,所以需要额外指定运行者;

/etc/crontab

vim命令

#Example of job definition:

#.---------------- minute (0 - 59)

#|  .------------- hour (0 - 23)

#|  | .---------- day of month (1 - 31)

#|  | |  .------- month (1 - 12) ORjan,feb,mar,apr ...

#|  | |  |  .---- day of week (0 - 6) (Sunday=0 or 7) ORsun,mon,tue,wed,thu,fri,sat

#|  | |  |  |

#*  * *  *  * user-name command to be executed

7个字段:

前5个字段:时间点

user-name:以哪个用户的身份运行任务

commandto be executed: 要运行任务

(2) 用户cron任务:由某个用户所提交,默认就以提交者的身份运行,所以无需额外指定运行者;

/var/spool/cron/USERNAME

vim命令;不建议

crontab命令:建议

#Example of job definition:

#.---------------- minute (0 - 59)

#|  .------------- hour (0 - 23)

#|  | .---------- day of month (1 - 31)

#|  | |  .------- month (1 - 12) ORjan,feb,mar,apr ...

#|  | |  |  .---- day of week (0 - 6) (Sunday=0 or 7) ORsun,mon,tue,wed,thu,fri,sat

#|  | |  |  |

#*  * *  *  * command to be executed

5个时间点:

分钟:有效取值范围0-59;

小时:0-23

日:1-31

月:1-12

周几:0-7

注意:几月几号与周几,不建议同时使用;

例如:

6* * * *

时间表示法:

*:对应时间点有效取值范围内的每个时间点;

-:一个特定连续时间范围,3-7

,:一个离散的时间点,3,5,7

/#:有效时间范围内的每多少时间;用于指定频率;

1-30/4,*/4

5*/3 * * * /bin/echo "howdy"

57 * * 1-5 /bin/echo "howdy"

crontab命令:

crontab[-u user] [-l | -r | -e]

-uuser: 不是管理自己的cron任务,而是指定的目标用户的cron任务;仅root拥有管理其它用户的cron任务的权限;默认管理自己的;

-l:list,列出任务;

-r:remove, 移除所有任务;

-e: edit,编辑,打开一当前shell会话的默认编辑器来编辑cron任务表;

Afield may be an asterisk (*), which always stands for "first throughlast".

Rangesof numbers are allowed. Ranges are two numbers separated with a hyphen. Thespecified range is inclusive; for example, 8-11 for an "hours" entryspecifies execution at hours 8, 9, 10 and 11.

Listsare allowed. A list is a set of numbers (or ranges) separated by commas.Examples: "1,2,5,9", "0-4,8-12".

Stepvalues can be used in conjunction with ranges. For example, "0-23/2"can be used in the hours field to specify command execution every other hour.Steps are also permitted after an asterisk, so if you want to say "everytwo hours", you can use "*/2".

Namescan also be used for the "month" and "day of week" fields.Use the first three letters of the particular day or month (case doesn‘tmatter). Ranges or lists of names are not allowed.

The"sixth" field (the rest of the line) specifies the command to be run.The entire command portion of the line, up to a newline or % character, will beexecuted by /bin/sh or by the shell specified in the SHELL variable of thecronfile. Percent-signs (%) in the command, unless escaped with backslash (\),will be changed into newline characters, and all data after the first % will besent to the command as standard input.

Notethat the day of a command‘s execution can be specified by two fields: day ofmonth, and day of week. If both fields are restricted (in other words, theyaren‘t *), the command will be run when either field matches the current time.For example, "30 4 1,15 * 5" would cause a command to be run at 4:30am on the 1st and 15th of each month, plus every Friday

思考:

1、如果想实现每8分钟运行一次某任务?*/8* * * * /bin/echo "howdy"

2、如果想实现每10秒钟运行一次某任务?

#!/bin/envbash

while[ true ];

do   # do what you need to here

sleep30

done

注意:

(1)如果不想接收任务执行结果的通知邮件:

COMMAND> /dev/null

COMMAND&> /dev/null

(2)对于crontab文件来讲,%有特殊功用;如果命令中会出现%,要记得转义,或者用单引号对其引用;

(3)crontab的PATH变量与用户的变量不完全相同,所以,建议在cron中的任务要使用绝对路径

/root/bin/a.sh

补充性的工具:anacron:

Anacron is thecron for desktops and laptops. Anacron does not expect the system to be running24 x 7 like a server.

When you want abackground job to be executed automatically on a machine that is not running 24x 7, you should use anacron. For example, if you have a backup script scheduledeveryday at 11 PM as a regular cron job, and if your laptop is not up at 11 PM,your backup job will not be executed. However, if you have the same jobscheduled in anacron, you can be sure that it will be executed once the laptopcome back up. Anacrontab Format Just like how cron has /etc/crontab, anacronhas /etc/anacrontab.

/etc/anacrontabfile has the anacron jobs mentioned in the following format.

练习:

1、每周一到周六的凌晨3点20分,运行cp命令对/etc/目录进行归档另存,存储位置为/backups/etc-YYYY-MM-DD;

2、每周日凌晨2点30分,运行cp命令对/etc/fstab文件进行备份,存储位置为/backup/fstab-YYYY-MM-DD-hh-mm-ss

3、每天晚上12点,取得/proc/meminfo文件中所有以S或M开头的行,追加至/statistics/meminfo.txt文件中,且每天的消息之前,要加上类似===============分隔线;

时间: 2024-08-07 17:02:41

计划任务和周期任务mail,at,batch,atq, atrm, cron, crontab的相关文章

mail,at,batch,sleep小练习

1)如果有自己喜欢的电影,公司服务器,晚上访问量小,带宽使用小,此时用个at让晚上下载或白天用batch命令,让内核决定什么时候下载. 2)如何每天0点对数据库备份或etc目录备份.对于每天重复的事情crontab可以解决 3)磁盘满了给root发mail 4)如何实现秒级别的执行命令:在每分钟到达时,运行一个命令,需要60秒,就行了 5)如何实现每7分钟运行一次任务? 6)每4小时备份一次/etc目录至/backup目录中,保存的文件名称格式为"etc-yyyy-mm-dd-HH.tar.xz

Linux任务计划、周期性任务执行之cron/crontab

周期性任务计划需要一个程序包,不间断的监视时间.CentOS7需要主程序包:cronie(提供crond守护进程及相关辅助工具) 周期性任务计划工作首先需要确保crond守护进程正产运行 crond需要使用专用的配置文件,此文件有固定的格式,不建议使用文本编辑器直接编辑此文件,要使用crontab命令 系统cron任务:主要用于实现自身的维护  文件位于:/etc/crontab 注意:PATH中指定的命令目录不多,如果将就原始参数,有很多执行的命令需要写命令的绝对路径,推荐重新定义PATH参数

Linux未来任务的调度——at,batch,crontab命令

### 在linux中,我们可以实现对于任务的未来调度,从而可以借助于linux实现半自动化运维操作方式,简而言之就是可以在未来的某个时间点,系统可以自动运行我们部署的任务 前提: 需要一个准确的时间 可以支持任务调度的应用程序 **注意**: 在计划任务中,所有执行的命令的输出结果,这类及结果均以邮件的方式发送至指定的用户邮箱中: 邮箱的发送: mail命令 mail [-s SUBJECT]  USERNAME[@DOMAINNAME] (Ctrd + d发送) 系统用户的邮箱的目录/var

计划任务及其相关命令

Linux 计划任务的分类: 一次性任何计划: 未来的某时间点执行一次任务 at:专门用来处理一次性任务计划 batch :系统自行选择空闲时间去执行此处指定的任务 周期性的任务计划: 周期性运行某任务 crontab :可以根据定义的周期信息,循环的去做些事情 at 计划任务 at 是由atd服务提供的,依赖与atd 服务, 需要启动才能实现at 任务 在centos6上可以用service atd start来启动服务 在centos7上要使用systemctl start atd.serv

Linux之计划任务

1 at:一次性任务配置,其任务只能在指定的时间执行一次,前提是atd服务开启. [[email protected] ~]# at 17:45 2017-05-30                    #设置任务 at> ls / > a.txt at> <EOT> job 3 at 2017-05-30 17:45 [[email protected] ~]# atq                                    #查看一次性任务 3 2017-

Linux学习笔记:Linux系统的进程调度(任务调度)

今天我们学习了Linux系统的进程调度,进程调度是为了在未来某个时间点,让系统自动执行我们事先编写好的命令或脚本的列表,从而使得即使用户不在计算机旁边也可以按时完成任务.这样有利于我们更好的进行任务计划以及在需要执行任务的时候自动完成我们设定好的命令,从而完成任务. 当然为了能够进行进程调度,就必须要有一种机制能够识别事件发生的时间并且能够运行用户所预期的命令:这种机制在Linux中是通过特定的守护进程来实现的.这类的进程有: atd:将预先编写好的命令在未来某个时间点执行一次. crond:

Linux计划任务at&batch&crontab

计划任务        执行一次某任务:at,batch        周期性运行某任务:crontab    1.at命令,只执行一次 at/atq/atrm/batch    at [option]... time        HH:MM[YYYY-mm-dd] //        noon,midnight,teatime //12:00,24:00,16:00        tomorrow HH:MM         now+#            UNIT:minutest,ho

谢烟客---------Linux之邮件服务及任务计划执行

任务计划命令 mail,at,batch,crond,sleep 邮件服务工作模式: 发 代理-> stmp --> smtp --> 投递代理 --> 邮筒 --> pop3,imap4 <-- 代理 <-- 收 at,mail,batch,cron命令均建议用完整路径或在脚本中定义PATH 特点: mail -s '主题' [email protected] 发邮件 batch 根据负载选定命令执行时间 at HH:MM am|pm [YYYY-MM-DD]

Linux基础知识---计划任务

计划任务 所谓的计划任务是在未来某一特定的时间执行一次或多次特定的作业(任务).实现无需人工干预的情况下执行作业. 一.计划任务的分类 根据计划任务在未来被执行的次数,分为一次性计划任务和周期性计划任务. 一次性计划任务:只在未来的某一时间执行一次. 周期性计划任务:在未来某一特定时间内循环执行多次. 二.一次性计划任务 一次性计划任务常用的命令有at,batch. 1.设定一次性计划任务 1)at命令设定一次性计划任务,它是由atd后台进程管理的.要想设置一次性任务需启动atd服务进程.在Ce