博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CentOS下使用yum命令安装计划任务程序crontab
阅读量:6989 次
发布时间:2019-06-27

本文共 9711 字,大约阅读时间需要 32 分钟。

概述

利用“任务计划”,可以将任何脚本、程序或文档安排在某个最方便的时间运行。通俗的讲就是定时执行某个脚本、程序。

常见用途

定时关机

定时检查更新
定时对系统配置、数据库、日志进行备份
定时清理垃圾文件

检测是否已安装

# crontab-bash: crontab: command not found

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

开始安装

# yum -y install vixie-cronLoaded plugins: fastestmirror, refresh-packagekit, securityExisting lock /var/run/yum.pid: another copy is running as pid 25960.Another app is currently holding the yum lock; waiting for it to exit...

可能是系统自动升级正在运行,yum在锁定状态中。

可以通过强制关掉yum进程:
#rm -f /var/run/yum.pid
然后就可以使用yum了。

# yum -y install vixie-cronLoaded plugins: fastestmirror, refresh-packagekit, securityDetermining fastest mirrors * base: mirrors.btte.net * extras: mirrors.sina.cn * updates: mirrors.sina.cnbase                                                                                                                                                                    | 3.7 kB     00:00     extras                                                                                                                                                                  | 3.4 kB     00:00     extras/primary_db                                                                                                                                                       |  32 kB     00:00     updates                                                                                                                                                                 | 3.4 kB     00:00     updates/primary_db                                                                                                                                                      | 1.9 MB     00:07     Setting up Install ProcessResolving Dependencies--> Running transaction check---> Package cronie.x86_64 0:1.4.4-15.el6 will be installed--> Processing Dependency: dailyjobs for package: cronie-1.4.4-15.el6.x86_64--> Processing Dependency: /usr/sbin/sendmail for package: cronie-1.4.4-15.el6.x86_64--> Running transaction check---> Package cronie-anacron.x86_64 0:1.4.4-15.el6 will be installed--> Processing Dependency: crontabs for package: cronie-anacron-1.4.4-15.el6.x86_64---> Package sendmail.x86_64 0:8.14.4-9.el6 will be installed--> Processing Dependency: procmail for package: sendmail-8.14.4-9.el6.x86_64--> Running transaction check---> Package crontabs.noarch 0:1.10-33.el6 will be installed---> Package procmail.x86_64 0:3.22-25.1.el6_5.1 will be installed--> Finished Dependency ResolutionDependencies Resolved=============================================================================================================================================================================================== Package                                          Arch                                     Version                                                Repository                              Size===============================================================================================================================================================================================Installing: cronie                                           x86_64                                   1.4.4-15.el6                                           base                                    74 kInstalling for dependencies: cronie-anacron                                   x86_64                                   1.4.4-15.el6                                           base                                    31 k crontabs                                         noarch                                   1.10-33.el6                                            base                                    10 k procmail                                         x86_64                                   3.22-25.1.el6_5.1                                      base                                   162 k sendmail                                         x86_64                                   8.14.4-9.el6                                           base                                   716 kTransaction Summary===============================================================================================================================================================================================Install       5 Package(s)Total download size: 994 kInstalled size: 2.1 MDownloading Packages:(1/5): cronie-1.4.4-15.el6.x86_64.rpm                                                                                                                                   |  74 kB     00:00     (2/5): cronie-anacron-1.4.4-15.el6.x86_64.rpm                                                                                                                           |  31 kB     00:00     (3/5): crontabs-1.10-33.el6.noarch.rpm                                                                                                                                  |  10 kB     00:00     (4/5): procmail-3.22-25.1.el6_5.1.x86_64.rpm                                                                                                                            | 162 kB     00:00     (5/5): sendmail-8.14.4-9.el6.x86_64.rpm                                                                                                                                 | 716 kB     00:00     -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Total                                                                                                                                                          1.3 MB/s | 994 kB     00:00     Running rpm_check_debugRunning Transaction TestTransaction Test SucceededRunning Transaction  Installing : procmail-3.22-25.1.el6_5.1.x86_64                                                                                                                                           1/5   Installing : sendmail-8.14.4-9.el6.x86_64                                                                                                                                                2/5   Installing : cronie-1.4.4-15.el6.x86_64                                                                                                                                                  3/5   Installing : crontabs-1.10-33.el6.noarch                                                                                                                                                 4/5   Installing : cronie-anacron-1.4.4-15.el6.x86_64                                                                                                                                          5/5 ERROR:dbus.proxies:Introspect error on :1.2513:/org/freedesktop/PackageKit: dbus.exceptions.DBusException: org.freedesktop.DBus.Error.NoReply: Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.Unable to send message to PackageKit  Verifying  : crontabs-1.10-33.el6.noarch                                                                                                                                                 1/5   Verifying  : cronie-anacron-1.4.4-15.el6.x86_64                                                                                                                                          2/5   Verifying  : sendmail-8.14.4-9.el6.x86_64                                                                                                                                                3/5   Verifying  : cronie-1.4.4-15.el6.x86_64                                                                                                                                                  4/5   Verifying  : procmail-3.22-25.1.el6_5.1.x86_64                                                                                                                                           5/5 Installed:  cronie.x86_64 0:1.4.4-15.el6                                                                                                                                                                 Dependency Installed:  cronie-anacron.x86_64 0:1.4.4-15.el6               crontabs.noarch 0:1.10-33.el6               procmail.x86_64 0:3.22-25.1.el6_5.1               sendmail.x86_64 0:8.14.4-9.el6              Complete!

查看命令帮助

crontab 并不支持-h或–help之类的选项,不过还是可以通过它查看命令帮助

# crontab -hcrontab:无效选项 -- hcrontab: usage error: unrecognized optionusage:  crontab [-u user] file    crontab [-u user] [ -e | -l | -r ]        (default operation is replace, per 1003.2)    -e  (edit user's crontab)    -l  (list user's crontab)    -r  (delete user's crontab)    -i  (prompt before deleting user's crontab)    -s  (selinux context)

也可以使用 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

  进入vi编辑模式,编辑的内容一定要符合下面的格式:/1 * * * ls >> /tmp/ls.txt

服务状态控制

/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

现在Cron这个服务已经在进程里面了,我们就可以用这个服务了。

转载地址:http://lhzvl.baihongyu.com/

你可能感兴趣的文章
不浮躁,获取充实感
查看>>
Pyqt 国际化多语言支持
查看>>
大多数女生为什么不适合当程序员?
查看>>
SID1190471 / 烦人的幻灯片 暴力出奇迹 !!!!!!!!!!!!!!!!!!...
查看>>
高速排序 与 随机高速排序 算法分析
查看>>
使用MyEclipse 2014构建Maven项目的两种方法
查看>>
WebGIS中以version方式实现代码更新后前端自动读取更新代码的方法
查看>>
Centos 安装Apache软件
查看>>
微信小程序中在swiper-item中遍历循环添加多个数据内容(微信小程序交流群:604788754)...
查看>>
java-mybaits-00503-延迟加载
查看>>
看淡你的权力
查看>>
Linux学习(一)
查看>>
[1-5] 把时间当做朋友(李笑来)Chapter 5 【小心所谓成功学】 摘录
查看>>
POJ 3126 Prime Path SPFA
查看>>
Shortest Path [3]
查看>>
离线情报分析工具CaseFile
查看>>
【iCore4 双核心板_FPGA】例程九:锁相环实验——锁相环使用
查看>>
05Hadoop-左外连接
查看>>
python3 识别图片文字
查看>>
文字在div中水平和垂直居中的的css样式
查看>>