linux 定时任务

定时任务

  1. crond任务调度

crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进程,crond进程每分钟会定期检查是否有要执行的任务,如果有要执行的任务,则自动执行该任务。Linux下的任务调度分为两类,系统任务调度和用户任务调度。

系统任务调度:系统周期性所要执行的工作,比如写缓存数据到硬盘、日志清理等。在/etc目录下有一个crontab文件,这个就是系统任务调度的配置文件。/etc/crontab文件包括下面几行:

root@hcss-ecs-c2b8:~# cat /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
# You can also override PATH, but by default, newer versions inherit it from the environment
#PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
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 )
#

用户任务调度:用户定期要执行的工作,比如用户数据备份、定时邮件提醒等。用户可以使用 crontab 工具来定制自己的计划任务。所有用户定义的crontab 文件都被保存在 /var/spool/cron目录中。其文件名与用户名一致。下面是几个特殊的文件介绍:

文件:/etc/cron.deny 该文件中所列用户不允许使用crontab命令
文件:/etc/cron.allow 该文件中所列用户允许使用crontab命令
文件:/var/spool/cron/ 所有用户crontab文件存放的目录,以用户名命名

  1. crontab文件

    用户所建立的crontab文件中,每一行都代表一项任务,每行的每个字段代表一项设置,它的格式共分为六个字段,前五段是时间设定段,第六段是要执行的命令段,格式如下:

    minute hour day month week command
    其中:
    minute: 表示分钟,可以是从0到59之间的任何整数。
    hour:表示小时,可以是从0到23之间的任何整数。
    day:表示日期,可以是从1到31之间的任何整数。
    month:表示月份,可以是从1到12之间的任何整数。
    week:表示星期几,可以是从0到7之间的任何整数,这里的0或7代表星期日。
    command:要执行的命令,可以是系统命令,也可以是自己编写的脚本文件。
    在以上各个字段中,还可以使用以下特殊字符:
    在以上各个字段中,还可以使用以下特殊字符:
    
    星号(*):代表所有可能的值,例如month字段如果是星号,则表示在满足其它字段的制约条件后每月都执行该命令操作。
    逗号(,):可以用逗号隔开的值指定一个列表范围,例如,“1,2,5,7,8,9”
    中杠(-):可以用整数之间的中杠表示一个整数范围,例如“2-6”表示“2,3,4,5,6”
    正斜线(/):可以用正斜线指定时间的间隔频率,例如“0-23/2”表示每两小时执行一次。同时正斜线可以和星号一起使用,例如*/10,如果用在minute字段,表示每十分钟执行一次。
    
  2. 简单编写一个日志输出的定时任务

    我们常用的命令如下:

    crontab [-u username]    //省略用户表表示操作当前用户的crontab
        -e      (编辑工作表)
        -l      (列出工作表里的命令)
        -r      (删除工作表)
    

    我们用crontab -e进入当前用户的工作表编辑,是常见的vim界面。每行是一条命令。

    crontab的命令构成为 时间+动作,其时间有分、时、日、月、周五种,操作符有

    • ***** 取值范围内的所有数字
    • /n每过多少个数字
    • n-m 从X到Z
    • **,**散列数字
    root@hcss-ecs-c2b8:/var/spool/cron/crontabs# crontab -e
    no crontab for root - using an empty one
    # 这里他会让我们选择一个编辑器来编写定时任务
    Select an editor.  To change later, run 'select-editor'.
      1. /bin/nano        <---- easiest
      2. /usr/bin/vim.basic
      3. /usr/bin/vim.tiny
      4. /bin/ed
    # 我选的vim,
    Choose 1-4 [1]: 3
    # 这里是编辑器内的内容
    # Edit this file to introduce tasks to be run by cron.
    # 
    # Each task to run has to be defined through a single line
    # indicating with different fields when the task will be run
    # and what command to run for the task
    #
    # To define the time you can provide concrete values for
    # minute (m), hour (h), day of month (dom), month (mon),
    # and day of week (dow) or use '*' in these fields (for 'any').
    #
    # Notice that tasks will be started based on the cron's system
    # daemon's notion of time and timezones.
    #
    # Output of the crontab jobs (including errors) is sent through
    # email to the user the crontab file belongs to (unless redirected).
    #
    # For example, you can run a backup of all your user accounts
    # at 5 a.m every week with:
    # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
    #
    # For more information see the manual pages of crontab(5) and cron(8)
    # 
    # m h  dom mon dow   command
    # 这里我们每隔一分钟像日志中打印一下当前的日期就能知道他对应的定时任务是否生效
    */1 * * * * date >> /var/test/timer_task_date.log
                                                            
    crontab: installing new crontab
    root@hcss-ecs-c2b8:/var/spool/cron/crontabs# 
    
    

    我们用crontab -l查看工作表内容,类似于cat查看文本文件

    root@hcss-ecs-c2b8:/var/test# crontab -l
    # Edit this file to introduce tasks to be run by cron.
    # 
    # Each task to run has to be defined through a single line
    # indicating with different fields when the task will be run
    # and what command to run for the task
    # 
    # To define the time you can provide concrete values for
    # minute (m), hour (h), day of month (dom), month (mon),
    # and day of week (dow) or use '*' in these fields (for 'any').
    # 
    # Notice that tasks will be started based on the cron's system
    # daemon's notion of time and timezones.
    # 
    # Output of the crontab jobs (including errors) is sent through
    # email to the user the crontab file belongs to (unless redirected).
    # 
    # For example, you can run a backup of all your user accounts
    # at 5 a.m every week with:
    # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
    # 
    # For more information see the manual pages of crontab(5) and cron(8)
    # 
    # m h  dom mon dow   command
    
    */1 * * * * date >> /var/test/per_minute.log
    

    我们用crontab -lr删除工作作,在使用-l查看,发现内容已经没有了,日志的打印内容也随之停止了

    oot@hcss-ecs-c2b8:/var/test# crontab -r
    root@hcss-ecs-c2b8:/var/test# crontab -l
    no crontab for root
    oot@hcss-ecs-c2b8:/var/test# cat timer_task_date.log 
    Sun Sep 29 11:09:01 PM CST 2024
    Sun Sep 29 11:10:01 PM CST 2024
    Sun Sep 29 11:11:01 PM CST 2024
    Sun Sep 29 11:12:01 PM CST 2024
    Sun Sep 29 11:13:01 PM CST 2024
    Sun Sep 29 11:14:01 PM CST 2024
    Sun Sep 29 11:15:01 PM CST 2024
    Sun Sep 29 11:16:01 PM CST 2024
    Sun Sep 29 11:17:01 PM CST 2024
    root@hcss-ecs-c2b8:/var/test# date
    Sun Sep 29 11:21:48 PM CST 2024
    root@hcss-ecs-c2b8:/var/test# 
    
    1. at定时任务

      基本介绍

      1.at命令是一次性定时计划任务,at的守护进程atd会以后台模式运行,检查作业队列来运行。

      2.默认情况下,atd守护进程每60秒检查作业队列,有作业时,会检查作业运行时间,如果时间与当前时间匹配,则运行此作业

      3.at命令是一次性定时的计划任务,执行完一个任务后不在执行此任务了

      4.在使用at命令的时候,一定要保证atd进程的启动,可以使用相关指令来查看

      ps -ef  查看所有运行的进程
      ps -ef | grep atd  可以检测atd是否在运行
      oot@hcss-ecs-c2b8:/var/test# ps -ef | grep atd
      daemon       986       1  0 Sep20 ?        00:00:00 /usr/sbin/atd -f
      root      124222  123429  0 23:23 pts/0    00:00:00 grep --color=auto atd
      root@hcss-ecs-c2b8:/var/test# 
      
      
      at命令格式
      
      at [选项][时间]
      选项:
      -f:指定包含具体指令的任务文件
      -q:指定新任务的队列名称
      -l:显示待执行任务的列表
      -d:删除指定的待执行任务
      -m:任务执行完成后向用户发送 E-mail
      时间:时间表达式
      at 命令使用的是我们日常生活中所使用的时间格式,非常方便:
      
      YYMMDDhhmm[.ss]
      (缩写年、月、日、小时、分钟[])
      CCYYMMDDhhmm[.ss]
      (完整年、月、日、小时、分钟和[])
      now
      midnight
      noon
      teatime 下午茶时间 下午四点
      AM
      PM
      时间和日期可以是绝对的,也可以添加一个加号 ( + ) 使它们相对于现在。在指定相对时间时,下面这些日常生活中所使用的词汇都可以使用:
      minutes
      hours
      days
      weeks
      months
      years
      Ctrl + D 结束at命令的输入
      
      root@hcss-ecs-c2b8:/var/test# date
      Sun Sep 29 11:35:07 PM CST 2024
      root@hcss-ecs-c2b8:/var/test# echo "time past 1 minute" | at now + 1 minutes
      warning: commands will be executed using /bin/sh
      job 7 at Sun Sep 29 23:36:00 2024
      root@hcss-ecs-c2b8:/var/test# 
      

      思考:linux提供了方便简介的定时任务功能,在服务端开发的时候我们需要对日志没数据,等进行备份,只需要将对应脚本或命令配置一下可以省去许多人力物力,日志堆积也可以用此方案定时检查堆积内容的大小,查过一定阈值将旧的删除

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值