2023-03-30计划将来的任务

计划延迟的用户作业

计划好的命令通常称为任务或作业,延迟则表示任务或作业是在未来执行的;可以使用at命令。

  • at命令是由atd守护进程负责,并提供了a-z一共26个队列,队列越后,优先级越高。
  • at计划任务只能计划将来的任务,无法处理重复的任务或周期性的任务
  • at计划任务从RHEL7开始支持,在此之前都是使用cron
[root@myhost ~]# systemctl status atd
● atd.service - Job spooling tools
   Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
   Active: active (running) since 三 2023-03-22 18:50:53 CST; 1 weeks 4 days ago
 Main PID: 1298 (atd)
    Tasks: 1
   CGroup: /system.slice/atd.service
           └─1298 /usr/sbin/atd -f

3月 22 18:50:53 myhost systemd[1]: Started Job spooling tools.
  • 创建at计划任务
#使用at的交互界面
[root@serverb ~]# at now +5min
at> echo "hello">/root/attest.txt
at> <EOT>
job 2 at Mon Apr  3 13:40:00 2023

#直接使用命令行,适用于脚本
[root@serverb ~]# echo "hello 5min" >>/root/attest.txt | at now +10min
job 3 at Mon Apr  3 13:48:00 2023

#查看当前所有的at计划任务
[root@serverb ~]# atq
2       Mon Apr  3 13:40:00 2023 a root
3       Mon Apr  3 13:48:00 2023 a root

#删除at命令计划任务
[root@serverb ~]# atq
4       Mon Apr  3 15:05:00 2023 a root
[root@serverb ~]# atrm 4
[root@serverb ~]# atq

计划周期性用户作业

按计划重复执行的作业被称为周期性作业。

  • RHEL中的周期性作业由crond守护进程负责,由cronie软件包提供
  • 如果计划任务未设置重定向的输出或错误,则通过邮件通知对应的用户
  • 描述用户作业格式
    -- 默认使用crontab -e调用vim,每行只能输入一个周期性作业
    -- * * * * * shell command表示每行周期性作业的格式分钟 小时 几号 月份 星期几 执行的命令
    -- 分 时 日 月 周的表达语法结构
    *表示“无关紧要”,/始终
    数字可用于指定分钟数,小时数,几号,几月,星期几
    x-y表示范围,x到y(含)
    x,y表示列表,列表也可以包含范围
    */x表示x的时间间隔
[root@serverb ~]# crontab -e
[root@serverb ~]# crontab -l
*/1 * * * * echo "hello cron" >> ~/cron.txt  #每分钟追加一条hello cron到家目录的cron.txt文件中
[root@serverb ~]# cat ~/cron.txt
hello cron
hello cron
  • crontab命令常用选项
    -- crontab -e表示编辑当前用户的周期性作业
    -- crontab -e -u USERNAME表示编辑指定用户的周期性作业,root用户操作
    -- crontab -l表示查看当前用户的所有周期性作业
    -- crontab -l -u USERNAME表示查看指定用户的周期性作业,root用户操作
    -- crontab -r删除当前用户所有计划任务
[root@serverb ~]# crontab -r  #删除当前用户所有周期计划任务
[root@serverb ~]# crontab -l  
no crontab for root

计划周期性作业

系统管理员经常需要运行周期性作业;最佳做法就是从系统账户运行;系统范围的crontab文件和用户的crontab类似。

[root@serverb ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# 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
  • 周期性系统作业在两个位置定义:/etc/crontab文件和/etc/cron.d目录中的文件。
  • 要计划的周期性系统作业,应始终在/etc/cron.d目录下创建自定义crontab文件。
  • 通过将自定义的crontab文件放入/etc/cron.d,可以防止/etc/crontab的供应商进行软件更新时将它覆盖。
  • crontab系统中还包含需要每小时、每天、每周和每月运行的脚本存储库。这些存储库分别命名为/etc/cron.hourly、/etc/cron.daily、/etc/cron.weekly、/etc/cron.monthly的目录。这些目录中包含可执行的shell脚本,而不是crontab文件。
  • /etc/cron.hourly、/etc/cron.daily、/etc/cron.weekly、/etc/cron.monthly目录存放着每时、每天、每周、每月要运行的脚本,这些脚本都要加上可执行权限
#创建一个系统计划任务,每小时统计一下系统活动用户数量
[root@serverb ~]# vim /etc/cron.hourly/usercount
#!/bin/bash
USERCOUNT=${ w -h | wc -l }
logger "there are currently ${USERCOUNT} active users"
#每个整点会运行该脚本
]# tailf /var/log/messages
Apr  3 22:01:01 serverb root: there are currently 2 active users
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容