crontab工具来做计划任务,定时任务,执行某个脚本等等

1.检查是否已安装crontab    

# crontab

-bash: crontab: command not found

  

   执行 crontab 命令如果报 command not found,表明没有安装.

2.yum安装       

  #yum -y install vixie-cron

  

 1 Loaded plugins: security
 2 base                                                                                                                                                                    | 3.7 kB     00:00     
 3 epel                                                                                                                                                                    | 4.7 kB     00:00     
 4 epel/primary_db                                                                                                                                                         | 6.0 MB     00:00     
 5 extras                                                                                                                                                                  | 3.4 kB     00:00     
 6 extras/primary_db                                                                                                                                                       |  29 kB     00:00     
 7 updates                                                                                                                                                                 | 3.4 kB     00:00     
 8 updates/primary_db                                                                                                                                                      | 5.3 MB     00:00     
 9 Setting up Install Process
10 Resolving Dependencies
11 --> Running transaction check
12 ---> Package cronie.x86_64 0:1.4.4-16.el6_8.2 will be installed
13 --> Processing Dependency: dailyjobs for package: cronie-1.4.4-16.el6_8.2.x86_64
14 --> Processing Dependency: /usr/sbin/sendmail for package: cronie-1.4.4-16.el6_8.2.x86_64
15 --> Running transaction check
16 ---> Package cronie-anacron.x86_64 0:1.4.4-16.el6_8.2 will be installed
17 --> Processing Dependency: crontabs for package: cronie-anacron-1.4.4-16.el6_8.2.x86_64
18 ---> Package exim.x86_64 0:4.89-2.el6 will be installed
19 --> Running transaction check
20 ---> Package crontabs.noarch 0:1.10-33.el6 will be installed
21 --> Finished Dependency Resolution
22 
23 Dependencies Resolved
24 
25 ===============================================================================================================================================================================================
26  Package                                           Arch                                      Version                                             Repository                               Size
27 ===============================================================================================================================================================================================
28 Installing:
29  cronie                                            x86_64                                    1.4.4-16.el6_8.2                                    base                                     75 k
30 Installing for dependencies:
31  cronie-anacron                                    x86_64                                    1.4.4-16.el6_8.2                                    base                                     31 k
32  crontabs                                          noarch                                    1.10-33.el6                                         base                                     10 k
33  exim                                              x86_64                                    4.89-2.el6                                          epel                                    1.4 M
34 
35 Transaction Summary
36 ===============================================================================================================================================================================================
37 Install       4 Package(s)
38 
39 Total download size: 1.5 M
40 Installed size: 4.3 M
41 Downloading Packages:
42 (1/4): cronie-1.4.4-16.el6_8.2.x86_64.rpm                                                                                                                               |  75 kB     00:00     
43 (2/4): cronie-anacron-1.4.4-16.el6_8.2.x86_64.rpm                                                                                                                       |  31 kB     00:00     
44 (3/4): crontabs-1.10-33.el6.noarch.rpm                                                                                                                                  |  10 kB     00:00     
45 (4/4): exim-4.89-2.el6.x86_64.rpm                                                                                                                                       | 1.4 MB     00:00     
46 -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
47 Total                                                                                                                                                           10 MB/s | 1.5 MB     00:00     
48 Running rpm_check_debug
49 Running Transaction Test
50 Transaction Test Succeeded
51 Running Transaction
52   Installing : exim-4.89-2.el6.x86_64                                                                                                                                                      1/4 
53   Installing : cronie-1.4.4-16.el6_8.2.x86_64                                                                                                                                              2/4 
54   Installing : crontabs-1.10-33.el6.noarch                                                                                                                                                 3/4 
55   Installing : cronie-anacron-1.4.4-16.el6_8.2.x86_64                                                                                                                                      4/4 
56   Verifying  : crontabs-1.10-33.el6.noarch                                                                                                                                                 1/4 
57   Verifying  : cronie-anacron-1.4.4-16.el6_8.2.x86_64                                                                                                                                      2/4 
58   Verifying  : exim-4.89-2.el6.x86_64                                                                                                                                                      3/4 
59   Verifying  : cronie-1.4.4-16.el6_8.2.x86_64                                                                                                                                              4/4 
60 
61 Installed:
62   cronie.x86_64 0:1.4.4-16.el6_8.2                                                                                                                                                             
63 
64 Dependency Installed:
65   cronie-anacron.x86_64 0:1.4.4-16.el6_8.2                                crontabs.noarch 0:1.10-33.el6                                exim.x86_64 0:4.89-2.el6                               
66 
67 Complete!

安装完成后,可使用 info crontab 命令查看详细的帮助信息

cron服务提供crontab命令来设定cron服务的,以下是这个命令的一些参数与说明:

  crontab -u //设定某个用户的cron服务,一般root用户在执行这个命令的时候需要此参数

  crontab -l //列出某个用户cron服务的详细内容

  crontab -r //删除没个用户的cron服务

  crontab -e //编辑某个用户的cron服务

  比如说root查看自己的cron设置:crontab -u root -l

  再例如,root想删除fred的cron设置:crontab -u fred -r

  在编辑cron服务时,编辑的内容有一些格式和约定,输入:crontab -u root -e

crontab服务状态操作

  /sbin/service crond start //启动服务 
  /sbin/service crond stop //关闭服务 
  /sbin/service crond restart //重启服务 
  /sbin/service crond reload //重新载入配置 
  /sbin/service crond status //查看状态

或者使用

  # service crond start 
  # service crond stop 
  # service crond restart 
  # service crond reload 
  # service crond status

添加到开机服务

  在/etc/rc.d/rc.local这个脚本的末尾加上:

    /sbin/service crond start

 

 

 

Loaded plugins: securitybase                                                                                                                                                                    | 3.7 kB     00:00     epel                                                                                                                                                                    | 4.7 kB     00:00     epel/primary_db                                                                                                                                                         | 6.0 MB     00:00     extras                                                                                                                                                                  | 3.4 kB     00:00     extras/primary_db                                                                                                                                                       |  29 kB     00:00     updates                                                                                                                                                                 | 3.4 kB     00:00     updates/primary_db                                                                                                                                                      | 5.3 MB     00:00     Setting up Install ProcessResolving Dependencies–> Running transaction check—> Package cronie.x86_64 0:1.4.4-16.el6_8.2 will be installed–> Processing Dependency: dailyjobs for package: cronie-1.4.4-16.el6_8.2.x86_64–> Processing Dependency: /usr/sbin/sendmail for package: cronie-1.4.4-16.el6_8.2.x86_64–> Running transaction check—> Package cronie-anacron.x86_64 0:1.4.4-16.el6_8.2 will be installed–> Processing Dependency: crontabs for package: cronie-anacron-1.4.4-16.el6_8.2.x86_64—> Package exim.x86_64 0:4.89-2.el6 will be installed–> Running transaction check—> Package crontabs.noarch 0:1.10-33.el6 will be installed–> Finished Dependency Resolution
Dependencies Resolved
=============================================================================================================================================================================================== Package                                           Arch                                      Version                                             Repository                               Size===============================================================================================================================================================================================Installing: cronie                                            x86_64                                    1.4.4-16.el6_8.2                                    base                                     75 kInstalling for dependencies: cronie-anacron                                    x86_64                                    1.4.4-16.el6_8.2                                    base                                     31 k crontabs                                          noarch                                    1.10-33.el6                                         base                                     10 k exim                                              x86_64                                    4.89-2.el6                                          epel                                    1.4 M
Transaction Summary===============================================================================================================================================================================================Install       4 Package(s)
Total download size: 1.5 MInstalled size: 4.3 MDownloading Packages:(1/4): cronie-1.4.4-16.el6_8.2.x86_64.rpm                                                                                                                               |  75 kB     00:00     (2/4): cronie-anacron-1.4.4-16.el6_8.2.x86_64.rpm                                                                                                                       |  31 kB     00:00     (3/4): crontabs-1.10-33.el6.noarch.rpm                                                                                                                                  |  10 kB     00:00     (4/4): exim-4.89-2.el6.x86_64.rpm                                                                                                                                       | 1.4 MB     00:00     ———————————————————————————————————————————————————————————————–Total                                                                                                                                                           10 MB/s | 1.5 MB     00:00     Running rpm_check_debugRunning Transaction TestTransaction Test SucceededRunning Transaction  Installing : exim-4.89-2.el6.x86_64                                                                                                                                                      1/4   Installing : cronie-1.4.4-16.el6_8.2.x86_64                                                                                                                                              2/4   Installing : crontabs-1.10-33.el6.noarch                                                                                                                                                 3/4   Installing : cronie-anacron-1.4.4-16.el6_8.2.x86_64                                                                                                                                      4/4   Verifying  : crontabs-1.10-33.el6.noarch                                                                                                                                                 1/4   Verifying  : cronie-anacron-1.4.4-16.el6_8.2.x86_64                                                                                                                                      2/4   Verifying  : exim-4.89-2.el6.x86_64                                                                                                                                                      3/4   Verifying  : cronie-1.4.4-16.el6_8.2.x86_64                                                                                                                                              4/4 
Installed:  cronie.x86_64 0:1.4.4-16.el6_8.2                                                                                                                                                             
Dependency Installed:  cronie-anacron.x86_64 0:1.4.4-16.el6_8.2                                crontabs.noarch 0:1.10-33.el6                                exim.x86_64 0:4.89-2.el6                               
Complete!

版权声明:本文为dxy1451713982原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:http://www.cnblogs.com/dxy1451713982/p/8081569.html