4-13-1 Linux中的计划作业 --- crontab(二

2021-10-26  本文已影响0人  捌千里路雲和月
[root@localhost ~]# vim /etc/crontab    ## 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@localhost ~]# 
[root@localhost ~]# vim /etc/crontab    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 >> mydate.txt    ## <---- 输入的内容                                                                  
~                                                                                                      
~                                                                                                      
~                                                                                                      
:wq                                        
* * * * * root date >> mydate.txt 命令图解
[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root 522 Oct  8 13:30 mydate.txt
[root@localhost ~]# 
[root@localhost ~]# cat mydate.txt 
Fri Oct  8 13:13:01 CST 2021
Fri Oct  8 13:14:01 CST 2021
Fri Oct  8 13:15:02 CST 2021
Fri Oct  8 13:16:01 CST 2021
Fri Oct  8 13:17:01 CST 2021
Fri Oct  8 13:18:01 CST 2021
Fri Oct  8 13:19:01 CST 2021
Fri Oct  8 13:20:01 CST 2021
Fri Oct  8 13:21:01 CST 2021
Fri Oct  8 13:22:01 CST 2021
Fri Oct  8 13:23:01 CST 2021
Fri Oct  8 13:24:01 CST 2021
Fri Oct  8 13:25:01 CST 2021
Fri Oct  8 13:26:01 CST 2021
Fri Oct  8 13:27:01 CST 2021
Fri Oct  8 13:28:01 CST 2021
Fri Oct  8 13:29:01 CST 2021
Fri Oct  8 13:30:01 CST 2021
[root@localhost ~]# 

## crontab -l 查看不到 /etc/cronta 配置文件制定的 crontab 计划任务
[root@localhost ~]# crontab -l    
no crontab for root
[root@localhost ~]# 


[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
* * * * * root date >> mydate.txt
*/5 * * * * torres touch root-directory     ## <---- 输入的内容   
~                                                                                      
~                                                                                      
~                                                                                      
~                                                                                      
:wq                                          
[torres@localhost ~]$ ll
total 0
-rw-r--r--. 1 torres torres 0 Oct  8 15:15 root-directory
[torres@localhost ~]$ ll
total 0
-rw-r--r--. 1 torres torres 0 Oct  8 15:20 root-directory
[torres@localhost ~]$ ll
total 0
-rw-r--r--. 1 torres torres 0 Oct  8 15:25 root-directory
[torres@localhost ~]$ ll
total 0
-rw-r--r--. 1 torres torres 0 Oct  8 15:30 root-directory
[torres@localhost ~]$ 


③、用脚本形式制定 user1 普通用户每 2 分钟输出一次 date 到 user1date.txt 文件。

[root@localhost ~]# 
[root@localhost ~]# vim user1.sh    ## vim 编辑 user1.sh 脚本    

date >> user1date.txt    ## 脚本内容:输出 date 到 user1date.txt 文件                                                                              
~                                                                                                 
~                                                                                                 
~                                                                                                 
:wq!       
                   
[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root 22 Oct 11 16:25 user1.sh      ## 脚本文件
[root@localhost ~]# 
         
[root@localhost ~]# 
[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root 22 Oct 11 16:25 user1.sh
[root@localhost ~]# 
[root@localhost ~]# chmod 755 user1.sh 
[root@localhost ~]# 
[root@localhost ~]# ll
total 4
-rwxr-xr-x. 1 root root 22 Oct 11 16:25 user1.sh    # 拥有者、所属组、其他人都具有可执行权限
[root@localhost ~]# 

[root@localhost ~]# 
[root@localhost ~]# vim /etc/crontab    ## 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
*/2 * * * * user1 /root/user1.sh    ## user1 用户每两分钟调用一次 root 的 user1.sh 脚本
~                                                                                      
~                                                                                      
~                                                                                      
:wq!    ## 保存并退出                                
user1@localhost ~]$ 
[user1@localhost ~]$ ll    ## user1 家目录没有文件
total 0
[user1@localhost ~]$ 

## 每分钟按 enter 回车键都会收到提示信息
[root@localhost ~]# 
You have new mail in /var/spool/mail/root
[root@localhost ~]# 
You have new mail in /var/spool/mail/root
[root@localhost ~]# 
You have new mail in /var/spool/mail/root
[root@localhost ~]# 

[root@localhost ~]# 
[root@localhost ~]# tail /var/spool/mail/root 
X-Cron-Env: <PATH=/sbin:/bin:/usr/sbin:/usr/bin>
X-Cron-Env: <MAILTO=root>
X-Cron-Env: <HOME=/home/user1>
X-Cron-Env: <LOGNAME=user1>
X-Cron-Env: <USER=user1>
Message-Id: <20211011090001.97AD860EC912@localhost.localdomain>
Date: Mon, 11 Oct 2021 17:00:01 +0800 (CST)

/bin/bash: /root/user1.sh: Permission denied    ## 没有 /root/user1.sh 权限

[root@localhost ~]# 

[root@localhost ~]# ll
total 4
-rwxr-xr-x. 1 root root 22 Oct 11 16:25 user1.sh
[root@localhost ~]# pwd
/root
[root@localhost ~]# 
[root@localhost ~]# ll /
total 20
lrwxrwxrwx.   1 root root    7 Mar 22  2021 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 Jun 17 09:37 boot
drwxr-xr-x.   3 root root   20 Mar 18  2021 data
drwxr-xr-x.  19 root root 3160 Oct 11 08:32 dev
drwxr-xr-x.  90 root root 8192 Oct 11 17:03 etc
drwxr-xr-x.   2 root root    6 Jun 29 11:22 fdfsd
drwxr-xr-x.   5 root root   42 Oct 11 15:21 home
lrwxrwxrwx.   1 root root    7 Mar 22  2021 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 Mar 22  2021 lib64 -> usr/lib64
drwxr-xr-x.   3 root root   19 Apr  2  2021 media
drwxr-xr-x.   5 root root   42 Mar 22  2021 mnt
drwxr-xr-x.   5 root root   45 Jul 23 11:09 opt
dr-xr-xr-x. 151 root root    0 Oct 11 08:31 proc
dr-xr-x---.   3 root root  187 Oct 11 17:03 root    ## <---- root 目录的其他人项中没有任何权限
drwxr-xr-x.  25 root root  720 Oct 11 08:32 run
lrwxrwxrwx.   1 root root    8 Mar 22  2021 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 Apr 11  2018 srv
dr-xr-xr-x.  13 root root    0 Oct 11 08:31 sys
drwxr-xr-x.   2 root root    6 Jun 29 11:26 test目录分别创建
drwxr-xr-x.   2 root root    6 Jun 29 11:27 test目录分别创建demo1,demo2,demo3三个文件夹
drwxrwxrwt.  12 root root 4096 Oct 11 17:06 tmp
drwxr-xr-x.  13 root root  155 Mar 22  2021 usr
drwxr-xr-x.  19 root root  267 Mar 22  2021 var
drwxr-xr-x.   2 root root    6 Jun 29 11:22 新建一个test文件夹
[root@localhost ~]# 

[user1@localhost ~]$
[user1@localhost ~]$ ll /root/
ls: cannot open directory /root/: Permission denied
[user1@localhost ~]$ 

[root@localhost ~]# 
[root@localhost ~]# ll
total 4
-rwxr-xr-x. 1 root root 22 Oct 11 16:25 user1.sh
[root@localhost ~]# 
[root@localhost ~]# mv user1.sh /home/user1/    ## 剪贴 user1.sh 到 /home/user1/ 目录下
[root@localhost ~]# 
[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
*/2 * * * * user1 /home/user1/user1.sh    ## 修改 user1.sh 的路径
~                                                                                      
~                                                                                      
~                                                                                      
~                                                                                      
~                                                                                      
:wq    ## 保存并退出
                               
## user1 用户
[user1@localhost ~]$ 
[user1@localhost ~]$ ll
total 8
-rw-r--r--. 1 user1 user1 29 Oct 11 17:10 user1date.txt
-rwxr-xr-x. 1 root  root  22 Oct 11 16:25 user1.sh
[user1@localhost ~]$ 
[user1@localhost ~]$ cat user1date.txt 
Mon Oct 11 17:10:01 CST 2021
Mon Oct 11 17:12:01 CST 2021
[user1@localhost ~]$ 

## root 用户
[root@localhost ~]# 
[root@localhost ~]# ll /home/user1/
total 8
-rw-r--r--. 1 user1 user1 58 Oct 11 17:12 user1date.txt
-rwxr-xr-x. 1 root  root  22 Oct 11 16:25 user1.sh
[root@localhost ~]# 
[root@localhost ~]# cat /home/user1/user1date.txt 
Mon Oct 11 17:10:01 CST 2021
Mon Oct 11 17:12:01 CST 2021
[root@localhost ~]# 


④、同样的原理,普通用户编写的脚本文件,root 用户编辑 /etc/crontab 时也可以调用脚本文件。

[user1@localhost ~]$ ll
total 0
[user1@localhost ~]$ vim user1.sh

echo "i am user1" >> /home/user1/user1.txt    ## 输入的内容
                                                                                                                                                                   
~                                                                                      
~                                                                                      
~                                                                                      
:wq    ## 保存并退出

[user1@localhost ~]$ ll
total 4
-rw-rw-r--. 1 user1 user1 41 Oct 12 11:39 user1.sh    ## 生成的 user1.sh 文件
[user1@localhost ~]$ 

[user1@localhost ~]$ 
[user1@localhost ~]$ vim /etc/crontab    ## user1 用户编辑  /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
* * * * * user1 /home/user1/user1.sh
~                                                                                      
~                                                                                      
~                                                                                      
~                                                                                      
~                                                                                      
"/etc/crontab"
"/etc/crontab" E212: Can't open file for writing    ## 保存的时候提示无法打开文件写入,只能 q! 退出 
Press ENTER or type command to continue
                  
[user1@localhost ~]$ 
[user1@localhost ~]$ ll /etc/crontab 
-rw-r--r--. 1 root root 488 Oct 12 11:46 /etc/crontab    ## 除 root 外,其他人没有写 /etc/crontab 的权限
[user1@localhost ~]$ 

[root@localhost ~]# ll /home/user1/
total 4
-rw-rw-r--. 1 user1 user1 41 Oct 12 11:39 user1.sh
[root@localhost ~]# 
[root@localhost ~]# chmod u+x /home/user1/user1.sh    ## user1.sh 文件拥有者添加 x 执行权限
[root@localhost ~]# 
[root@localhost ~]# ll /home/user1/user1.sh 
-rwxrw-r--. 1 user1 user1 41 Oct 12 11:39 /home/user1/user1.sh

[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
* * * * * user1 /home/user1/user1.sh    ## 执行用户是 user1,调用 /home/user1/user1.sh 脚本

~                                                                                      
~                                                                                      
~                                                                                      
~                                                                                      
:wq    ## 保存并退出                              
[user1@localhost ~]$ ll
total 8
-rwxrw-r--. 1 user1 user1 43 Oct 12 13:48 user1.sh
-rw-r--r--. 1 user1 user1 33 Oct 12 13:54 user1.txt
[user1@localhost ~]$ 
[user1@localhost ~]$ cat user1.txt
i am user1
i am user1
i am user1
[user1@localhost ~]$ 


⑤、添加脚本的路径到 /etc/crontab 的环境变量,制定执行命令时不需要写全路径。

[root@localhost ~]# 
[root@localhost ~]# vim rootdate.sh    ## root 用户创建 rootdate.sh 脚本

date >> rootdate.txt    ## 内容:输出 date 到 rootdate.txt 文件                                                                
~                                                                                      
~                                                                                      
~                                                                                      
:wq    ## 保存并退出

[root@localhost ~]# 
[root@localhost ~]# vim /etc/crontab    ## 编辑  /etc/crontab 文件

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/root/    ## 把脚本文件的父路径写到环境变量中,:/root/
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 rootdate.sh    ## 脚本的父路径已经添加到环境变量,这里只需要写脚本名。
~                                                                                      
~                                                                                      
~                                                                                      
~                                                                                      
~                                                                                      
:wq    ## 保存并退出                         
[root@localhost ~]# 
[root@localhost ~]# ll
total 8
-rwxr--r--. 1 root root  21 Oct 13 08:56 rootdate.sh
-rw-r--r--. 1 root root 319 Oct 13 09:08 rootdate.txt    ## 生成的 rootdate.txt    
[root@localhost ~]# 
[root@localhost ~]# cat rootdate.txt    ## rootdate.txt 内容 
Wed Oct 13 08:58:01 CST 2021
Wed Oct 13 08:59:01 CST 2021
Wed Oct 13 09:00:01 CST 2021
Wed Oct 13 09:01:01 CST 2021
Wed Oct 13 09:02:01 CST 2021
Wed Oct 13 09:03:02 CST 2021
Wed Oct 13 09:04:01 CST 2021
Wed Oct 13 09:05:01 CST 2021
Wed Oct 13 09:06:01 CST 2021
Wed Oct 13 09:07:01 CST 2021
Wed Oct 13 09:08:01 CST 2021
Wed Oct 13 09:09:01 CST 2021
[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 用户每分钟输出 date 到 rootdate.txt

* * * * * user1 date >> user1date.txt     ## <---- user1 用户每分钟输出 date 到 user1date.txt

* * * * * torres date >> torresdate.txt    ## <---- torres 用户每分钟输出 date 到 torresdate.txt

* * * * * x date >> xdate.txt     ## <---- x 用户每分钟输出 date 到 xdate.txt

:wq!                                                         

⑦、用脚本完成输出数据到不同用户的家目录。

## root 用户脚本
[root@localhost ~]# vim rootdate.sh

date >> rootdate.txt    ## 输出 date 到 root 用户家目录下,生成 rootdate.txt 记录数据                                                                        
~                                                                                      
~                                                                                      
~                                                                                      
:wq!                  
## user1 用户脚本
[root@localhost ~]# vim user1date.sh

date >> /home/user1/user1date.txt     ## 输出 date 到 user1 用户家目录下,生成 user1date.txt 记录数据                                                          
~                                                                                      
~                                                                                      
~                                                                                      
:wq!                                 
## torres 用户脚本
[root@localhost ~]# vim torresdate.sh

date >> /home/torres/torresdate.txt    ## 输出 date 到 torres 用户家目录下,生成 torresdate.txt 记录数据                                                                
~                                                                                      
~                                                                                      
~                                                                                      
:wq!

## x 用户脚本
[root@localhost ~]# vim xdate.sh    

date >> /home/x/xdate.txt    ## 输出 date 到 x 用户家目录下,生成 xdate.txt 记录数据                                                                  
~                                                                                      
~                                                                                      
~                                                                                      
:wq!

[root@localhost ~]# 
[root@localhost ~]# ll
total 16
-rw-r--r--. 1 root root 21 Oct 13 15:34 rootdate.sh
-rw-r--r--. 1 root root 23 Oct 13 15:35 torresdate.sh
-rw-r--r--. 1 root root 22 Oct 13 15:35 user1date.sh
-rw-r--r--. 1 root root 18 Oct 13 15:36 xdate.sh
[root@localhost ~]# 

## 所有脚本权限 u 拥有者添加 x 执行权限
[root@localhost ~]# 
[root@localhost ~]# chmod u+x rootdate.sh    ## 添加文件拥有者 x 可执行权限 
[root@localhost ~]# chmod u+x torresdate.sh 
[root@localhost ~]# chmod u+x user1date.sh 
[root@localhost ~]# chmod u+x xdate.sh 
[root@localhost ~]# 
[root@localhost ~]# ll
total 16
-rwxr--r--. 1 root root 21 Oct 13 15:34 rootdate.sh    ## 拥有者已具有可执行权限
-rwxr--r--. 1 root root 23 Oct 13 15:35 torresdate.sh
-rwxr--r--. 1 root root 22 Oct 13 15:35 user1date.sh
-rwxr--r--. 1 root root 18 Oct 13 15:36 xdate.sh
[root@localhost ~]# 

[root@localhost ~]# 
[root@localhost ~]# vim /etc/crontab 

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/root/    ## 脚本的父路径添加到环境变量中 :/root/
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 rootdate.sh    ## 只需写脚本文件名
* * * * * root torresdate.sh
* * * * * root user1date.sh
* * * * * root xdate.sh
~                                                                                      
~                                                                                      
:wq    ## 保存并退出      
                  
## root 家目录
[root@localhost ~]# ll
total 20
-rwxr--r--. 1 root root  21 Oct 13 15:34 rootdate.sh
-rw-r--r--. 1 root root 174 Oct 13 15:59 rootdate.txt
-rwxr--r--. 1 root root  36 Oct 13 15:45 torresdate.sh
-rwxr--r--. 1 root root  34 Oct 13 15:45 user1date.sh
-rwxr--r--. 1 root root  26 Oct 13 15:46 xdate.sh
[root@localhost ~]# 
[root@localhost ~]# cat rootdate.txt 
Wed Oct 13 15:54:01 CST 2021
Wed Oct 13 15:55:01 CST 2021
Wed Oct 13 15:56:01 CST 2021
Wed Oct 13 15:57:02 CST 2021
Wed Oct 13 15:58:01 CST 2021
Wed Oct 13 15:59:01 CST 2021
Wed Oct 13 16:00:01 CST 2021
Wed Oct 13 16:01:01 CST 2021
[root@localhost ~]# 

## user1 家目录
[user1@localhost ~]$ 
[user1@localhost ~]$ ll
total 4
-rw-r--r--. 1 root root 232 Oct 13 16:01 user1date.txt
[user1@localhost ~]$ 
[user1@localhost ~]$ cat user1date.txt 
Wed Oct 13 15:54:01 CST 2021
Wed Oct 13 15:55:01 CST 2021
Wed Oct 13 15:56:01 CST 2021
Wed Oct 13 15:57:02 CST 2021
Wed Oct 13 15:58:01 CST 2021
Wed Oct 13 15:59:01 CST 2021
Wed Oct 13 16:00:01 CST 2021
Wed Oct 13 16:01:01 CST 2021
[user1@localhost ~]$ 

## torres 家目录
[torres@localhost ~]$ ll
total 4
-rw-r--r--. 1 root root 232 Oct 13 16:01 torresdate.txt
[torres@localhost ~]$ 
[torres@localhost ~]$ cat torresdate.txt 
Wed Oct 13 15:54:01 CST 2021
Wed Oct 13 15:55:01 CST 2021
Wed Oct 13 15:56:01 CST 2021
Wed Oct 13 15:57:02 CST 2021
Wed Oct 13 15:58:01 CST 2021
Wed Oct 13 15:59:01 CST 2021
Wed Oct 13 16:00:01 CST 2021
Wed Oct 13 16:01:01 CST 2021
[torres@localhost ~]$ 

## x 家目录
[x@localhost ~]$ ll
total 4
-rw-r--r--. 1 root root 232 Oct 13 16:01 xdate.txt
[x@localhost ~]$ 
[x@localhost ~]$ cat xdate.txt 
Wed Oct 13 15:54:01 CST 2021
Wed Oct 13 15:55:01 CST 2021
Wed Oct 13 15:56:01 CST 2021
Wed Oct 13 15:57:02 CST 2021
Wed Oct 13 15:58:01 CST 2021
Wed Oct 13 15:59:01 CST 2021
Wed Oct 13 16:00:01 CST 2021
Wed Oct 13 16:01:01 CST 2021
[x@localhost ~]$ 


⑧、root 用户制定备份各用户数据。

[root@localhost ~]# 
[root@localhost ~]# mkdir backup_dates    ## 备份目录
[root@localhost ~]# 
[root@localhost ~]# cd backup_dates/
[root@localhost backup_dates]# 
[root@localhost backup_dates]# mkdir root_dates    ## 各用户资料的备份目录
[root@localhost backup_dates]# mkdir user1_dates
[root@localhost backup_dates]# mkdir torres_dates
[root@localhost backup_dates]# mkdir x_dates
[root@localhost backup_dates]# 
[root@localhost backup_dates]# ll
total 0
drwxr-xr-x. 2 root root 6 Oct 13 16:13 root_dates
drwxr-xr-x. 2 root root 6 Oct 13 16:13 torres_dates
drwxr-xr-x. 2 root root 6 Oct 13 16:13 user1_dates
drwxr-xr-x. 2 root root 6 Oct 13 16:13 x_dates
[root@localhost backup_dates]# 

[root@localhost ~]# 
[root@localhost ~]# vim backdates.sh

## root 家目录下的所有文件同步到 /root/backup_dates/root_dates 目录。
rsync -a /root/* /root/backup_dates/root_dates 

## user1 家目录下的所有文件同步到 /root/backup_dates/user1_dates    
rsync -a /home/user1/* /root/backup_dates/user1_dates

## torres 家目录下的所有文件同步到 /root/backup_dates/torres_dates
rsync -a /home/torres/* /root/backup_dates/torres_dates

## x 家目录下的所有文件同步到 /root/backup_dates/x_dates
rsync -a /home/x/* /root/backup_dates/x_dates                                                                    
~                                                                                      
~                                                                                      
~                                                                                      
:wq    ## 保存并退出                    
[root@localhost ~]# 
[root@localhost ~]# vim /etc/crontab 

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/root/
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 rootdate.sh
* * * * * root torresdate.sh
* * * * * root user1date.sh
* * * * * root xdate.sh
0 17 13 10 * root backdates.sh    ## 10 月 13 日 17 点整调用 backdates.sh 脚本
~                                                                                      

:wq    ## 保存并退出                                    
[root@localhost backup_dates]# 
[root@localhost backup_dates]# ll root_dates/    ## root 用户家目录的备份数据
total 24
-rwxr--r--. 1 root root  203 Oct 13 16:42 backdates.sh
drwxr-xr-x. 6 root root   78 Oct 13 16:13 backup_dates
-rwxr--r--. 1 root root   21 Oct 13 15:34 rootdate.sh
-rw-r--r--. 1 root root 1943 Oct 13 17:00 rootdate.txt
-rwxr--r--. 1 root root   36 Oct 13 15:45 torresdate.sh
-rwxr--r--. 1 root root   34 Oct 13 15:45 user1date.sh
-rwxr--r--. 1 root root   26 Oct 13 15:46 xdate.sh
[root@localhost backup_dates]# 
[root@localhost backup_dates]# ll torres_dates/    ## torres 用户家目录的备份数据
total 4
-rw-r--r--. 1 root root 1943 Oct 13 17:00 torresdate.txt
[root@localhost backup_dates]# 
[root@localhost backup_dates]# ll user1_dates/    ## user1 用户家目录的备份数据
total 4
-rw-r--r--. 1 root root 1943 Oct 13 17:00 user1date.txt
[root@localhost backup_dates]# 
[root@localhost backup_dates]# ll x_dates/     ## x 用户家目录的备份数据
total 4
-rw-r--r--. 1 root root 1943 Oct 13 17:00 xdate.txt
[root@localhost backup_dates]# 

⑨、run-parts,读取目录下所有可执行文件。

[root@localhost ~]# mkdir sh    ## 创建 sh 目录
[root@localhost ~]# 
[root@localhost ~]# cd sh    ## 进入 sh 目录
[root@localhost sh]# 
[root@localhost sh]# vim rootdate.sh    ## 编辑 rootdate.sh 的脚本 

date >> /root/rootdate.txt    ## 输出 date 到  /root/rootdate.txt 文件                                                
~                                                                                      
~                                                                                      
~                                                                                      
:wq    ## 保存并退出                    
[root@localhost sh]# vim user1date.sh

date >> /home/user1/user1date.txt                                                                   
~                                                                                      
~                                                                                      
~                                                                                      
:wq                                     
[root@localhost sh]# vim torresdate.sh

date >> /home/torres/torresdate.txt                                               
~                                                                                      
~                                                                                      
~                                                                                      
:wq                                        
[root@localhost sh]# vim xdate.sh

date >> /home/x/xdate.txt                                     
~                                                                                      
~                                                                                      
~                                                                                      
:wq                           
[root@localhost sh]# 
[root@localhost sh]# chmod u+x rootdate.sh 
[root@localhost sh]# chmod u+x user1date.sh 
[root@localhost sh]# chmod u+x torresdate.sh 
[root@localhost sh]# chmod u+x xdate.sh 
[root@localhost sh]# 
[root@localhost sh]# ll
total 16
-rwxr--r--. 1 root root 27 Oct 15 09:24 rootdate.sh
-rwxr--r--. 1 root root 36 Oct 15 09:26 torresdate.sh
-rwxr--r--. 1 root root 34 Oct 15 09:25 user1date.sh
-rwxr--r--. 1 root root 26 Oct 15 09:26 xdate.sh
[root@localhost sh]# 

[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 run-parts /root/sh    ## run-parts 调用 /root/sh 目录下的所有执行文件
~                                                                                      
~                                                                                      
~                                                                                      
~                                                                                      
~                                                                                      
:wq                                     
[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root 58 Oct 15 09:32 rootdate.txt
drwxr-xr-x. 2 root root 82 Oct 15 09:26 sh
[root@localhost ~]# 
[root@localhost ~]# cat rootdate.txt 
Fri Oct 15 09:31:01 CST 2021
Fri Oct 15 09:32:01 CST 2021
Fri Oct 15 09:33:01 CST 2021
Fri Oct 15 09:34:01 CST 2021
Fri Oct 15 09:35:01 CST 2021
Fri Oct 15 09:36:02 CST 2021
Fri Oct 15 09:37:01 CST 2021
Fri Oct 15 09:38:01 CST 2021
Fri Oct 15 09:39:01 CST 2021
Fri Oct 15 09:40:01 CST 2021
[root@localhost ~]# 

[torres@localhost ~]$ ll
total 4
-rw-r--r--. 1 root root 58 Oct 15 09:32 torresdate.txt
[torres@localhost ~]$ cat torresdate.txt 
Fri Oct 15 09:31:01 CST 2021
Fri Oct 15 09:32:01 CST 2021
Fri Oct 15 09:33:01 CST 2021
Fri Oct 15 09:34:01 CST 2021
Fri Oct 15 09:35:01 CST 2021
Fri Oct 15 09:36:02 CST 2021
Fri Oct 15 09:37:01 CST 2021
Fri Oct 15 09:38:01 CST 2021
Fri Oct 15 09:39:01 CST 2021
Fri Oct 15 09:40:01 CST 2021
[torres@localhost ~]$ 

[user1@localhost ~]$ ll
total 4
-rw-r--r--. 1 root root 58 Oct 15 09:32 user1date.txt
[user1@localhost ~]$ cat user1date.txt 
Fri Oct 15 09:31:01 CST 2021
Fri Oct 15 09:32:01 CST 2021
Fri Oct 15 09:33:01 CST 2021
Fri Oct 15 09:34:01 CST 2021
Fri Oct 15 09:35:01 CST 2021
Fri Oct 15 09:36:02 CST 2021
Fri Oct 15 09:37:01 CST 2021
Fri Oct 15 09:38:01 CST 2021
Fri Oct 15 09:39:01 CST 2021
Fri Oct 15 09:40:01 CST 2021
[user1@localhost ~]$ 

[x@localhost ~]$ ll
total 4
-rw-r--r--. 1 root root 58 Oct 15 09:32 xdate.txt
[x@localhost ~]$ cat xdate.txt 
Fri Oct 15 09:31:01 CST 2021
Fri Oct 15 09:32:01 CST 2021
Fri Oct 15 09:33:01 CST 2021
Fri Oct 15 09:34:01 CST 2021
Fri Oct 15 09:35:01 CST 2021
Fri Oct 15 09:36:02 CST 2021
Fri Oct 15 09:37:01 CST 2021
Fri Oct 15 09:38:01 CST 2021
Fri Oct 15 09:39:01 CST 2021
Fri Oct 15 09:40:01 CST 2021
[x@localhost ~]$ 

上一篇下一篇

猜你喜欢

热点阅读