计算机网络/计算机科学与应用/系统/运维/开发

认真学Linux -【crontab】定时任务

一、用户级别的定时任务(命令)

crontab命令 周期性执行任务


学习crontab命令之前先认识crond, crontab命令需要crond服务支持,它是linux操作系统中用来周期性,执行某种任务或等待处理任务的一个守护进程,crond是一个服务,启动后,一直在后台运行,工作时,使用crontab命令进行配置,命令crond执行任务列表


crontab是配置各种定时任务,具体执行任务需要使用crond服务

crond 服务的启动和自启动:

# systemctl start crond  启动crond服务
# systemctl enable crond 设为开机自启动
# ps -ef|grep  crond  查询crond服务是否已经启动
[root@VM-4-2-centos ~]# ps -ef|grep crond
root      4404 26021  0 11:45 pts/0    00:00:00 grep --color=auto crond
root      4871     1  0  2022 ?        00:04:29 /usr/sbin/crond -n


# systemctl status crontab   查看crond服务的状态
[root@VM-4-2-centos ~]# systemctl status crontab
Unit crontab.service could not be found.


[root@VM-4-2-centos ~]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2022-06-29 16:22:27 CST; 10 months 19 days ago
 Main PID: 4871 (crond)
    Tasks: 1
   Memory: 688.0K
   CGroup: /system.slice/crond.service
           └─4871 /usr/sbin/crond -n


crond 服务每分钟会检查是否有要执行的任务,如果有则自动执行,一般情况下,操作系统安装完成后,默认就会安装crond服务和crontab工具,crond服务默认

开机自启动,如果不存在,则可以通过DNF命令安装部署

# dnf -y install crontabs


启用周期性任务的前提条件,对应系统服务crond服务必须已运行,通过/etc/cron.allow和/etc/cron.deny 文件控制用户是否可以使用crontab命令


/etc/cron.allow 只有写入此文件的用户可以使用crontab命令,没有写入的用户不能使用crontab命令,优先级最高

/etc/cron.deny 写入此文件的用户不能使用crontab命令,没有写入文件的用户可以使用crontab命令


默认只存在/etc/cron.deny /etc/cron.allow文件需要自行创建

所有用户通过crontab命令配置定时任务,配置文件默认存放在/var/spool/cron 文件名以用户名命名


crontab语法:

编辑计划

crontab -e [-u 用户名]

查看计划

crontab -l [-u 用户名]

删除计划

crontab -r [-u 用户名]


提示:

执行crontab -e命令 系统调用文本编辑器 (默认vim)打开文件/var/spool/cron/用户名


crontab命令配置定时任务语法:

时间周期设置                任务内容设置:

50    3      2   1    *         run_command

分钟  小时  日期 月份  星期     要执行的命令


说明:

分钟:取值0-59 整数

小时:0-23 整数

日期:1-31整数

月份:1-12整数

星期:0-7 整数 0或7表示星期日

要执行的命令:执行的命令或程序脚本


符号:

*  范围内的任意时间

,   间隔的多个不连续的时间点

-  一个连续的时间范围

/  指定间隔的时间频率


实例:

0 23 * * 1-5:周一到周五每天23:00
30 2 * * 1,3,5: 周一,周三,周五的2点30分
0 8-18/2 * * *: 每天8点到18点之间每隔2小时


案例1:每天凌晨1:50停止sshd服务,防止员工远程登录服务器,到早上7.50再启动sshd服务,恢复登录

#crontab -e
50 1 * * * systemctl stop sshd
50 7 * * * systemctl start sshd


案例2:每3分钟备份一次/etc/passwd文件,并将备份的文件备注上时间日期

# mkdir /opt/passwdbak 创建备份文件夹
# crontab -e 配置定时任务
# date 时间
# crontab -l 查看刚才配置好的定时任务
*/3 * * * * /usr/bin/cp -rf /etc/passwd /opt/passwdbak/passwd-$(date+\%Y\%m\%d\%H\%M\%S)
# ll /opt/passwdbak 查看定时任务的备份文件


案例3:每3分钟执行一次Shell脚本

# vim demo47.sh
#!/bin/bash
echo "现在的时间是:`date +"%Y-%m-%d %H:%M:%S"`" >> /opt/date.txt
# crontab -e 配置定时任务 每2分钟执行一次shell脚本
# crontab -l 查看配置好的定时任务
# cat /opt/date.txt 定时任务按定义配置正常运行中


普通用户配置定时任务:

# su - xiaozhou 切换用户
$ crontab -e 配置定时任务
$ crontab -l 查看配置定时任务
*/2 * * * * /usr/bin/echo "1111" >> /home/xiaozhou/date.txt


$ cat date.txt 查看普通用户定时任务正常运行中
$ su - root 切换到root用户
# crontab -l 默认查看自己配置的定时任务
# crontab -l -u xiaozhou 查看指定用户配置的定时任务
*/2 * * * *  /usr/bin/echo "1111" >> /home/xiaozhou/date.txt


注意:

普通用户配置定时任务,需要注意权限问题,上面案例中没有指定用户,那是因为默认使用当前登录的用户

二、系统级别定时任务(配置文件)

文件名称 /etc/crontab


打开配置文件/etc/crontab

[root@VM-4-2-centos myscript]# 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

说明:

SHELL表示指定使用哪种Shell

PATH表示指定PATH环境变量

MAILTO表示将任务的输出的结果发送到指定的邮箱


/etc/crontab 语法格式:

50    3   2    1    *  user-name  run_command

分钟 小时 日期 月份 星期 用户名  要执行的命令


多出一个user-name 字段,定义用户名 表示执行任务时采用的身份

[root@VM-4-2-centos myscript]# 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
*/2 * * * *  xiaozhou      /usr/bin/echo "123" >> /home/xiaozhou/date.txt
*/3 * * * *  root          /bin/bash  /opt/demo47.sh
*/3 * * * *  root          /usr/bin/cp  -rf /etc/passwd  /opt/passwdbak/passwd-$(date+\%Y\%m\%d\%H\%M\%S)


三、参考文献

零基础学Linux

linux 计划任务 crontab

人生活在得失之间,得亦是失,失亦是得。

评论

^