CronTab: Not running my PHP scripts?

10,869

You can use the web version in the cron if GET(lwp-request) or curl is installed on the server

GET

* * * * * GET http://localhost/cron/sendQueuedEmails.php > /dev/null

curl

* * * * * curl -o /dev/null http://localhost/cron/sendQueuedEmails.php
Share:
10,869

Related videos on Youtube

Garrett
Author by

Garrett

Updated on September 17, 2022

Comments

  • Garrett
    Garrett over 1 year

    final edit: i have moved this to 406 Error with GET Cron Job?

    EDIT 4:

    i am getting a 406 error page with this cron!

    here is the crontab (copied from cPanel):

        * * * * * GET https://abc.com/cron/sendBulletinEmails.php >>
    /home/abc/public_html/cron/logs/sendBulletinEmails.log
    

    here is the log:

    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>406 Not Acceptable</title>
    </head><body>
    <h1>Not Acceptable</h1>
    <p>An appropriate representation of the requested resource /cron/sendSurveyEmails.php could not be found on this server.</p>
    <p>Additionally, a 404 Not Found
    error was encountered while trying to use an ErrorDocument to handle the request.</p>
    </body></html>
    

    i have php set up on a virtual machine running linux. i've set my crontab to:

    * * * * * { cd /var/www/cron && /usr/bin/php -f sendQueuedEmails.php ;} |/usr/bin/logger -t sendQueuedEmails
    

    however, the cron does not seem to be running. it is not logging any errors and is not sending emails. do you know what is wrong?

    thanks!

    EDIT 3:

    i've found logs of the cron running. nothing seems to be wrong but it still isnt working or outputting anything!

    Aug  5 16:20:01 fiqsrv1 CRON[18543]: (cgurnik) CMD ({ cd /var/www/cron && /usr/bin/php -f sendQueuedEmails.php ;} |/usr/bin/logger -t sendQueuedEmails)
    Aug  5 16:21:01 fiqsrv1 CRON[18549]: (cgurnik) CMD ({ cd /var/www/cron && /usr/bin/php -f sendQueuedEmails.php ;} |/usr/bin/logger -t sendQueuedEmails)
    Aug  5 16:22:01 fiqsrv1 CRON[18554]: (cgurnik) CMD ({ cd /var/www/cron && /usr/bin/php -f sendQueuedEmails.php ;} |/usr/bin/logger -t sendQueuedEmails)
    Aug  5 16:23:01 fiqsrv1 CRON[18559]: (cgurnik) CMD ({ cd /var/www/cron && /usr/bin/php -f sendQueuedEmails.php ;} |/usr/bin/logger -t sendQueuedEmails)
    Aug  5 16:24:01 fiqsrv1 CRON[18564]: (cgurnik) CMD ({ cd /var/www/cron && /usr/bin/php -f sendQueuedEmails.php ;} |/usr/bin/logger -t sendQueuedEmails)
    Aug  5 16:25:01 fiqsrv1 CRON[18569]: (cgurnik) CMD ({ cd /var/www/cron && /usr/bin/php -f sendQueuedEmails.php ;} |/usr/bin/logger -t sendQueuedEmails)
    Aug  5 16:26:01 fiqsrv1 CRON[18574]: (cgurnik) CMD ({ cd /var/www/cron && /usr/bin/php -f sendQueuedEmails.php ;} |/usr/bin/logger -t sendQueuedEmails)
    Aug  5 16:27:01 fiqsrv1 CRON[18595]: (cgurnik) CMD ({ cd /var/www/cron && /usr/bin/php -f sendQueuedEmails.php ;} |/usr/bin/logger -t sendQueuedEmails)
    Aug  5 16:28:01 fiqsrv1 CRON[18601]: (cgurnik) CMD ({ cd /var/www/cron && /usr/bin/php -f sendQueuedEmails.php ;} |/usr/bin/logger -t sendQueuedEmails)
    Aug  5 16:29:01 fiqsrv1 CRON[18610]: (cgurnik) CMD ({ cd /var/www/cron && /usr/bin/php -f sendQueuedEmails.php ;} |/usr/bin/logger -t sendQueuedEmails)
    

    EDIT 2:

    now when i run the script, it outputs nothing.

    i thought i'd post the script to show that it ALWAYS outputs something, which is why i am confused when i run it and nothing comes out (no errors, no output)

        <?php
    require '../includes/common.php';
    
    /*
     * check that this cron job isn't already running (it can take a long time if there is a large email load, which it is meant for)
     * if it is running, end the script
     * if it is not running, continue
     * set the global variable for this cron job to on
     * get all queued emails that have their time to be sent in the past (and so they should be mailed out now)
     * loop through them, checking to see if the user is still set to receive the email, and if so, sending it to them
     * set the global variable for this cron job to off
     * 
     * JUST IN CASE: put the script in a try catch after the email cron is set to running in globalvars so that it is always reset, even upon failure
     */
    // check that this cron job isn't already running (it can take a long time if there is a large email load, which it is meant for)
    if(GlobalVars::isEmailCronRunning()) {
        echo "Already running! Aborted.";
        exit; // if it is running, end the script
    }
    
    // if it is not running, continue
    // set the global variable for this cron job to on
    GlobalVars::set(GlobalVars::VAR_IS_EMAIL_CRON_RUNNING, 1);
    
    try {
    
        //  get all queued emails that have their time to be sent in the past (and so they should be mailed out now)
        $queuedEmails = Emails::getAllQueuedToSend();
    
        // loop through them, checking to see if the user is still set to receive the email, and if so, sending it to them
        $numEmailsSent = 0;
        $numEmailsRecalled = 0;
        foreach($queuedEmails as $email) {
            if(Emails::shouldBeSentToUser($email)) {
                Emails::sendQueuedEmail($email[Emails::id]);
                $numEmailsSent++;
            } else {
                Emails::update($email[Emails::id], array(Emails::result => Emails::RESULT_NOT_SENT) );
                $numEmailsRecalled++;
            }
        }
    
        // set the global variable for this cron job to off
        GlobalVars::set(GlobalVars::VAR_IS_EMAIL_CRON_RUNNING, 0);
    } catch (Exception $e) {
        // set the global variable for this cron job to off
        GlobalVars::set(GlobalVars::VAR_IS_EMAIL_CRON_RUNNING, 0);
        echo "Error: " . print_r($e);
    }
    
    if($numEmailsSent || $numEmailsRecalled) {
        $details = "Sent " . $numEmailsSent . ". Recalled " . $numEmailsRecalled . ".";
        echo nl2br($details);
        ActionLogs::add(ActionLogs::CAT_CRON_JOBS, ActionLogs::TYPE_CRON_EMAILER_RUN, $details);
    } else {
        echo "No emails were sent.";
    }
    ?>
    

    EDIT: i tried running it and got the following:

    Warning: require_once(/includes/Swift-4.0.6/lib/swift_required.php): failed to open stream: No such file or directory in /var/www/includes/common.php on line 31
    
    Fatal error: require_once(): Failed opening required '/includes/Swift-4.0.6/lib/swift_required.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/includes/common.php on line 31
    

    how can i set the include path so that this works both on my linux debugging server AND my linux live server?

    • Admin
      Admin almost 14 years
      Do these scripts generate any output?
    • Admin
      Admin almost 14 years
      Perhaps it can't find php. Try specifying the full path (i.e. /usr/bin/php)
    • Admin
      Admin almost 14 years
      does php -f sendBulletinEmails.php run okay from the command line ?
    • Admin
      Admin almost 14 years
      Why all cron values are "*"? Is it supported on your system?
    • Admin
      Admin almost 14 years
    • Admin
      Admin almost 14 years
      that one is too crowded, and it took a few steps to get to this point. this is an updated question.
    • Admin
      Admin almost 14 years
      i'm not sure if it is user or etc, but i am logged in as a user. how can i find out?
    • Admin
      Admin over 11 years
      @WoLpH, yep that fixed my problem too. I had to give it the full path to the PHP executable for it to work.
    • Admin
      Admin over 8 years
      Possible duplicate of 406 Error with GET Cron Job?
  • Garrett
    Garrett almost 14 years
    i dont have '/var/log/cron'. i guess that means its not running?
  • James L
    James L almost 14 years
    which flavour of Linux are you running?
  • Garrett
    Garrett almost 14 years
    ubuntu. i updated my question with some new info =)
  • Garrett
    Garrett almost 14 years
    no, /var/www/includes/Swift-4.0.6/lib/swift_required.php exists (without cron/). how can i make it look there?
  • Garrett
    Garrett almost 14 years
    i fixed the include path (or so i think), because now it runs with no errors, but the script doesn't do anything =S what's wrong?
  • Garrett
    Garrett almost 14 years
    /usr/bin/php is the full path to the php executable
  • Garrett
    Garrett almost 14 years
    i added that to the file, and the cron job hasn't performed its actions so i'm assuming its not working. is there a way i can put that in my crontab instead of the file itself?
  • Garrett
    Garrett almost 14 years
    where is the log located?
  • Garrett
    Garrett almost 14 years
    how? and is locate a php command?
  • ESW
    ESW almost 14 years
    not that I know of; I have a PHP cron running this way at the moment. Are you sure you put in the correct path to PHP on YOUR server? the executable is likely not installed as "php5" on your machine (it is on mine).
  • Garrett
    Garrett almost 14 years
    i found the logs in /var/log/messages, so i played around for a while, but then i removed it to clean it up and now it's not coming back =S is there a way to make it come back and continue logging?
  • James L
    James L almost 14 years
    Does the script work normally? If not, it's a coding issue. If so, where do you normally execute it from? I'm guessing it sends emails so check /var/log/mail.log (I think that's the location on Ubuntu) after you've run it to see if there's anything there.
  • Garrett
    Garrett almost 14 years
    it does work from a browswer. from the terminal, it doesn't output anything, and i think the logs would be useful to see if there are any errors now but i lost them after deleting the file =S there's nothing in my mail log.
  • James L
    James L almost 14 years
    Do you execute the script directly from the website or via another one? Do you have Plesk running on this server? Can you send any email using PHP from command line? What's the code. The problem here isn't cron.
  • Garrett
    Garrett almost 14 years
    thats a good solution, but i need it to be secure too
  • bobbyjohnson
    bobbyjohnson almost 14 years
    no, locate is a shell commande, using it will give you the full path to any file named like swift_required.php, and you'll know where your includes folder is.
  • sajal
    sajal almost 14 years
    Whats insecure about this method? you can always hack sendQueuedEmails.php to execute only if accessed from localhost.
  • Garrett
    Garrett almost 14 years
    i get this in my log (using the GET method): <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>404 Not Found</title> </head><body> <h1>Not Found</h1> <p>The requested URL /cron/index.php was not found on this server.</p> <hr> <address>Apache/2.2.12 (Ubuntu) Server at localhost Port 80</address> </body></html>
  • sajal
    sajal almost 14 years
    ignore the <pre></pre> bit... the system was removing http:// portions from my previous comment
  • Garrett
    Garrett almost 14 years
    i have an issue. i should have tested like that before, because now, on the live site, i have to use the real domain, and it is causing issues. i have updated my question with the problem details. thanks!!
  • Pete
    Pete over 10 years
    just a note: hitting localhost may not work if, for example, the server is configured to serve up different content on different domains. abc.com maybe be an apache virtualhost (this is the case with one of my drupal installs)