job.answiz.com
  • 3
Votes
name
name Punditsdkoslkdosdkoskdo

CronTab: Not running in my PHP script?

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?

Different environment

Cron passes a minimal set of environment variables to your jobs. To see the difference, add a dummy job like this:

* * * * * env > /tmp/env.output

Wait for /tmp/env.output to be created, then remove the job again. Now compare the contents of /tmp/env.output with the output of envrun in your regular terminal.

A common "gotcha" here is the PATH environment variable being different. Maybe your cron script uses the command somecommand found in /opt/someApp/bin, which you've added to PATH in /etc/environment? cron ignores PATH from that file, so runnning somecommand from your script will fail when run with cron, but work when run in a terminal. It's worth noting that variables from /etc/environmentwill be passed on to cron jobs, just not the variables cron specifically sets itself, such as PATH.

To get around that, just set your own PATH variable at the top of the script. E.g.

#!/bin/bash
PATH=/opt/someApp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

# rest of script follows

Some prefer to just use absolute paths to all the commands instead. I recommend against that. Consider what happens if you want to run your script on a different system, and on that system, the command is in /opt/someAppv2.2/bin instead. You'd have to go through the whole script replacing /opt/someApp/bin with /opt/someAppv2.2/bin instead of just doing a small edit on the first line of the script.

You can also set the PATH variable in the crontab file, which will apply to all cron jobs. E.g.

PATH=/opt/someApp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

15 1 * * * backupscript --incremental /home /root
  • 0
Reply Report

Check /var/log/cron and see if it is actually being run. If it is, try sudo -u to see if it gives errors.

  • 0
Reply Report

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
  • 4
Reply Report