CodeIgniter Forums

Full Version: Cronjob outputs welcome message [SOLVED]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
My webhosting (strato.de) supports the use of cronjobs.
I have a controller named Taskplanner, and a method named Test.
The only thing this method does is:
PHP Code:
echo "This is a test!"

My website is inside a folder on the hosting platform, let's say testwebsite.
The cronjob command is set up like this:
Code:
/bin/php -f ./testwebsite/index.php taskplanner test

I received an e-mail when the cronjob was executed.
Instead of "This is a test!" it contains the complete ouput of my welcome page (from my default controller, NOT from Taskplanner), including all html code, starting with "<!DOCTYPE html> and ending with </html>
In between, I see the html header where css and Javascript were loaded. This caused some error messages, which are also displayed. At the end, there's a database error because my session library tries to write the IP-address into the database. But due to the type of request, there is no IP-address.

What's going wrong here? I believe I set up the cronjob according to the CI documentation, but apparently, I'm making some mistake. Anyone else who is a STRATO customer and has successfully set up cronjobs for CodeIgniter? STRATO can't help me. They say the cronjob function works the way it was designed. Calling a plain php script will work OK. But I don't want to fall back to traditional php scripts and miss all the good stuff CodeIgniter provides.

Any help would be appreciated.
I remember reading once that CI had issues with CLI going through the index.php file when I needed to run a complicated CLI crontab.


You're welcome to try it (see attached), however,  it's based on CI 3.0.0 dev and is a modified copy of PyroCMS v 2.2 (I did some obvious editing, but likely didn't get it all) community index.php file so you may need to copy the relevant bits out into a copy of your own index.php...

call it like you have been but instead of index.php call whatever you name this file.  IE: 
PHP Code:
/bin/php -./testwebsite/ci_cli.php taskplanner test 

I'm not sure you need the `-f` flag, but I didn't look into that for crontab.  

Good luck.
@enlivenapp: unfortunately, it makes no difference. Still getting the html output of the home page.
It looks like CI doesn't detect it's a cronjob at all. It tries to handle the request as if it's a regular http request.
Try searching for "cron jobs session variables."

Cron cannot access cookies but someone may have found a workaround.

Tapped tediously on a tablet Sad
(12-24-2016, 11:48 AM)Wouter60 Wrote: [ -> ]@enlivenapp: unfortunately, it makes no difference. Still getting the html output of the home page.
It looks like CI doesn't detect it's a cronjob at all. It tries to handle the request as if it's a regular http request.

What package do you have from Strato?
The package is "Powerweb Advanced".
There's some progress now.

I copied index.php to index_cronjobs.php and set the environment to "cronjobs".
In application/config/cronjobs/config.php I put this line:
PHP Code:
$config['enable_query_strings'] = TRUE
I also put an autoload.php in the new folder, that only loads the database library, not session and ion_auth.

My cronjob now:
Code:
/bin/php -q ./testwebsite/index_cronjobs.php c=taskplanner m=test

Finally, the correct method is running!!
There's one problem left:
PHP Code:
$this->input->is_cli_request(); 
returns FALSE
How can I fix that?
I did a simple test to see which value PHP_SAPI has.
On my WAMP server: apache2handler
On my hosted platform via http: cgi-fcgi
When running a cronjob on my hosted platform: cgi-fcgi

So, the is_cli() function can't detect if it's a cronjob or not.
When I replace /bin/php by /bin/php-cli I get an error: /bin/php-cli not found.

Any ideas?
I've found a workaround, which involves modifying the is_cli() function inside the system/core/Common.php file:
PHP Code:
function is_cli()
{
 
  //the old part was:    
 
  //return (PHP_SAPI === 'cli' OR defined('STDIN'));  

 
  //this will do the trick":
    
if( PHP_SAPI == 'cli' OR defined('STDIN') )
    {
        return 
TRUE;
    }
    
    if( empty(
$_SERVER['REMOTE_ADDR']) and !isset($_SERVER['HTTP_USER_AGENT']) and count($_SERVER['argv']) > 0
    {
        return 
TRUE;
    } 
    
    return 
FALSE;


I know I shouldn't modify any system files, but what other options do I have?
set a cronjob and print out the value from PHP_SAPI
(12-26-2016, 11:25 AM)Paradinight Wrote: [ -> ]set a cronjob and print out the value from PHP_SAPI

Well, I did. please read my post from yesterday 7.38 PM.
It turns out that the PHP_SAPI value is the same as when I open my website with a browser.
Pages: 1 2