CentOS下使用yum命令安装计划任务程序crontab的方法
概述
利用“任务计划”,可以将任何脚本、程序或文档安排在某个最方便的时间运行。通俗的讲就是定时执行某个脚本、程序。
常见用途
定时关机
定时检查更新
定时对系统配置、数据库、日志进行备份
定时清理垃圾文件
检测是否已安装
#crontab -bash:crontab:commandnotfound
执行crontab命令如果报commandnotfound,就表明没有安装.
开始安装
#yum-yinstallvixie-cron Loadedplugins:fastestmirror,refresh-packagekit,security Existinglock/var/run/yum.pid:anothercopyisrunningaspid25960. Anotherappiscurrentlyholdingtheyumlock;waitingforittoexit...
可能是系统自动升级正在运行,yum在锁定状态中。
可以通过强制关掉yum进程:
#rm-f/var/run/yum.pid
然后就可以使用yum了。
#yum-yinstallvixie-cron Loadedplugins:fastestmirror,refresh-packagekit,security Determiningfastestmirrors *base:mirrors.btte.net *extras:mirrors.sina.cn *updates:mirrors.sina.cn base|3.7kB00:00 extras|3.4kB00:00 extras/primary_db|32kB00:00 updates|3.4kB00:00 updates/primary_db|1.9MB00:07 SettingupInstallProcess ResolvingDependencies -->Runningtransactioncheck --->Packagecronie.x86_640:1.4.4-15.el6willbeinstalled -->ProcessingDependency:dailyjobsforpackage:cronie-1.4.4-15.el6.x86_64 -->ProcessingDependency:/usr/sbin/sendmailforpackage:cronie-1.4.4-15.el6.x86_64 -->Runningtransactioncheck --->Packagecronie-anacron.x86_640:1.4.4-15.el6willbeinstalled -->ProcessingDependency:crontabsforpackage:cronie-anacron-1.4.4-15.el6.x86_64 --->Packagesendmail.x86_640:8.14.4-9.el6willbeinstalled -->ProcessingDependency:procmailforpackage:sendmail-8.14.4-9.el6.x86_64 -->Runningtransactioncheck --->Packagecrontabs.noarch0:1.10-33.el6willbeinstalled --->Packageprocmail.x86_640:3.22-25.1.el6_5.1willbeinstalled -->FinishedDependencyResolution DependenciesResolved =============================================================================================================================================================================================== PackageArchVersionRepositorySize =============================================================================================================================================================================================== Installing: croniex86_641.4.4-15.el6base74k Installingfordependencies: cronie-anacronx86_641.4.4-15.el6base31k crontabsnoarch1.10-33.el6base10k procmailx86_643.22-25.1.el6_5.1base162k sendmailx86_648.14.4-9.el6base716k TransactionSummary =============================================================================================================================================================================================== Install5Package(s) Totaldownloadsize:994k Installedsize:2.1M DownloadingPackages: (1/5):cronie-1.4.4-15.el6.x86_64.rpm|74kB00:00 (2/5):cronie-anacron-1.4.4-15.el6.x86_64.rpm|31kB00:00 (3/5):crontabs-1.10-33.el6.noarch.rpm|10kB00:00 (4/5):procmail-3.22-25.1.el6_5.1.x86_64.rpm|162kB00:00 (5/5):sendmail-8.14.4-9.el6.x86_64.rpm|716kB00:00 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Total1.3MB/s|994kB00:00 Runningrpm_check_debug RunningTransactionTest TransactionTestSucceeded RunningTransaction Installing:procmail-3.22-25.1.el6_5.1.x86_641/5 Installing:sendmail-8.14.4-9.el6.x86_642/5 Installing:cronie-1.4.4-15.el6.x86_643/5 Installing:crontabs-1.10-33.el6.noarch4/5 Installing:cronie-anacron-1.4.4-15.el6.x86_645/5 ERROR:dbus.proxies:Introspecterroron:1.2513:/org/freedesktop/PackageKit:dbus.exceptions.DBusException:org.freedesktop.DBus.Error.NoReply:Didnotreceiveareply.Possiblecausesinclude:theremoteapplicationdidnotsendareply,themessagebussecuritypolicyblockedthereply,thereplytimeoutexpired,orthenetworkconnectionwasbroken. UnabletosendmessagetoPackageKit Verifying:crontabs-1.10-33.el6.noarch1/5 Verifying:cronie-anacron-1.4.4-15.el6.x86_642/5 Verifying:sendmail-8.14.4-9.el6.x86_643/5 Verifying:cronie-1.4.4-15.el6.x86_644/5 Verifying:procmail-3.22-25.1.el6_5.1.x86_645/5 Installed: cronie.x86_640:1.4.4 15.el6 DependencyInstalled: cronie-anacron.x86_640:1.4.4-15.el6crontabs.noarch0:1.10-33.el6procmail.x86_640:3.22-25.1.el6_5.1sendmail.x86_640:8.14.4-9.el6 Complete!
查看命令帮助
crontab并不支持-h或–help之类的选项,不过还是可以通过它查看命令帮助
#crontab-h crontab:无效选项--h crontab:usageerror:unrecognizedoption usage:crontab[-uuser]file crontab[-uuser][-e|-l|-r] (defaultoperationisreplace,per1003.2) -e(edituser'scrontab) -l(listuser'scrontab) -r(deleteuser'scrontab) -i(promptbeforedeletinguser'scrontab) -s(selinuxcontext)
也可以使用infocrontab命令查看详细的帮助信息。
cron服务提供crontab命令来设定cron服务的,以下是这个命令的一些参数与说明:
crontab-u//设定某个用户的cron服务,一般root用户在执行这个命令的时候需要此参数
crontab-l//列出某个用户cron服务的详细内容
crontab-r//删除没个用户的cron服务
crontab-e//编辑某个用户的cron服务
比如说root查看自己的cron设置:crontab-uroot-l
再例如,root想删除fred的cron设置:crontab-ufred-r
在编辑cron服务时,编辑的内容有一些格式和约定,输入:crontab-uroot-e
进入vi编辑模式,编辑的内容一定要符合下面的格式:/1***ls>>/tmp/ls.txt
服务状态控制
/sbin/servicecrondstart//启动服务 /sbin/servicecrondstop//关闭服务 /sbin/servicecrondrestart//重启服务 /sbin/servicecrondreload//重新载入配置 /sbin/servicecrondstatus//查看状态
或者使用
#servicecrondstart #servicecrondstop #servicecrondrestart #servicecrondreload #servicecrondstatus
开机启动
你也可以将这个服务在系统启动的时候自动启动:
在/etc/rc.d/rc.local这个脚本的末尾加上:
/sbin/servicecrondstart
现在Cron这个服务已经在进程里面了,我们就可以用这个服务了。
总结
以上所述是小编给大家介绍的CentOS下使用yum命令安装计划任务程序crontab,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!