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

[经验分享] PHP使用1个crontab管理多个crontab任务

[复制链接]

尚未签到

发表于 2015-8-25 10:55:38 | 显示全部楼层 |阅读模式
  http://www.binarytides.com/php-manage-multiple-cronjobs-with-a-single-crontab-entry/
  
  In many php applications there are multiple tasks that need to be run via cron at different times. In a typical application you may be doing the following tasks via cronjobs :
  1. Backup database.
2. Send out email to subscribers.
3. Clear temporary files.
4. Fetch xml feeds from some source and store them in database.
  So if you had separate php files doing these tasks , and had a cronjob entry for each , your cronjob could look like this :


MAILTO="happy@birthday.com"
0 0 * * * /var/www/my_app/backup_database.php
12 2 * * * /var/www/my_app/email_to_subscribers.php
10 5 * * * /var/www/my_app/clear_temp_files.php
10 7 * * * /var/www/my_app/fetch_xml_feeds.php  The above is the simplest approach to manage multiple cronjobs for your php application. However this approach has many drawbacks :
  1. Multiple entries in crontabs means more time and effort needed to edit the crontab and maintain it.
  - Since this approach involves writing each task separately in the cron list , it becomes difficult to maintain.
  2. The crontab command has to be used everytime a change is to be made.
  - You need to either manually do a `crontab -t` in the shell prompt or make your php application do it , everytime there is a change in either the tasks or their timing.
  3. If the script name changes then have to edit the crontab file again.
4. A log email would be generated for every cron run, creating multiple log emails.
  - Every task that is running would generate a cronlog and email itself to the email specified
  5. Inefficient when there are 10s or 100s of tasks to be run via cron.
  - Do you think it is a good idea if you had many many tasks to do.

An alternative solution
  How about having only 1 entry in the cronjobs, and that 1 cronjob manages the rest of the cronjobs.
  1. Add only 1 task to the crontab rule say :







1




* * * * * php /path/to/cronjob.php
  The cronjob.php file would run all the tasks that need to be run via cron. Its important to note that this cronjob.php will run every minute and forever. Do not be worried about it eating too much of system resources. It is a faily light thing and does not load your CPU or RAM with anything heavy.(注意这个cronjob.php会每分钟执行一次,而且一直会一直这样。不必担心这会消耗太多系统资源,这是一个非常轻量级的东西,它不会额外占用你的CPU和内存)
  Now the next thing would be to make sure cronjob.php can run all the tasks at the right time.
  2. Now we need to have different tasks have a different cron schedule. Lets say there are 3 different tasks to run at 3 different times :


0 5 * * * database_backup
0 5 1,15 * * monthly_sales_report
0 10 15 02 * purchase_report

  
  The cronjob.php that runs every minute should have an array like this :







1

2

3

4

5




$cronjobs = array();



$cronjobs['database_backup'] = '0 5 * * *';

$cronjobs['monthly_sales_report'] = '0 5 1,15 * *';

$cronjobs['purchase_report'] = '0 10 15 02 *';
  Now we test each job/task for the timestamp and run it like this :







1

2

3

4

5

6

7

8

9




foreach($cronjobs as $method => $cron)

{

    $time = time();

        if( is_time_cron($time , $cron) )

    {

        $result = $method();

        echo $result;

    }

}
  is_time_cron checks if the current timestamp matches the cron schedule or not. If it matches , then the task is executed. Theis_time_cron method can be found in the previous post here.
  In this approach the benefits are :
  1. Only 1 crontab entry.
  The crontab list is clean and your application does not overload it.
  2. Easy to maintain , does not need any modification unless the script path/name changes.
  The crontab once created does not need any change unless the name or path of 'cronjob.php' changes. Meanwhile the jobs inside cronjob.php can change their names , schedules and anything very easily.
  3. To add/edit/remove tasks or change their time schedules only the cronjob.php needs to be changed.
  This means easier modification , maintenance and the application can provide simple user interface to make changes anytime without the need to use crontab commands or anything as such.
  The above mentioned approach can be applied to any language , not just php.
  
  
  附加:判断当前时间蹉是否符合某个cronjob
  http://www.binarytides.com/php-check-if-a-timestamp-matches-a-given-cron-schedule/



PHP check if a timestamp matches a given cron schedule

























1

2

3

4

5

6




desktop:~$ php -a

Interactive shell



php > echo time();

1319362432

php >


  
  Above is an example of a given timestamp.
  And a cron schedule can look like this 0 5 * * * - which means run everyday at 5 hours and 0 minutes.
  Now in a php application you may need to test if a given timestamp , say 1319362432 matches a given cron schedule like 0 5 * * *.
  Here is a quick php function that can do this task.







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

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87




/**

    Test if a timestamp matches a cron format or not

    //$cron = '5 0 * * *';

*/

function is_time_cron($time , $cron)

{

    $cron_parts = explode(' ' , $cron);

    if(count($cron_parts) != 5)

    {

        return false;

    }

     

    list($min , $hour , $day , $mon , $week) = explode(' ' , $cron);

     

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

     

    $ranges = array(

        'min' => '0-59' ,

        'hour' => '0-23' ,

        'day' => '1-31' ,

        'mon' => '1-12' ,

        'week' => '0-6' ,

    );

     

    foreach($to_check 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;

        }

    }

     

    return true;

}



var_dump(time() , '0 5 * * *');  //true or false
  How does it work
  The above code uses the date format specifiers as follows :
'min' => 'i' ,
'hour' => 'G' ,
'day' => 'j' ,
'mon' => 'n' ,
'week' => 'w'
  over the timestamp to extract the minute , hour , day , month and week of a timestamp
  Then it checks the cron format by splitting it into parts like 0 , 5 , * , * , * and then tests each part for the corresponding value from timestamp.

运维网声明 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-103948-1-1.html 上篇帖子: php书籍推荐 下篇帖子: 超实用PHP函数总结整理
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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