How to prevent cronjobs from overlaping

Discussion in 'General' started by pawan, Aug 10, 2014.

  1. pawan

    pawan Member

    I have some cron jobs set up to be run from URL in ISPCONFIG.

    I have also given different time to run these crons yet at many occasions I find that they overlap.

    Since the crons are related to sendING mail or SMS at cron run, the resulting mail, sms becomes a garbled mail, message.

    I am sure there is a way to prevent this overlaps.

    Thanks.

    ISPCONFIG - 3.0.5.4p1
    Ubuntu - 12.04
     
  2. zapyahoo

    zapyahoo Member

    Crons should be able to overlap and conclude successfully.
    The only reason I see for this to happen is incorrect cron setup/commands on the webpage/s you are trying to run them.

    Can you post your cron lines here.
     
  3. pawan

    pawan Member

    Ok let say for example first script is like

    5 7 * * *
    http://mydomain.com//index.php?option=com_content&view=article&id=300

    and second script is set up like

    10 7 * * *
    http://mydomain.com//index.php?option=com_content&view=article&id=301

    content of the first script
    PHP:
    <?php
        $db 
    = &JFactory::getDbo();
    $query $db->getQuery(true);
    $query "SELECT m.member_name name, e.designation designation,c.club_name club m.email email ,m.mobile mobile FROM  member_view m,club_name c, event_listing3 e where DAY(m.marriage_date) = DAY(CURDATE()) AND MONTH(m.marriage_date) = MONTH(CURDATE()) and (m.membership_status = 1 or m.membership_status is Null)and m.id=e.id and c.id=m.club_name";
    if (!
    $query) {
    echo 
    'Could not run query:' mysql_error();
    exit;
    }
    //$message .= "Host\t\tCount\n";
    $db->setQuery($query);
    $results $db->loadObjectList();
    //var_dump($results);exit;

    //global $messagetext1;
    //$messagetext1 .= '<table style="border:1px solid silver;padding:2px;">';
    //$messagetext1 .= "<tr><th>Name</th><th>Designation:</th><th>Club</th><th>Email</th><th>Mobile</th></tr>";
    foreach ($results as $result) {
    global 
    $messagetext;

    $messagetext .= $result->name;
    $messagetext .= ", ";

    $messagetext .= $result->designation;
    $messagetext .= ", ";
    $messagetext .= $result->club;
    $messagetext .= ", ";
    $messagetext .= $result->email;
    $messagetext .= ", ";
    $messagetext .= $result->mobile;

    $messagetext .= "\n";
    //var_dump($message);exit;
    }

    $message .="Dear Sir"."\n"."The Anniversary of the following members as on Today"."\n" $messagetext;
    $request "";
    $param['mobiles'] = "993725352,943746729,947579833,943703230,9692211277,943704579,943126700";
    $param['user'] = "ODLINE";
    $param['password'] = "D@ve12";
    $param['sendername'] = "ODLION";
    $param['senderid'] = "ODLION";
    $param['sms'] = $message;

    foreach (
    $param as $key => $val ){
    $request .= $key "=" urlencode($val);
    $request .= "&";}$request substr$request0strlen$request ) -);
    $url="http://mainadmin.dove-sms.com/sendsms.jsp?".$request;
    //'echo';print_r($url);'echo';exit;
    $ch curl_init($url);curl_setopt($chCURLOPT_URL$url );
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue );
    $a curl_exec($ch);
    curl_close$ch );
    ?>

    Content of second script
    PHP:
    <?php
    $db 
    = &JFactory::getDbo();
    $query $db->getQuery(true);
    $query "SELECT name,designation, club_name club,email,mobile FROM events WHERE
        DAY(birth_date) = DAY(CURDATE()) AND
        MONTH(birth_date) = MONTH(CURDATE())and email !=''"
    ;
    if (!
    $query) {
    echo 
    'Could not run query:' mysql_error();
    exit;
    }
    //$message .= "Host\t\tCount\n";
    $db->setQuery($query);
    $results $db->loadObjectList();
    //var_dump($results);exit;
    global $messagetext1;
    $messagetext1 .= '<table style="border:1px solid silver;padding:2px;">';
    $messagetext1 .=  "<tr><th>Name</th><th>Designation:</th><th>Club</th><th>Email</th><th>Mobile</th></tr>";
    foreach (
    $results as $result) {
    global 
    $messagetext;

    $messagetext .= "<tr><td>";
    $messagetext .=  $result->name;
    $messagetext .= "</td>";
    $messagetext .= "<td>";
    $messagetext .=  $result->designation;
    $messagetext .= "</td>";
    $messagetext .= "<td>";
    $messagetext .=  $result->club;
    $messagetext .= "</td>";
    $messagetext .= "<td>";
    $messagetext .= $result->email;
    $messagetext .= "</td>";
    $messagetext .= "<td>";
    $messagetext .= $result->mobile;
    $messagetext .=  "</td></tr>";
    $messagetext .= "<br/>";
    //var_dump($message);exit;
    }
    $messagetext .=  "</table>";

    $message .="Dear Sir"."<br>"."The Birthdays of the following members(DG VDG) as on Today"."<br>".$messagetext1 $messagetext;
    $subject "List of Members with birthday (DG VDG) as on Today";
    $MailFrom "ODLION";
    $headers "From:" $MailFrom ;
    $thisto = array('[email protected]','[email protected]','[email protected]','[email protected]','[email protected]','[email protected]');
    //$mailer->addRecipient($recipient);
        
    $mail JFactory::getMailer();
    $res=$mail->sendmail($MailFrom$FromName$thisto$subject$message,true);?>

     
    Last edited: Aug 10, 2014
  4. zapyahoo

    zapyahoo Member

  5. pawan

    pawan Member

    Yes I have it set up in ispconfig panel -> sites -> cron jobs.

    Only difference is that I have not added the line
    /usr/bin/wget -q before the http://mydomain.com//index.php?optio...article&id=300

    is this reason for the error? as the cron is running fine otherwise.
     
  6. zapyahoo

    zapyahoo Member

    Yes, that's the reason :)
     
  7. pawan

    pawan Member

    Thanks Zapyahoo
    But in the mean time I have edited one of the cron job with that preceding the link, but the cron is not running at all.
    Does this mean that my cron jobs are not setup correctly?
    How can I rectify the same?
     
  8. zapyahoo

    zapyahoo Member

    If you use ispconfig for the cron then you have to specify the path that runs that specific type of cron file... if it's a php or sh, etc.

    this is for a debian server /usr/bin/wget

    You can always use crontab -e from your shell.
     
  9. pawan

    pawan Member

    I am really sorry zapyahoo, but without the /usr/bin/wget
    It is running just fine.

    Once I add /usr/bin/wget then nothing happens.

    I really don't know what to do?
     
  10. till

    till Super Moderator Staff Member ISPConfig Developer

    Its is absolutely fine to add just http://mydomain.com//index.php?optio...article&id=300 as cron command in ispconfig. It is not nesscessary to add /usr/bin/wget and it is even recomended to not do that.

    Your reason is something else, cronjobs overlap if they are running more often then the time they need to run. So there are 2 solutions:

    a) Run the cronjob less frequently

    b) Sdd some code in your cronjob to detect if there is already another cronjob running. e.g.

    at the beginning, add:

    if(is_file('cron.lock') die();
    touch('cron.lock');


    and at the end add:

    unlink('cron.lock');

    this ensures that not more then one script is run at the same time.
     
    Last edited: Aug 11, 2014
  11. pawan

    pawan Member

    Thank you so much Till,
    Just wanted to confirm in your suggestion that
    is it touch('cron.loc'); ?
    or
    touch('cron.lock'); ?

    Thanks once again.
     
    Last edited: Aug 11, 2014
  12. till

    till Super Moderator Staff Member ISPConfig Developer

    Sorry, was typing too fast :) It is:

    touch('cron.lock');
     
  13. pawan

    pawan Member

    I have added the code as suggested by you and
    tried to run the file via url manually, but I am getting the white-screen.

    I have also tried with some variation in your suggested line for example

    your code

    if (is_file('cron.lock') die();
    touch('cron.lock');

    Variation1

    if (is_file('cron.lock')) die();
    touch('cron.lock');

    Variation2
    if is_file('cron.lock') die();
    touch('cron.lock');



    Note: I have added the line unlink('cron.lock'); in the end.

    But none of the above seems to be working.
    as just after your code line I have added:
    echo 'code is working';
    Which shows when I comment out the first line.

    Thanks.
     
  14. till

    till Super Moderator Staff Member ISPConfig Developer

    Is there a file "cron.lock" in the directory? If yes, delete that.
     
  15. pawan

    pawan Member

    Thanks Till
    Sorry for troubling you.
    But I couldn't find any.
    BTW in which directory exactly I should look into?
     

Share This Page