0

I have 2 different code running under hook cron like below:

function custom_module_cron(){

  // First here code run for posting nodes to Facebook pages 
    (this should run for cron tab Ist )


  // Second code runs for sending new posted links in form of digest 
     to users according to their selected hours. (This should run for cron tab 2nd )

}

For above process I have different cron tabs running on my server such as:

http://oexample.com/cron.php?cron_key=something7rsds

For Facebook at : 23:30, 23, 21, and many more hours

For Digest at : 7 , 8 , 10 and many more

How can we differentiate that each code run according to required hours. Can we make different urls to run for different code to prevent running of code at unwanted hours? I am using rough technique by using in_array for current time and hours in cron tab but thats very handy and I think its not solution.

Please suggest what I can do to make my codes run according to separate schedules ?

Thanks!

0

Adding a GET parameter should work. In your crontab edit the url and add something like &type=facebook.

In the hook_cron implementation you should be able to catch the type via $_GET['type']

  • How we need to set parameter in cron url can you please suggest, I found some post saying in this way stackoverflow.com/questions/11297945/… – jas Nov 2 '15 at 13:00
  • You can look here: drupal.org/node/23714 You need to call it via wget or something. For example: 0 * * * * wget -O - -q -t 1 http://mydrupal.com/cron.php?cron_key=.......&type=facebook – Andreas Gyr Nov 2 '15 at 13:23
  • Thanks for your try but FYI this way it does not work and I had set up different URLS with hook_menu and passed argument and run on server with curl etc – jas Nov 4 '15 at 18:28
0

This is how I solved my problem, If anyone have better approach please suggest.

function custom_fb_menu() {

   $items['cronjobs'] = array(
     'title' => 'Custom Cron Jobs',
     'page callback' => 'custom_cron_jobs',
     'type' => MENU_CALLBACK,
     'access callback' => TRUE,
 );
   return $items;
}


function custom_cron_jobs() {
  $type = arg(1);

  switch($type) {
    case "facebook":
        facebook_callback();
        break;
    case "digest":
        digest_callback();
        break;
 }
  return "";
}


function facebook_callback() {
    dd("Hi I am in facebook_callback"); // to write file in temp folder with help of debel mdule
}


function digest_callback() {
   dd("Hi I am in digest_callback");
}

* * * * * /usr/bin/curl http://www.example.com/cronjobs/facebook > /dev/null 2>&1

* * * * * /usr/bin/curl http://www.example.com/cronjobs/digest > /dev/null 2>&1

Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.