您的位置:首页 > 其它

工作调度--cron

2015-12-12 13:44 330 查看
工作调度

----

**cron**

作用:每次到给定的时间都要执行该任务

例如:我要每年闺蜜过生日都要给他发送email,每天晚上凌晨2点更新数据库,每隔5分钟向qys的邮箱发送系统日志文件……

预备工作:

由于我的虚拟机是完全安装,所以这些包都有,如果没有自行安装,现在进行查询

[root@linuxclient ~]# rpm -qa|grep cron

cronie-anacron-1.4.4-2.el6.i686

crontabs-1.10-32.1.el6.noarch

cronie-1.4.4-2.el6.i686

查看状态

[root@linuxclient ~]# service crond status

crond (pid 2328) is running...

各种文件:

循环调度分为系统调度和用户自定义调度,他们保存在`/etc/crontab`中

[root@linuxclient ~]# cat /etc/crontab

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

HOME=/

# 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

# | | | | |

# * * * * * command to be executed

以下查看系统会主动做的一些循环调度,比如`logwatch`日志监视(所以`root`下的`mail`才会有好多`logwatch`发来的文件)

[root@linuxclient ~]# ls -l /etc/cron.daily/

total 44

-rwxr-xr-x. 1 root root 133 Sep 21 2005 00webalizer

-rwxr-xr-x. 1 root root 265 Dec 2 2009 0logwatch

-rwxr-xr-x. 1 root root 2243 Jun 24 2010 certwatch

-rwxr-xr-x. 1 root root 118 Jul 16 2010 cups

-rwxr-xr-x. 1 root root 979 Jun 9 2010 cyrus-imapd

-rwxr-xr-x. 1 root root 196 Jun 24 2010 logrotate

-rwxr-xr-x. 1 root root 905 Jun 24 2010 makewhatis.cron

-rwxr-xr-x. 1 root root 174 May 20 2009 mlocate.cron

-rwxr-xr-x. 1 root root 2126 Apr 23 2010 prelink

-rwxr-xr-x. 1 root root 563 Mar 24 2010 readahead.cron

-rwxr-xr-x. 1 root root 365 Oct 16 2009 tmpwatch

同`at`一样,有`/etc/cron.allow`和`/etc/cron.deny`

命令行:

crontab -e

*/1 * * * * date >> /home/date1.txt

[root@linuxclient ~]# crontab -e

no crontab for root - using an empty one

crontab: installing new crontab

解释:使用crontab -e 可以对循环调度进行编辑,也就是打开了一个vim编辑器,在里面添加的是每分钟将系统时间写进/home/date1.txt这个文件中,*的顺序是:每隔一分钟(/表示每隔),任一小时,任一天,任一月,任一周。

保存成功后会有提示信息。

查看状态:(确实正在做这件事……)

[root@linuxclient ~]# cat /home/date1.txt

Sat Dec 12 00:07:02 EST 2015

Sat Dec 12 00:08:01 EST 2015

其他命令:

查看任务:

[root@linuxclient ~]# crontab -l

* * * * * date >> /home/date1.txt

删除全部任务:

[root@linuxclient ~]# crontab -r

[root@linuxclient ~]# crontab -l

no crontab for root

如果不想删除全部,就`crontab -e` 去vim中一个一个地删除。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: