1.首先我们先看一下是否安装了crontab。一般情况下linux系统会自带crontab及crond。
如果没有安装,请使用以下指令安装:yum install vixie-cron
[root@localhost 桌面]# yum install vixie-cron
已加载插件:fastestmirror, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Loading mirror speeds from cached hostfile
base | 3.6 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
updates/x86_64/primary_db | 9.5 MB 00:00:17
没有可用软件包 vixie-cron。
错误:无须任何处理
报错没有可用安装包,百度了一下,安装命令是:yum install cronie
[root@localhost 桌面]# yum install cronie
已加载插件:fastestmirror, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 cronie.x86_64.0.1.4.11-11.el7 将被 升级
--> 正在处理依赖关系 cronie = 1.4.11-11.el7,它被软件包 cronie-anacron-1.4.11-11.el7.x86_64 需要
---> 软件包 cronie.x86_64.0.1.4.11-23.el7 将被 更新
--> 正在检查事务
---> 软件包 cronie-anacron.x86_64.0.1.4.11-11.el7 将被 升级
---> 软件包 cronie-anacron.x86_64.0.1.4.11-23.el7 将被 更新
--> 解决依赖关系完成
依赖关系解决
======================================================================================
Package 架构 版本 源 大小
======================================================================================
正在更新:
cronie x86_64 1.4.11-23.el7 base 92 k
为依赖而更新:
cronie-anacron x86_64 1.4.11-23.el7 base 36 k
事务概要
======================================================================================
升级 1 软件包 (+1 依赖软件包)
总下载量:128 k
Is this ok [y/d/N]: y
Downloading packages:
No Presto metadata available for base
(1/2): cronie-1.4.11-23.el7.x86_64.rpm | 92 kB 00:00:00
(2/2): cronie-anacron-1.4.11-23.el7.x86_64.rpm | 36 kB 00:00:00
--------------------------------------------------------------------------------------
总计 163 kB/s | 128 kB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在更新 : cronie-anacron-1.4.11-23.el7.x86_64 1/4
正在更新 : cronie-1.4.11-23.el7.x86_64 2/4
清理 : cronie-anacron-1.4.11-11.el7.x86_64 3/4
清理 : cronie-1.4.11-11.el7.x86_64 4/4
验证中 : cronie-1.4.11-23.el7.x86_64 1/4
验证中 : cronie-anacron-1.4.11-23.el7.x86_64 2/4
验证中 : cronie-1.4.11-11.el7.x86_64 3/4
验证中 : cronie-anacron-1.4.11-11.el7.x86_64 4/4
更新完毕:
cronie.x86_64 0:1.4.11-23.el7
作为依赖被升级:
cronie-anacron.x86_64 0:1.4.11-23.el7
完毕!
2.开启crontab服务
[root@localhost 桌面]# service crond start
Redirecting to /bin/systemctl start crond.service
出现Redirecting to /bin/systemctl start crond.service, 即service crond start 需要替换为systemctl方式:
[root@localhost 桌面]# systemctl start crond
[root@localhost 桌面]# systemctl stop crond
3.查看运行结果,到日志中查看是否有信息输入。
如何查看crontab的日志记录:看 /var/log/cron这个文件就可以,可以用tail -f /var/log/cron观察
[root@localhost 桌面]# tail -f /var/log/cron
Aug 6 16:50:25 localhost crontab[4328]: (root) REPLACE (root)
Aug 6 16:50:25 localhost crontab[4328]: (root) END EDIT (root)
Aug 6 16:57:09 localhost crond[4419]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 49% if used.)
Aug 6 16:57:11 localhost crond[4419]: (CRON) INFO (running with inotify support)
Aug 6 16:57:11 localhost crond[4419]: (CRON) INFO (@reboot jobs will be run at computer's startup.)
Aug 6 16:57:11 localhost crond[4419]: (CRON) INFO (Shutting down)
Aug 6 16:57:11 localhost crond[4447]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 25% if used.)
Aug 6 16:57:13 localhost crond[4447]: (CRON) INFO (running with inotify support)
Aug 6 16:57:13 localhost crond[4447]: (CRON) INFO (@reboot jobs will be run at computer's startup.)
Aug 6 16:59:57 localhost crond[4447]: (CRON) INFO (Shutting down)
^Z
[1]+ 已停止 tail -f /var/log/cron
ctrl + Z 退出tail命令。
4.开启服务,查看状态
[root@localhost 桌面]# systemctl start crond
[root@localhost 桌面]# service crond status
Redirecting to /bin/systemctl status crond.service
crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled)
Active: active (running) since 五 2021-08-06 17:08:28 CST; 12min ago
Main PID: 4576 (crond)
CGroup: /system.slice/crond.service
└─4576 /usr/sbin/crond -n
8月 06 17:08:28 localhost.localdomain systemd[1]: Starting Command Scheduler...
8月 06 17:08:28 localhost.localdomain systemd[1]: Started Command Scheduler.
8月 06 17:08:28 localhost.localdomain crond[4576]: (CRON) INFO (RANDOM_DELAY will...)
8月 06 17:08:30 localhost.localdomain crond[4576]: (CRON) INFO (running with inot...)
8月 06 17:08:30 localhost.localdomain crond[4576]: (CRON) INFO (@reboot jobs will...)
8月 06 17:20:48 localhost.localdomain systemd[1]: Started Command Scheduler.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost 桌面]# systemctl stop crond
[root@localhost 桌面]# service crond status
Redirecting to /bin/systemctl status crond.service
crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled)
Active: inactive (dead) since 五 2021-08-06 17:25:06 CST; 6s ago
Process: 4576 ExecStart=/usr/sbin/crond -n $CRONDARGS (code=exited, status=0/SUCCESS)
Main PID: 4576 (code=exited, status=0/SUCCESS)
8月 06 17:08:28 localhost.localdomain systemd[1]: Starting Command Scheduler...
8月 06 17:08:28 localhost.localdomain systemd[1]: Started Command Scheduler.
8月 06 17:08:28 localhost.localdomain crond[4576]: (CRON) INFO (RANDOM_DELAY will...)
8月 06 17:08:30 localhost.localdomain crond[4576]: (CRON) INFO (running with inot...)
8月 06 17:08:30 localhost.localdomain crond[4576]: (CRON) INFO (@reboot jobs will...)
8月 06 17:20:48 localhost.localdomain systemd[1]: Started Command Scheduler.
8月 06 17:25:06 localhost.localdomain systemd[1]: Stopping Command Scheduler...
8月 06 17:25:06 localhost.localdomain systemd[1]: Stopped Command Scheduler.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost 桌面]#
5.查看其配置文件/etc/crontab 。
[root@localhost 桌面]# 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中每行任务的描述格式如下: minute hour day month dayofweek command
minute - 从0到59的整数
hour - 从0到23的整数
day - 从1到31的整数 (必须是指定月份的有效日期)
month - 从1到12的整数 (或如Jan或Feb简写的月份)
dayofweek - 从0到7的整数,0或7用来描述周日 (或用Sun或Mon简写来表示)
command - 需要执行的命令(可用as ls /proc >> /tmp/proc或 执行自定义脚本的命令)
对于以上各语句,星号(*)表示所有可用的值。例如*在指代month时表示每月执行(需要符合其他限制条件)该命令。
(
前5个(*)星号:
M(分钟0-59) H(小时0-23) D(天1-31) m(月1-12) d(一星期0-6,0为星期天)
特殊符号:
* 代表所有的取值范围的数字
/ 代表每的意思,*/5就是每5个单位
- 代表从某个数字到某个数字 , 分开几个离散的数字
eg: 0 14 * * 1-5 周一到周五每天下午两点触发任务
0 10 * * 0,6 周六、周天早上十点触发任务
*/5 9-10 * * 1-5 周一到周五九点到十点每五分钟触发任务
)
6.用户通过crontab -e来编辑定义自己的任务。
使用crontab -e进入编辑模式时,需要编辑执行的时间和执行的命令。设置crontab的基本格式:
* * * * * command
[root@localhost 桌面]# crontab -e
crontab: installing new crontab
[root@localhost 桌面]# crontab -l 查看设置的任务
30 23 1 * * service httpd restart
30 23 1 1 * service httpd restart
[root@localhost 桌面]# crontab -r 删除所有的任务
[root@localhost 桌面]# crontab -l
no crontab for root
[root@localhost 桌面]#