Linux crontab

/etc:

这个目录包含所有系统层面的配置文件。它也包含一系列的 shell 脚本, 在系统启动时,这些脚本会开启每个系统服务。这个目录中的任何文件应该是可读的文本文件。

有趣的文件:虽然/etc 目录中的任何文件都有趣,但这里只列出了一些我一直喜欢的文件:

  • /etc/crontab, 定义自动运行的任务。
  • /etc/fstab,包含存储设备的列表,以及与他们相关的挂载点。
  • /etc/passwd,包含用户帐号列表。

/etc/crontab:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# 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

 */1 * * * * root /scripts/xxxx.sh start

0 */2 * * * root echo > /var/log/rinetd.log

 

 

安装crontab:

yum install crontabs

服务操作说明:

/sbin/service crond start //启动服务

/sbin/service crond stop //关闭服务

/sbin/service crond restart //重启服务

/sbin/service crond reload //重新载入配置

/sbin/service crond status //启动服务


查看crontab服务是否已设置为开机启动,执行命令:

ntsysv

加入开机自动启动:

chkconfig –level 35 crond on

 

注意(第一位为 * 的话得注意):

 * */1 * * *  =  */1 * * * *  =  * * * * * (每秒钟执行一次)

 

用检测算法验证一下:

<?php
var_dump(_isTimeByCron(time(),'* */1 * * * *'));

function _isTimeByCron($time, $cron)
    {
        $cronParts = explode(' ', $cron);
        if (count($cronParts) != 6) {
            return false;
        }

        list($sec, $min, $hour, $day, $mon, $week) = $cronParts;

        $checks = array('sec' => 's', 'min' => 'i', 'hour' => 'G', 'day' => 'j', 'mon' => 'n', 'week' => 'w');

        $ranges = array(
            'sec' => '0-59',
            'min' => '0-59',
            'hour' => '0-23',
            'day' => '1-31',
            'mon' => '1-12',
            'week' => '0-6',
        );

        foreach ($checks as $part => $c) {
            $val = $$part;
            $values = array();

            /*
                For patters like 0-23/2
            */
            if (strpos($val, '/') !== false) {
                //Get the range and step
                list($range, $steps) = explode('/', $val);

                //Now get the start and stop
                if ($range == '*') {
                    $range = $ranges[$part];
                }
                list($start, $stop) = explode('-', $range);

                for ($i = $start; $i <= $stop; $i = $i + $steps) {
                    $values[] = $i;
                }
            } /*
                For patters like :
                2
                2,5,8
                2-23
            */
            else {
                $k = explode(',', $val);

                foreach ($k as $v) {
                    if (strpos($v, '-') !== false) {
                        list($start, $stop) = explode('-', $v);

                        for ($i = $start; $i <= $stop; $i++) {
                            $values[] = $i;
                        }
                    } else {
                        $values[] = $v;
                    }
                }
            }

            if (!in_array(date($c, $time), $values) and (strval($val) != '*')) {
                return false;
            }
            print_r($values);
        }

        return true;
    }

?>

输出:

Array
(
    [0] => *
)
Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
    [6] => 6
    [7] => 7
    [8] => 8
    [9] => 9
    [10] => 10
    [11] => 11
    [12] => 12
    [13] => 13
    [14] => 14
    [15] => 15
    [16] => 16
    [17] => 17
    [18] => 18
    [19] => 19
    [20] => 20
    [21] => 21
    [22] => 22
    [23] => 23
    [24] => 24
    [25] => 25
    [26] => 26
    [27] => 27
    [28] => 28
    [29] => 29
    [30] => 30
    [31] => 31
    [32] => 32
    [33] => 33
    [34] => 34
    [35] => 35
    [36] => 36
    [37] => 37
    [38] => 38
    [39] => 39
    [40] => 40
    [41] => 41
    [42] => 42
    [43] => 43
    [44] => 44
    [45] => 45
    [46] => 46
    [47] => 47
    [48] => 48
    [49] => 49
    [50] => 50
    [51] => 51
    [52] => 52
    [53] => 53
    [54] => 54
    [55] => 55
    [56] => 56
    [57] => 57
    [58] => 58
    [59] => 59
)
Array
(
    [0] => *
)
Array
(
    [0] => *
)
Array
(
    [0] => *
)
Array
(
    [0] => *
)
bool(true)

可以看到,秒的位置被解析成 * ,分钟的取值范围是 0-59,这就意味着每秒执行一次

 

posted @ 2018-11-22 14:52  凌雨尘  阅读(186)  评论(0编辑  收藏  举报