美文网首页
4-13-3 Linux中的计划作业 --- crontab-e

4-13-3 Linux中的计划作业 --- crontab-e

作者: 捌千里路雲和月 | 来源:发表于2021-11-09 19:45 被阅读0次
  • crontab-e 和 /etc/crontab 都能实现计划任务。它们之间存在一定的区别。

一、级别不同:

  • ①、crontab -e 是用户级别,每个用户都可以通过 crontab -e 的命令指定计划任务。

  • root 用户:

[root@localhost ~]# crontab -e    ##/ root 用户创建 crontab 任务

* * * * * date >> rootdate.txt    ## 每分钟输出 date 保存到家目录的 rootdate.txt 文件
                                                                                
~                                                                                                 
~                                                                                                 
~                                                                                                 
:wq    ## 保存退出

---- 输出结果:

[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root 203 Oct 17 22:42 rootdate.txt
[root@localhost ~]# 
[root@localhost ~]# cat rootdate.txt 
Sun Oct 17 22:36:01 CST 2021
Sun Oct 17 22:37:01 CST 2021
Sun Oct 17 22:38:01 CST 2021
Sun Oct 17 22:39:01 CST 2021
Sun Oct 17 22:40:02 CST 2021
Sun Oct 17 22:41:01 CST 2021
Sun Oct 17 22:42:01 CST 2021
[root@localhost ~]# 

  • torres 普通用户:
[torres@localhost ~]$ crontab -e    ## torres 普通用户创建 crontab 任务

* * * * * date >> torresdate.txt    ## 每分钟输出 date 保存到家目录的 torresdate.txt 文件                                                                                                                                                                                                                 
~                                                                                                                       
~                                                                                                                       
~                                                                                                                       
:wq    ## 保存退出

---- 输出结果:

[torres@localhost ~]$ ll
total 4
-rw-r--r--. 1 torres torres 232 Oct 17 22:45 torresdate.txt
[torres@localhost ~]$ cat torresdate.txt 
Sun Oct 17 22:38:01 CST 2021
Sun Oct 17 22:39:01 CST 2021
Sun Oct 17 22:40:02 CST 2021
Sun Oct 17 22:41:01 CST 2021
Sun Oct 17 22:42:01 CST 2021
Sun Oct 17 22:43:01 CST 2021
Sun Oct 17 22:44:01 CST 2021
[torres@localhost ~]$ 

  • ②、/etc/crontab 是系统级别,/etc/crontab 是一个文件,它的权限是 -rw-r--r--,拥有者是 root。只有 root 用户才能对它进行入写操作。 所属组 和 其他人 的用户都没有对 /etc/crontab 文件写入的权限。
[root@localhost ~]# 
[root@localhost ~]# ll /etc/crontab 
-rw-r--r--. 1 root root 486 Oct  9 01:22 /etc/crontab    ## /etc/crontab 权限 -rw-r--r--
[root@localhost ~]# 

  • root 用户可以通过 /etc/crontab 统筹多用户的计划任务。
[root@localhost ~]# 
[root@localhost ~]# vim /etc/crontab    ## root 用户编辑 /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

## root 用户每隔 2 分钟输出 date 到家目录下的 rootdate.txt 文件
*/2 * * * * root date >> rootdate.txt
## x 用户每隔 4 分钟输出 date 到家目录下的 xdate.txt 文件
*/4 * * * * x date >> /home/x/xdate.txt
## torres 用户每隔 6 分钟输出 date 到家目录下的 torresdate.txt 文件
*/6 * * * * torres date >> /home/torres/torresdate.txt
## user1 用户每隔 8 分钟输出 date 到家目录下的 user1date.txt 文件
*/8 * * * * user1 date >> /home/user1/user1date.txt

~                                                                                                 
:wq                                        
  • 输出结果:
## root 用户每 2 分钟输出一次 date 到 rootdate.txt 文件。
[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root 348 Oct 17 23:56 rootdate.txt
[root@localhost ~]# 
[root@localhost ~]# cat rootdate.txt 
Sun Oct 17 23:34:01 CST 2021
Sun Oct 17 23:36:01 CST 2021
Sun Oct 17 23:38:01 CST 2021
Sun Oct 17 23:40:01 CST 2021
Sun Oct 17 23:42:01 CST 2021
Sun Oct 17 23:44:01 CST 2021
Sun Oct 17 23:46:01 CST 2021
Sun Oct 17 23:48:01 CST 2021
Sun Oct 17 23:50:01 CST 2021
Sun Oct 17 23:52:01 CST 2021
Sun Oct 17 23:54:01 CST 2021
Sun Oct 17 23:56:01 CST 2021
[root@localhost ~]# 

## x 用户每 4 分钟输出一次 date 到 xdate.txt 文件。
[x@localhost ~]$ 
[x@localhost ~]$ ll
total 4
-rw-r--r--. 1 x x 174 Oct 17 23:56 xdate.txt
[x@localhost ~]$ 
[x@localhost ~]$ cat xdate.txt 
Sun Oct 17 23:36:01 CST 2021
Sun Oct 17 23:40:01 CST 2021
Sun Oct 17 23:44:01 CST 2021
Sun Oct 17 23:48:01 CST 2021
Sun Oct 17 23:52:01 CST 2021
Sun Oct 17 23:56:01 CST 2021
[x@localhost ~]$ 

## torres 用户每 6 分钟输出一次 date 到 torresdate.txt 文件。
[torres@localhost ~]$ ll
total 4
-rw-r--r--. 1 torres torres 116 Oct 17 23:54 torresdate.txt
[torres@localhost ~]$ 
[torres@localhost ~]$ cat torresdate.txt 
Sun Oct 17 23:36:01 CST 2021
Sun Oct 17 23:42:01 CST 2021
Sun Oct 17 23:48:01 CST 2021
Sun Oct 17 23:54:01 CST 2021
[torres@localhost ~]$ 

## user1 用户每 8 分钟输出一次 date 到 user1date.txt 文件。
[user1@localhost ~]$ ll
total 4
-rw-r--r--. 1 user1 user1 87 Oct 17 23:56 user1date.txt
[user1@localhost ~]$ 
[user1@localhost ~]$ cat user1date.txt 
Sun Oct 17 23:40:01 CST 2021
Sun Oct 17 23:48:01 CST 2021
Sun Oct 17 23:56:01 CST 2021
[user1@localhost ~]$ 


二、命令格式不一样。

  • ①、crontab 命令的格式:* * * * * command。

  • crontab -e 每个用户都可以执行的命令。除了 root 用户根据需要指定用户之外。其他用户执行 crontab -e 之后编写出来的计划任务只对自己有效。不需要指定那个用户执行。crontab -e 命令属于用户级别,制定计划任务相对独立。

  • 例:普通用户 torres 执行 crontab -e 只针对自身制定计划任务,其他用户不能干预 torres 指定的任务计划。root 除外,root 可以通过 -eu、-lu、-ru 参数对普通用户进行计划任务的编辑、查看 和 删除任务。

[torres@localhost ~]$ crontab -e    ## torres 普通用户创建 crontab 任务

* * * * * date >> torresdate.txt    ## 每分钟输出 date 保存到家目录的 torresdate.txt 文件 
~                                                                                                                       
~                                                                                                                       
~                                                                                                                       
:wq    ## 保存退出

---- 输出结果:

[torres@localhost ~]$ ll
total 4
-rw-r--r--. 1 torres torres 232 Oct 17 22:45 torresdate.txt    ## 生成的文件拥有者 torres,只有自身有写入的权限(root 除外)
[torres@localhost ~]$ cat torresdate.txt 
Sun Oct 17 22:38:01 CST 2021
Sun Oct 17 22:39:01 CST 2021
Sun Oct 17 22:40:02 CST 2021
Sun Oct 17 22:41:01 CST 2021
Sun Oct 17 22:42:01 CST 2021
Sun Oct 17 22:43:01 CST 2021
Sun Oct 17 22:44:01 CST 2021
[torres@localhost ~]$ 
  • 同理,root 用户只用 crontab -e 制定的计划任务也是针对 root 本身。
[root@localhost ~]# crontab -e    ##/ root 用户创建 crontab 任务

* * * * * date >> rootdate.txt    ## 每分钟输出 date 保存到家目录的 rootdate.txt 文件
                                                                                
~                                                                                                 
~                                                                                                 
~                                                                                                 
:wq    ## 保存退出

---- 输出结果:

[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root 203 Oct 17 22:42 rootdate.txt    ## 生成的文件拥有者 root,只有自身有写入的权限
[root@localhost ~]# 
[root@localhost ~]# cat rootdate.txt 
Sun Oct 17 22:36:01 CST 2021
Sun Oct 17 22:37:01 CST 2021
Sun Oct 17 22:38:01 CST 2021
Sun Oct 17 22:39:01 CST 2021
Sun Oct 17 22:40:02 CST 2021
Sun Oct 17 22:41:01 CST 2021
Sun Oct 17 22:42:01 CST 2021
[root@localhost ~]# 
  • 在有需要的情况下,root 可以通过 -eu、-lu、-ru 参数对普通用户进行计划任务的编辑、查看 和 删除任务。
## crontab -eu 指定用户,创建或编辑指定用户的 crontab 任务
[root@localhost ~]# crontab -eu torres    ## root 用户为 torres 用户制定计划任务
no crontab for torres - using an empty one

* * * * * date >> torresdate.txt                                                                            
~                                                                                      
~                                                                                      
~                                                                                      
:wq

## ---- root 用户为普通用户 torres 制定的 crontab 任务输出结果 ----

[torres@localhost ~]$ ll    ## torres 用户生成的文件
total 4
-rw-r--r--. 1 torres torres 87 Oct 18 16:37 torresdate.txt
[torres@localhost ~]$ 
[torres@localhost ~]$ cat torresdate.txt 
Mon Oct 18 16:35:01 CST 2021
Mon Oct 18 16:36:02 CST 2021
Mon Oct 18 16:37:01 CST 2021
[torres@localhost ~]$ 

## crontab -lu 指定用户,root 用户查看指定用户的计划任务
[root@localhost ~]# 
[root@localhost ~]# crontab -lu torres    ## root 用户查看 torres 用户的计划任务
* * * * * date >> torresdate.txt
[root@localhost ~]# 

## ---- torres 用户 -l 可以查看用 crontab 命令创建在自身的计划任务
[torres@localhost ~]$ 
[torres@localhost ~]$ crontab -l     ## torres 用户查看自身的计划任务
* * * * * date >> torresdate.txt
[torres@localhost ~]$ 

## crontab -ru 指定用户,root 用户删除指定用户的计划任务
[root@localhost ~]# 
[root@localhost ~]# crontab -ru torres    ## root 用户删除 torres 用户的计划任务
[root@localhost ~]# 
[root@localhost ~]# crontab -lu torres    ## 删除后,再查看 torres 计划任务,已显示没有任务
no crontab for torres
[root@localhost ~]# 

## ---- torres 用户查看自身 crontab 任务也已经显示没有任务
[torres@localhost ~]$ crontab -l
no crontab for torres
[torres@localhost ~]$ 

  • ②、/etc/crontab 文件的格式:
[root@localhost ~]# 
[root@localhost ~]# vim /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      ## <---- 命令格式样板
~                                                                                      
~                                                                                      
~                                                   
  • 命令格式: * * * * * user-name command to be executed,从命令模板能看到需要指明用户,也就是user-name 。如以下例子:
[root@localhost ~]# 
[root@localhost ~]# vim /etc/crontab    ## root 用户编辑 /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

## root 用户每个 2 分钟输出 date 到家目录下的 rootdate.txt 文件
*/2 * * * * root date >> rootdate.txt
## x 用户每个 4 分钟输出 date 到家目录下的 xdate.txt 文件
*/4 * * * * x date >> /home/x/xdate.txt
## torres 用户每个 6 分钟输出 date 到家目录下的 torresdate.txt 文件
*/6 * * * * torres date >> /home/torres/torresdate.txt
## user1 用户每个 8 分钟输出 date 到家目录下的 user1date.txt 文件
*/8 * * * * user1 date >> /home/user1/user1date.txt

~                                                                                                 
:wq                                     
  • /etc/crontab 的命令格式和 crontab -e 不同,crontab -e 以用户本身制定任务,所以不需要指明用户身份。而 /etc/crontab 由 root 统筹,在 /etc/crontab 的编辑上需要指明用户才可以编辑不同用户的计划任务,也就是 user-name(执行的用户)。

  • ③、/etc/crontab 制定的计划任务,只能查看 /etc/crontab 文件了解其内容,不能通过 crontab -l 查看。因为 /etc/crontab 是一个文件,crontab 是命令,通过 crontab -e 执行的计划任务可以用 crontab -l 查看。


三、语法检测不一样

  • crontab -e 有语法检测,如果语法格式不通过,它会在文件保存后给出提示并咨询是否通过 y or n 来继续进行编辑 或 退出编辑。
## 假设前面输 3 个 * 而不是 5 个 *
[root@localhost ~]# crontab -e
no crontab for root - using an empty one

* * * date >> rootdate.txt
                                                                           
~                                                                                      
~                                                                                      
~                                                                                      
:wq

crontab: installing new crontab
"/tmp/crontab.615m7n":1: bad month
errors in crontab file, can't install.      ## <---- 提示 crontab 有错误
Do you want to retry the same edit?     ## <---- 是否继续编辑,y(进入编辑),n(退出编辑)

Do you want to retry the same edit? y    ## <---- 输入 y 进入编辑界面

* * * date >> rootdate.txt
~                                                                                      
~


Do you want to retry the same edit? n    ## <---- 输入 n 退回用户界面
crontab: edits left in /tmp/crontab.615m7n
[root@localhost ~]# 
                          
  • /etc/crontab 文档的编辑没有语法检测,root 输入的内容都能够保存。但输入的指令如有错误不会作出提示,而是不会作正确输出。
[root@localhost ~]# ll    ## root 家目录现时没有任何文件
total 0
[root@localhost ~]# vim /etc/crontab    ## 编辑 /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
* * * * * date >> rootdate.txt    ## <---- 这里没有指明那个用户,语法上和模板不符。
~                                                                                      
~                                                                                      
~                                                                                      
~                                                                                      
~                                                                                      
:wq    ## <---- 仍然可以正常保存退出。      

[root@localhost ~]# 
[root@localhost ~]# ll    ## 这种情况没有输出
total 0
[root@localhost ~]# 

## -------------- 这种情况需要重新进入  /etc/crontab 修改正确的语句

[root@localhost ~]# vim /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
* * * * * root date >> rootdate.txt    ## <---- 指明执行的用户 root
~                                                                                      
~                                                                                      
~                                                                                      
~                                                                                      
~                                                                                      
:wq

[root@localhost ~]# 
[root@localhost ~]# ll    ## 指令正确才有输出
total 4
-rw-r--r--. 1 root root 58 Oct 19 09:25 rootdate.txt
[root@localhost ~]# 
[root@localhost ~]# cat rootdate.txt 
Tue Oct 19 09:24:01 CST 2021
Tue Oct 19 09:25:01 CST 2021
[root@localhost ~]# 
                                                                

相关文章

网友评论

      本文标题:4-13-3 Linux中的计划作业 --- crontab-e

      本文链接:https://www.haomeiwen.com/subject/kjymoltx.html