Problem with php cron job

16 posts by 2 authors in: Forums > CMS Builder
Last Post: August 6, 2014   (RSS)

By nmsinc - July 29, 2014

Having an issue running a php cron job - see error below along with php file!

ERROR: "setPrefixedCookie: Can't set cookie, headers already sent! Output started in line 0."

<?php
  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = array('/home/claimsca/public_html/','','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  $where2 = "";
  $where2 = 'disabled = 0';
  $where = '';
  $where = 'remove_record_from_listing = 0 and status != "Final"';
  // load records
  list($listingRecords, $listingMetaData) = getRecords(array(
    'tableName'   => 'listing',
    'where'       => $where,
    'orderBy'     => 'num',
  ));
  list($accountsRecords, $accountsMetaData) = getRecords(array(
    'tableName'   => 'accounts',
    'where'       => $where2,
  ));
?>
<?php foreach ($accountsRecords as $record):?>
<?php
                $activeNum     = 0;
                $n = 0;
                ?>  
                <?php foreach ($listingRecords as $record1): ?>
                <?php $n = $n + 1;?>
                <?php if ($record1['in_adjuster'] == $record['num']): ?>
                <?php $activeNum = $activeNum + 1; ?>
                <?php elseif ($record1['in_dispatcher'] == $record['num']): ?>
                <?php $activeNum = $activeNum + 1; ?>
                <?php elseif ($record1['in_processor'] == $record['num']): ?>
                <?php $activeNum = $activeNum + 1; ?>
                <?php elseif ($record1[in_agent'] == $record['num']): ?>
                <?php $activeNum = $activeNum + 1; ?>
                <?php elseif ($record1['i_adjuster'] == $record['num']): ?>
                <?php $activeNum = $activeNum + 1; ?>
                <?php elseif ($record1['i_dispatcher'] == $record['num']): ?>
                <?php $activeNum = $activeNum + 1; ?>
                <?php elseif ($record1['i_processor'] == $record['num']): ?>
                <?php $activeNum = $activeNum + 1; ?>
                <?php elseif ($record1['i_agent'] == $record['num']): ?>
                <?php $activeNum = $activeNum + 1; ?>
                <?php else: ?>
                <?php $activeNum = $activeNum + 0; ?>
                <?php endif; ?>
                <?php endforeach ?>
                <?php clearstatcache();?>
                <?php
                $query = "UPDATE `{$TABLE_PREFIX}accounts` SET
                      
                                            
                      inventory_working          = $activeNum,
                      inventory_total            = $n,       
                      updatedDate                = NOW()

                      
                 WHERE num = '".mysql_escape( $record['num'] )."'";
                 mysql_query($query) or die("MySQL Error:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
                 $userNum = mysql_insert_id();
exit;                 
                 ?>
                 
<?php endforeach ?>

Thanks - nmsinc

nmsinc

By Dave - July 29, 2014

Hi nmsinc, 

Check for and remove any whitespace before the first <?php tag.  Output started in line 0 usually means there was something that got sent on line 0, usually spaces or enters.

And failing that, check none of the plugins you're loading have extra whitespace at the top or bottom (disabling a plugin is a quick way to check, if you don't get an error after disabling the plugin then it's that plugin).

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com

By nmsinc - July 30, 2014 - edited: July 30, 2014

Hi Dave,

I deactivated each plugin one at a time and allowed the cron job to run. I also made sure that the white spaces on the first and last lines of the cron job were removed and results were the same.

Also, if I remove the following three lines of text then I get a record access error!

  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = array('/home/claimsca/public_html/','','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}

Any other suggestions?

Thanks - nmsinc

nmsinc

By nmsinc - August 5, 2014

Hi Dave,

I sent you two emails last week regarding this and no answer - do you have any ideas?

Thanks - nmsinc

nmsinc

By Dave - August 5, 2014

Hi nmsinc, 

Sorry, looks like these got flagged as spam as a false positive (Google Apps Email).  I'll check this out now, thanks for letting me know you didn't get a response!

Dave Edis - Senior Developer
interactivetools.com

By Dave - August 5, 2014

I responded via email.  Can you post back here when you respond just so I can ensure I get it?  Thanks!

Dave Edis - Senior Developer
interactivetools.com

By nmsinc - August 5, 2014

Hi Dave,

I just responded!

nmsinc

By Dave - August 5, 2014

Thanks, it looks like email is getting through now.

The issue was this line:

<?php if (!$CURRENT_USER) { websiteLogin_redirectToLogin(); } ?>

The website login system uses cookies, and when the script is being run from the shell there's no browser or cookies, so you're getting errors related to the login system trying to set cookies.

I replaced that with this line and it now works correctly: 

if (!inCLI()) { die("This script can only be run via the command-line or cronjob."); }   

The inCLI() function returns true if the script is being run from the shell/command-line.  

If you wanted it to run from the shell without requiring a login, and/or run from the web if the user is logged in, you could try something like this:

if (!inCLI() && !$CURRENT_USER) { websiteLogin_redirectToLogin(); }

And for new cron jobs, you might check out the new built-in cronjob system here: /cmsAdmin/admin.php?menu=admin&action=general#background-tasks And the example plugin "Cron Example" under "Plugins".  The new system logs cron output, catches errors, emails admins, makes it easy to manually launch cron tasks from the plugin menu, etc.   I wouldn't bother upgrading to it just yet, but next time you need to adjust a cronjob or write a new one it might be worth a look.  

Hope that helps!  Cheers.

Dave Edis - Senior Developer
interactivetools.com

By nmsinc - August 5, 2014

Thanks Dave I appreciate your help with this!

nmsinc

nmsinc