Linux命令:crontab

一: 命令概述

crontab是Linux下的定时处理的命令,用于设置周期性被执行的指令。该命令的功能就是让系统在指定的时间,去执行某个指定的工作
crontab就是一个自定义定时器

二: 基本操作命令(系统版本centos7)

1 查看是否安装:没有安装的话,自行前往安装

[root@localhost cron]# rpm -qa | grep crontab
crontabs-1.11-6.20121102git.el7.noarch
[root@localhost cron]# 
# 安装命令
yum install vixie-cron
yum install crontabs

2 该服务的启动和关闭:通过操作/sbin/下的crond命令去关闭、启动、重启、重新载入配置,操作如下

[root@localhost sbin]# ls -l /sbin/ | grep -w "crond"
-rwxr-xr-x. 1 root root       70216 89 2019 crond
[root@localhost sbin]# 
# 停止服务
[root@localhost sbin]# /bin/systemctl stop crond.service
# 启动服务
[root@localhost sbin]# /bin/systemctl start crond.service
# 重新载入配置
[root@localhost sbin]# /bin/systemctl reload crond.service
# 重启服务
[root@localhost sbin]# /bin/systemctl restart crond.service
[root@localhost sbin]# 

3 查看服务是否启动:有下面的三个命令进行查看

[root@localhost sbin]# pgrep crond
84647
[root@localhost sbin]# ps -elf|grep crond|grep -v "grep"
4 S root      84647      1  0  80   0 - 31595 hrtime 14:21 ?        00:00:00 /usr/sbin/crond -n
[root@localhost sbin]# 
# 查看该服务状态
[root@localhost sbin]# /bin/systemctl status crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 五 2021-12-24 14:21:16 CST; 1min 43s ago
  Process: 80360 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS)
 Main PID: 84647 (crond)
    Tasks: 1
   Memory: 664.0K
   CGroup: /system.slice/crond.service
           └─84647 /usr/sbin/crond -n

1224 14:21:16 localhost.localdomain systemd[1]: Started Command Scheduler.
1224 14:21:16 localhost.localdomain crond[84647]: (CRON) INFO (RANDOM_DEL...
1224 14:21:17 localhost.localdomain crond[84647]: (CRON) INFO (running wi...
1224 14:21:17 localhost.localdomain crond[84647]: (CRON) INFO (@reboot jo...
Hint: Some lines were ellipsized, use -l to show in full.

三: 配置文件

3.1 全局配置文件

[root@localhost spool]# ls -l /etc/ | grep -w "cron"
# 系统自动定期需要做的任务
drwxr-xr-x.  2 root root       54 1220 10:18 cron.d
# 每天执行一次的job
drwxr-xr-x.  2 root root       70 1220 12:24 cron.daily
# 用于控制不让哪些用户使用Crontab的功能
-rw-------.  1 root root        0 89 2019 cron.deny
# 每个小时执行一次的job
drwxr-xr-x.  2 root root       22 1220 10:17 cron.hourly
# 每月执行一次的job
drwxr-xr-x.  2 root root        6 610 2014 cron.monthly
# 每个星期执行一次的job
drwxr-xr-x.  2 root root        6 610 2014 cron.weekly
[root@localhost spool]# 

3.2 用户配置文件

每个用户都有自己的cron配置文件,通过crontab -e 就可以编辑

一般情况下我们编辑好用户的cron配置文件保存退出后,系统会自动就存放于/var/spool/cron/目录中

文件以用户名命名.linux的cron服务是每隔一分钟去读取一次/var/spool/cron、/etc/crontab、/etc/cron.d下面所有的内容.

[root@localhost testjar]# cd ~
[root@localhost ~]# cd /var/spool/cron/
# 查看该目录中是否有文件,没有是因为我们没有编辑用户的配置文件
[root@localhost cron]# ls
[root@localhost cron]# 
[root@localhost cron]# 

3.3 crontab文件格式

[root@localhost cron]#  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
 分  时 天 月 星期 
添加的命令必须以如下格式:    * * * * */command path 

前5个字段分别表示:
       分钟:0-59
       小时:1-23
       日期:1-31
       月份:1-12
       星期:0-6(0表示周日)

一些特殊符号:
       *	表示任何时刻
       ,	表示分割
  	-	表示一个段,如第二端里: 1-5,就表示1到5点
       /n 	表示每隔n的单位执行一次,如第二段里,*/1,就表示每隔1个小时执行一次命令。也可以写成1-23/1
      
command:要执行的命令,可以是系统命令,也可以是自己编写的脚本文件

四: 编写配置文件

4.1 纸上谈兵

命令案例:

# 每1分钟执行一次myCommand
* * * * * myCommand


# 小时的第3和第15分钟执行
3,15 * * * * myCommand


# 在上午8点到11点的第3和第15分钟执行
3,15 8-11 * * * myCommand


# 每隔两天的上午8点到11点的第3和第15分钟执行
3,15 8-11 */2  *  * myCommand


# 每周一上午8点到11点的第3和第15分钟执行
3,15 8-11 * * 1 myCommand


# 每晚的21:30重启smb
30 21 * * * /etc/init.d/smb restart


# 每月1、10、22日的4 : 45重启smb
45 4 1,10,22 * * /etc/init.d/smb restart


# 每一小时重启smb
0 */1 * * * /etc/init.d/smb restart


# 晚上11点到早上7点之间,每隔一小时重启smb
0 23-7/1 * * * /etc/init.d/smb restart


# 每天18 : 00至23 : 00之间每隔30分钟重启smb
0,30 18-23 * * * /etc/init.d/smb restart

4.2 实践

前置条件:将这个jar包编写出来(请参考这篇博客

[root@localhost testjar]# 
[root@localhost testjar]# pwd
/root/testjar
[root@localhost testjar]# ls
crontabstest.log  helloCrontab-1.0-SNAPSHOT.jar  nohup.out  test.log
[root@localhost testjar]# crontab -e
no crontab for root - using an empty one
* * * * *  nohup java -jar ./testjar/helloCrontab-1.0-SNAPSHOT.jar >> ./testjar/crontabstest.log 2>&1
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"/tmp/crontab.lkmOgT" 1L, 102C written


[root@localhost testjar]# cat crontabstest.log 

nohup: 忽略输入
helloCrontab
helloCrontab
[root@localhost testjar]# date -R
Fri, 24 Dec 2021 16:42:17 +0800
[root@localhost testjar]# cat crontabstest.log 

nohup: 忽略输入
helloCrontab
helloCrontab
[root@localhost testjar]# date -R
Fri, 24 Dec 2021 16:44:48 +0800
[root@localhost testjar]# cat crontabstest.log 

nohup: 忽略输入
helloCrontab
helloCrontab
helloCrontab
helloCrontab
[root@localhost testjar]# 
[root@localhost testjar]# 
[root@localhost testjar]# date -R
Fri, 24 Dec 2021 16:48:00 +0800
[root@localhost testjar]# cd /var/spool/cron/
[root@localhost cron]# ls
root
[root@localhost cron]# cat root 
* * * * *  nohup java -jar ./testjar/helloCrontab-1.0-SNAPSHOT.jar >> ./testjar/crontabstest.log 2>&1
[root@localhost cron]# 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

?abc!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值