I don't want cron.php to run certain code. This code is not working, anyone know why?

function mymodule_nodeapi(&$node, $op) {
  if ($node->type == 'mymodule') {
    switch($op) {

      ...

      case 'view':
        if ($_SERVER['SCRIPT_NAME'] != '/cron.php') {
          //don't want cron to enter here
          //do my code
          //redirect to another page
        }
        break;	
    }
  }
}

Comments

yuriy.babenko’s picture

Are you sure $_SERVER['SCRIPT_NAME'] is equal to '/cron.php'? This will only be the case if your site is in the root of the web server, otherwise it will also contain the path to the folder which houses your Drupal site.

Try this:

if(!preg_match('/\/cron.php$/', $_SERVER['SCRIPT_NAME'])) {
    //your code here
}

---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com

jisuo’s picture

It does print '/index.php' when ever I try to print it on normal pages, so I assumed it would be '/cron.php' since they are in the same folder. I'll try the regular expression, thanks!

edit: did not work, is there any other way to see if cron is running?

yuriy.babenko’s picture

Are you sure the code within that if statement is executing? Try adding this:

echo 'here';
exit();

to it and manually run cron.php - do you see it print out the text?

---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com

jisuo’s picture

Yes it's running. There is some redirect code there (to handle with changing site language). If I run cron manually the cron is interrupted and I get redirected to the page specified there (in the case "view":). If I change the if-statement to <?php if ($_SERVER['SCRIPT_NAME'] == '/cron.php') ?> or <?php if(preg_match('/\/cron.php$/', $_SERVER['SCRIPT_NAME'])) ?> or using strpos or similar cron runs just fine. It never enters that if-statement.

So the problem seems to be that I can't identify that cron.php is running by this method...

yuriy.babenko’s picture

When Drupal first starts the CRON process, it sets a variable called 'cron_semaphore' to the current timestamp. (See line 2686 in includes/common.inc). It then deletes this variable when CRON is completed.

You can check for 'active' CRON by checking this variable:

$semaphore = variable_get('cron_semaphore', FALSE);

if(!$semaphore || (time() - $semaphore > 3600)) {
 //your code
}

---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com

jisuo’s picture

I made the mistake of "Running cron manually" instead of entering /cron.php in the addressbar manually.

It IS working correctly if you do this way. If you click "run cron manually" it will be "/index.php" instead.

Me = fail.

Big thanks for the help still!

aron novak’s picture

I have this function to decide if it's cron or not.

function smallhacks_is_cron() {
  if (php_sapi_name() == 'cli') {
    // This maybe means cron via Drush
    return TRUE;
  }
  if ($_SERVER['SCRIPT_NAME'] == '/cron.php') {
    return TRUE;
  }
  if (arg(0) == 'admin' && arg(1) == 'reports' && arg(2) == 'status' && arg(3) == 'run-cron') {
    return TRUE;
  }
  return FALSE;
}

If your site is not under the / , instead of the second condition, you need a regexp.
Of course there are other ways of executing cron (3rd party modules?) where it may fail.

_vid’s picture

*Edited: fixed preg_match syntax.

Thanks Aron,
That's an excellent function.
"php_sapi_name() == 'cli' " has eluded me for a year.

I took your advice and used the regexp from above.
Here's the full function now:

function smallhacks_is_cron() {
  if (php_sapi_name() == 'cli') {
    // This maybe means cron via Drush
    return TRUE;
  }
  if(preg_match('/\/cron.php$/', $_SERVER['SCRIPT_NAME'])) {
    return TRUE;
  }
  if (arg(0) == 'admin' && arg(1) == 'reports' && arg(2) == 'status' && arg(3) == 'run-cron') {
    return TRUE;
  }
  return FALSE;
}//end function smallhacks_is_cron()