设为首页 收藏本站
查看: 676|回复: 0

[经验分享] crond定时任务详细分析

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-8-29 10:12:29 | 显示全部楼层 |阅读模式
一、定时任务crond的介绍  

crond是linux系统中用来定期执行命令或指定程序任务的一种服务或软件。一般情况下,我们安装文成系统之后,默认变回启动crond任务调度服务,crond服务会定期(默认每分钟检查一次)检查系统中是否有要执行的任务工作。如果有,变会根据预先设定的定时任务自动执行该定时任务,就如同生活中的闹钟一样。
1
2
3
4
[iyunv@iZ94lm56da9Z ~]# chkconfig --list|grep crond    <--查询是否开启
crond              0:off    1:off    2:on    3:on    4:on    5:on    6:off
[iyunv@iZ94lm56da9Z ~]# crontab -l    <--查询crond的配置
no crontab for root



因为crond是每分钟级别,如果crond服务搞不定了,一般工作中写脚本守护程序进程:
1
2
3
4
5
6
7
[iyunv@iZ94lm56da9Z ~]# cat corn
while true
do
    echo "wangde .....Rui"
    sleep 1
done
#内容意思为每秒echo出"wangde .....Rui"。



补充两个概念:
    程序文件:程序代码组成,但是没有在计算机内执行。当前没有执行。
    守护程序或者守护进程:进程就是在计算机中正在执行的程序;守护进程就是一直运行的程序。
可以看出定时任务是一个守护进程。
1
2
3
[iyunv@iZ94lm56da9Z ~]# ps -ef |grep crond
root       923     1  0 Aug25 ?        00:00:00 crond
root     27961 27915  0 15:29 pts/0    00:00:00 grep crond




crond和ssh一样是一直在后台运行的守护进程:
1
2
3
4
5
6
[iyunv@iZ94lm56da9Z ~]# /etc/init.d/sshd restart
Stopping sshd:                                             [  OK  ]
Starting sshd:                                             [  OK  ]
[iyunv@iZ94lm56da9Z ~]# /etc/init.d/crond restart
Stopping crond:                                            [  OK  ]
Starting crond:                                            [  OK  ]



二、不同系统的定时任务

windows的定时任务存放位置:所有程序->附件->系统工具->任务计划程序

QQ截图20160829101218.png
Linux系统的定时任务调度的工作可以分为以下两种情况:
    1.linux系统自身定期执行的任务工作:系统周期性的自行执行的任务工作,例如轮循备份系统日志、备份系统数据、清理系统缓存等,这些任务无需我们人为干预。如下:
1
2
3
4
5
6
7
8
9
10
11
[iyunv@alone ~]# ll /var/log/messages*
-rw-------  1 root root 113765 8月  23 23:49 /var/log/messages
-rw-------. 1 root root 226986 8月   7 02:58 /var/log/messages-20160807
-rw-------  1 root root  25713 8月  14 02:58 /var/log/messages-20160814
-rw-------  1 root root  25649 8月  21 02:58 /var/log/messages-20160821
[iyunv@alone ~]# ll /var/log/secure*
-rw-------  1 root root  43166 8月  23 23:44 /var/log/secure
-rw-------. 1 root root  91198 8月   7 03:16 /var/log/secure-20160807
-rw-------  1 root root 133631 8月  14 03:46 /var/log/secure-20160814
-rw-------  1 root root 134102 8月  21 03:30 /var/log/secure-20160821
#每周一份日志,系统自身执行的定时任务



系统默认执行的日志脚本存放位置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[iyunv@alone ~]# cd /etc/logrotate.d/
[iyunv@alone logrotate.d]# pwd
/etc/logrotate.d
[iyunv@alone logrotate.d]# ls -lrt
总用量 32
-rw-r--r--. 1 root root 136 8月  23 2010 ppp
-rw-r--r--. 1 root root 329 7月  17 2012 psacct
-rw-r--r--. 1 root root 100 2月   4 2013 wpa_supplicant
-rw-r--r--. 1 root root 100 2月  22 2013 yum
-rw-r--r--. 1 root root 210 8月  15 2013 syslog
-rw-r--r--. 1 root root  71 8月  17 2013 cups
-rw-r--r--. 1 root root 219 11月 23 2013 sssd
-rw-r--r--. 1 root root 103 11月 26 2013 dracut
[iyunv@alone logrotate.d]#
[iyunv@alone logrotate.d]# less syslog
[iyunv@alone logrotate.d]# cat syslog |grep -v '^$'
/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
    sharedscripts
    postrotate
    /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
    endscript
}



系统自动轮循任务的设置配置路径:
1
2
3
4
5
6
7
8
9
10
11
[iyunv@alone logrotate.d]# ll /etc/ |grep cron
-rw-------.  1 root root    541 11月 23 2013 anacrontab
drwxr-xr-x.  2 root root   4096 7月  27 13:14 cron.d
drwxr-xr-x.  2 root root   4096 7月  27 13:17 cron.daily
-rw-------.  1 root root      0 11月 23 2013 cron.deny
drwxr-xr-x.  2 root root   4096 7月  27 13:13 cron.hourly
drwxr-xr-x.  2 root root   4096 7月  27 13:15 cron.monthly
-rw-r--r--.  1 root root    457 9月  27 2011 crontab
drwxr-xr-x.  2 root root   4096 9月  27 2011 cron.weekly
[iyunv@alone logrotate.d]# pwd
/etc/logrotate.d



以下是系统及的定时任务,一般用户自定义的定时任务不会放在这里:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[iyunv@alone logrotate.d]# 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
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed



    2.用户执行的定时任务:某个用户或者系统管理员定期要做的任务工作,例如每个5分钟和互联网时间服务器同步(当然这个是最基础的系统优化),每天晚上0点备份站点数据及数据库数据,这些工作一般都是用户自己设定定时任务才行,

例如:服务器时间同步:
1
2
[iyunv@iZ94lm56da9Z ~]# crontab -l
*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1



三、linux系统下定时任务软件的种类

linux系统下基本有以下几种:at(适合只执行一次)、crontab(最常用)、anacron。

    at:适合执行一次就借宿的调度任务命令。例如:在今晚需要处理一个任务,仅仅是今天晚上,属于突发性的工作任务。要执行at命令,还需要启动一个名为atd的服务才行。(在工作中不常用,所以默认为关闭状态)
1
2
[iyunv@iZ94lm56da9Z ~]# chkconfig --list |grep atd
atd                0:off    1:off    2:off    3:off    4:off    5:off    6:off



    anacron:这个命令主要用于非7*24小时开机的服务器准备的,anacron并不能指定具体时间执行任务,而是以天为周期或者在系统每次开机后执行的任务工作。他会检测服务器停机期间应该执行但没有进行的任务工作,并将该任务执行一遍。(工作中一般服务器是持续开机,所以一般不怎么用)
    备注:

1.这里的crond服务是运行的程序,而crontab命令用户用来设置定时规则的命令
2.crond服务是工作冲常用的重要服务,at和anacron很少使用,可以忽略,
3.工作中几乎每个服务器都会用到crond服务。




四、crontab命令的格式 (下面进入主题)

crond    守护进程,一直运行着的
crontab    设置命令,-e 编辑;-l 列表。
1
2
3
4
5
6
7
8
9
10
11
[iyunv@alone logrotate.d]# crontab -h
crontab:无效选项 -- h
crontab: usage error: unrecognized option
usage:    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)



crontab -e  相当于 vi /var/spool/cron/root
crontab -l   相当于 cat /var/spool/cron/root
1
2
3
4
[iyunv@iZ94lm56da9Z ~]# crontab -l
*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1
[iyunv@iZ94lm56da9Z ~]# cat /var/spool/cron/root
*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1



让普通用户执行crontab -u richy -e   或者 切换到richy用户下 再使用crontab

crontab 定时任务命令字段解释
*           *         *         *           *

0-59   0-23   1-31   1-12     0-6

*  每   * * * * * /bin/sh/scripts/richy.sh  每分钟执行richy.sh

-  范围    00 17-19 * * * /bin/sh/scripts/richy.sh  每天17点,18点,19点整执行richy.sh

, 分隔   30 17,18,19 * * * /bin/sh/scripts/richy.sh  每天17:30,18:30,19:30执行richy.sh

/n  每单位时间   */10 * * * * /bin/sh/scripts/richy.sh  每10分钟执行richy.sh


30 3-5 17-19 * * /bin/sh/scripts/richy.sh  每月的17,18,19号的3:30,4:30,5:30执行命令richy.sh

五、crontab编辑定时任务依赖的服务
1
2
3
4
[iyunv@iZ94lm56da9Z ~]# chkconfig --list |grep crond
crond              0:off    1:off    2:on    3:on    4:on    5:on    6:off
[iyunv@iZ94lm56da9Z ~]# ps -ef |grep crond |grep -v grep
root     27988     1  0 15:30 ?        00:00:00 crond



六、crondtab的总结

  • 定时任务要加注释;

  • 结尾不要有>/dev/null 2>&1;
  • 如果存在目录,目录必须要存在,因为crontab不会创建新目录;
  • 定时任务中的路径一定要是绝对路径;
  • crond服务必须开启并运行;
    .
    .



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-264586-1-1.html 上篇帖子: 自动化运维之Cobbler自动化系统部署 下篇帖子: Linux NFS自动挂载autofs配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表