Linux,定时任务-crond服务、crontab命令

linux中的定时任务/计划任务,是为了简化系统管理员对于重复的有规律的任务提供的一种巧妙地服务。

比如:在业务低谷期,我们会对一些数据进行备份,可以对用户造成的影响降到最小。

在linux中,从系统安装完毕后**,cron**服务软件便开始自动启动了

在linux中常见的定时任务种类:crond、atd、anacron

具体说crond服务

查看软件包
[root@xxx ~/2020]# :rpm -aq cronie
cronie-1.4.11-19.el7.x86_64
查看软件安装路径
rpm  -ql  cronie

首先,介绍一下定时任务中一些常见的文件或目录

/etc/cron.hourly  系统每小时的定时任务
/etc/cron.daily  系统每天的定时任务
/etc/cronweekly  系统每周的定时任务
/etc/cron.monthly  系统每月的定时任务
/etc/cron.deny  系统拒绝  定时任务的黑名单
/etc/crontab  系统定时任务的配置文件
/var/spool/cron  每分钟都会查看该路径,用户的定时任务配置文件
/var/log/cron   用户的定时任务的日志文件

crontab命令

常见参数

-e  用户查看定时任务的内容
-l  用户编写定时任务的内容
-r  删除用户的定时任务的内容
-i  删除定时任务的时候,会提示确认
-u  指定使用用户执行任务

在编写定时任务时,注意格式说明

[root@xxx ~/2020]# :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
这五个*分别对应了分时日月周

练习不同的命令
在这里插入图片描述
**

crontab的使用

**
crontab与rsync结合使用

客户端操作
[root@xxx ~/2020]# :crontab -e
crontab: no changes made to crontab
You have new mail in /var/spool/mail/root
[root@xxx ~/2020]# :crontab -l
*/1 * * * * rsync -avzP --password-file=/etc/rsync.passwd /root/2020/backup_2020-03-26.tar.gz  rsync_backup@10.0.0.138::backup
服务器端查看
[root@hhh /backup]# ll
total 32
drwxr-xr-x. 2 root  root    19 Mar 30 10:44 01
drwxr-xr-x. 2 rsync rsync    6 Mar 30 21:35 0202
-rw-r--r--. 1 rsync rsync 2096 Mar 30 12:20 abc.txt
-rw-r--r--. 1 rsync rsync   11 Mar 30 08:42 a.txt
drwxr-xr-x. 2 rsync rsync   19 Mar 30 08:42 backup_2020-03-26
-rw-r--r--. 1 rsync rsync  174 Mar 30 10:35 backup_2020-03-26.tar.gz
发布了51 篇原创文章 · 获赞 5 · 访问量 1118

猜你喜欢

转载自blog.csdn.net/weixin_46669463/article/details/105226829