2

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?

11
  • 1
    Do these scripts generate any output?
    – Sjoerd
    Aug 5, 2010 at 13:40
  • 9
    Perhaps it can't find php. Try specifying the full path (i.e. /usr/bin/php)
    – Wolph
    Aug 5, 2010 at 13:40
  • 2
    does php -f sendBulletinEmails.php run okay from the command line ?
    – JoseK
    Aug 5, 2010 at 13:41
  • 3
    Why all cron values are "*"? Is it supported on your system? Aug 5, 2010 at 13:41
  • 1
    is this a user crontab or /etc/crontab? for the latter, you must have at least a user called "cd" for it to work.
    – mvds
    Aug 5, 2010 at 13:44

9 Answers 9

2

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
7
  • thats a good solution, but i need it to be secure too
    – Garrett
    Aug 5, 2010 at 21:05
  • Whats insecure about this method? you can always hack sendQueuedEmails.php to execute only if accessed from localhost.
    – sajal
    Aug 6, 2010 at 8:37
  • 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>
    – Garrett
    Aug 6, 2010 at 20:22
  • in the other thread i think you mentioned you can access <pre>localhost/cron/sendQueuedEmails.php</pre> ... however you can use this url <pre>actualdomain.com/cron/sendQueuedEmails.php</pre> ... use your actual subdomain/domain hosting this file... the same url ud need to access via a browser. but make sure to modify this script to allow access ONLY from the server IP!
    – sajal
    Aug 7, 2010 at 18:44
  • ignore the <pre></pre> bit... the system was removing http:// portions from my previous comment
    – sajal
    Aug 7, 2010 at 18:46
1

Heed my advice grasshopper! Been here, done this. Do this the easy way and use the cgi version of php "/usr/bin/php-cgi". If you don't have it, install it "apt-get install php5-cgi"

Don't believe me? Try this.

In your directory create a text file called test.txt and add some random text inside. Now create a new directory called test and a file inside it called test.php then add this code:

<?php

include '../test.txt';

?>

Your directory structure should look something like this:

/yourdirectory
/yourdirectory/test.txt
/yourdirectory/test/test.php

Change to the root directory "cd /" and run this from the commandline:

/usr/bin/php -f /yourdirectory/test/test.php

and then

/usr/bin/php-cgi -f /yourdirectory/test/test.php

See the difference?

0

A more simple way to test whether a line is runnable with cron:

env -i bash -i /dev/null <actual command>
0

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

9
  • i dont have '/var/log/cron'. i guess that means its not running?
    – Garrett
    Aug 5, 2010 at 14:56
  • which flavour of Linux are you running?
    – James L
    Aug 5, 2010 at 15:04
  • ubuntu. i updated my question with some new info =)
    – Garrett
    Aug 5, 2010 at 15:10
  • does /var/www/cron/includes/Swift-4.0.6/lib/swift_required.php exist? It looks like the includes are expecting a different working directory.
    – James L
    Aug 5, 2010 at 15:14
  • no, /var/www/includes/Swift-4.0.6/lib/swift_required.php exists (without cron/). how can i make it look there?
    – Garrett
    Aug 5, 2010 at 15:37
0

Also - I would try using the full path to the php executable. Usually it is '/usr/bin/php' but you can issue 'which php' to double check that for your system.

Your entries would look something like this:

          • cd /var/www/cron; /usr/bin/php -f sendBulletinEmails.php
          • cd /var/www/cron; /usr/bin/php -f sendSurveyEmails.php

Also, if you only changing the directory so that the files are in the current directory, you can always modify the cron to be:

          • /usr/bin/php -f /var/www/cron/sendBulletinEmails.php
          • /usr/bin/php -f /var/www/cron/sendSurveyEmails.php

Hope this helps.

1
  • /usr/bin/php is the full path to the php executable
    – Garrett
    Aug 5, 2010 at 16:56
0

Right now, these scripts may be sending their output to the bitbucket, and you won't know why they are failing. Your cronjobs may not be configured to send email, or email on the system may not be working.

I'm not a fan of emailing cron output, partially because people always think it's ok to spam the output to 'root', and I'm the one who receives that email. Since in most cases we're talking about 1-2 lines of output, I suggest you log the output of your cronjobs to syslog.

* * * * * { cd /var/www/cron && php -f sendBulletinEmails.php ;} |/usr/bin/logger -t sendBulletinEmails
* * * * * { cd /var/www/cron && php -f sendSurveyEmails.php ;} |/usr/bin/logger -t sendSurveyEmails
3
  • where is the log located?
    – Garrett
    Aug 5, 2010 at 18:33
  • @Garrett: This will be sent through syslog, so it will be sent to one of the standard logfiles under /var/log . Distros are different, but ou can see the details in /etc/syslog.conf . It will probably be under /var/log/messages. Aug 5, 2010 at 18:42
  • 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?
    – Garrett
    Aug 5, 2010 at 19:36
0

Add the folder containing the "includes" folder in your include path. To find it use locate swift_required.php

2
  • how? and is locate a php command?
    – Garrett
    Aug 5, 2010 at 18:35
  • 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.
    – greg0ire
    Aug 5, 2010 at 22:21
0

Based on your original question, and the code you posted, you're missing a shebang line which you'd need to run your PHP script this way. Try changing the first few lines to this:

#!/usr/local/bin/php5 -q
<?php

// your script here

?>

And you should have success.

Of course replace /usr/local/bin with the correct path to your PHP install. It may/may not be the same. You only need this initial line to run it as a cron job, not if you're running it from the browser.

2
  • 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
    Aug 5, 2010 at 18:31
  • 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).
    – ESW
    Aug 5, 2010 at 18:39
0

Hey... howz it hangin essa? Here are two solutions to your problem.

  1. Use full paths for include files inside your scripts. Need to do this for all includes. If the include file has other includes, you'll need to update those as well.

include '../somefile.php' should be include '/somedir/somefile.php'

  1. If #1 won't work or you don't want to use full paths to includes, then try using php-cgi instead of php cli version.

Also read this section of the php manual paying close attention to cli and cgi differences.

http://www.php.net/manual/en/features.commandline.differences.php

The CLI SAPI does not change the current directory to the directory of the executed script!

Kick @ss!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .