By jisuo on
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
Are you sure
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:
---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com
It does print '/index.php'
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?
Are you sure the code within
Are you sure the code within that
ifstatement is executing? Try adding this:to it and manually run cron.php - do you see it print out the text?
---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com
Yes it's running. There is
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...
When Drupal first starts the
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:
---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com
I made the mistake of
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!
This will be true for other
I have this function to decide if it's cron or not.
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.
Thanks Aron,That's an
*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: