Linux基础学习之Day10-计划任务

本文介绍了Linux中用于计划任务的两个工具:at和cron。at命令用于一次性调度任务,例如在特定时间执行用户添加操作;cron则用于设置周期性的重复任务,如每天定时备份数据。crontab命令用于管理用户级的cron任务,包括列出、删除和编辑任务。示例展示了如何使用这些工具设置不同时间间隔的任务,并解释了cron作业的语法格式。
摘要由CSDN通过智能技术生成

Linux基础学习之从入门到精通Day10

计划任务

一次调度执行at

作用: 计划任务主要是做一些周期性的任务,目前最主要的用途是定期备份数据

Schedule one-time tasks with at. 一次性调度执行 at
Schedule recurring jobs with cron. 循环调度执行 cron
Schedule recurring system jobs.
所有计划任务执行中的输出都会以邮件的方式发送给指定用户, 除非重定向

一次性调度执行 at

[root@tianyun ~]# yum -y install at
[root@tianyun ~]# man at
[root@tianyun ~]# systemctl start atd
[root@tianyun ~]# systemctl enable atd

语法格式:

at
now +5min
teatime tomorrow (teatime is 16:00)
noon +4 days
5pm august 3 2018

例1:

[root@~]# at now +1min
at> useradd uuuu
at>
job 1 at Sat Mar 21 22:34:00 2015
[root@~]# atq
3 Thu Mar 30 09:55:00 2017 a root
[root@tianyun ~]# id uuuu

例2:

[root@~]# vim at.jobs
useradd u99
useradd u00
touch /date +%F.txt
[root@~]# at now +1min < at.jobs

循环调度执行cron 用户级

[root@~]# systemctl status crond.service
[root@~]# ps aux |grep crond
root 550 0.0 0.0 126300 1648 ? Ss 10:05 0:00 /usr/sbin/crond -n
crond进程每分钟会处理一次计划任务

=用户级=
存储位置:
[root@~]# ls /var/spool/cron/

管理方式:
crontab -l List the jobs for the current user.
crontab -r Remove all jobs for the current users.
crontab -e Edit jobs for the current user.
管理员可以使用 -u username, 去管理其他用户的计划任务

/etc/cron.deny:
alice
jack
[alice@~]$ crontab -e
You (alice) are not allowed to use this program (crontab)
See crontab(1) for more information

语法格式 Job format:
Minutes Hours Day-of-Month Month Day-of-Week Command

#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) OR sun,mon,tue,wed,thu,fri,sat
#| | | | |
#* * * * * command

示例:

00 02 * * * ls //每天2:00整
00 02 1 * * ls //每月1号2:00整
00 02 14 2 * ls //每年2月14号2:00整
00 02 * * 7 ls //每周日2:00整
00 02 * 6 5 ls //每年6月的周五2:00整
00 02 14 * 7 ls //每月14号2:00整 或者 每周日2:00整,这两个时间都执行
00 02 14 2 7 ls //每年2月14号2:00整 或者 每周日2:00整,这两个时间都执行

00 02 * * * ls //每天2:00整

  • 02 * * * ls //每天2:00中的每一分钟
          • ls //每分钟执行ls
    • 14 2 * ls //2月14号的每分钟 1440分钟

*/5 * * * * ls //每隔5分钟
00 02 1,5,8 * * ls //每月1,5,8号的2:00整
00 02 1-8 * * ls //每月1到8号的2:00整

[root@localhost ~]# crontab -l 显示当前用户的计划任务
[root@localhost ~]# crontab -r 移除当前用户所有计划任务

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值