0

i downloaded cronjob bootstrapper and edited as shown:

#!/usr/bin/php

<?php

/*
|--------------------------------------------------------------
| CRON JOB BOOTSTRAPPER
|--------------------------------------------------------------
|
| By Jonathon Hill (http://jonathonhill.net)
| CodeIgniter forum member "compwright" (http://codeigniter.com/forums/member/60942/)
|
| Created 08/19/2008
| Version 1.2 (last updated 12/25/2008)
|
|
| PURPOSE
| -------------------------------------------------------------
| This script is designed to enable CodeIgniter controllers and functions to be easily called from the command line on UNIX/Linux systems.
|
|
| SETUP
| -------------------------------------------------------------
| 1) Place this file somewhere outside your web server's document root
| 2) Set the CRON_CI_INDEX constant to the location of your CodeIgniter index.php file
| 3) Make this file executable (chmod a+x cron.php)
| 4) You can then use this file to call any controller function:
|    ./cron.php --run=/controller/method [--show-output] [--log-file=logfile] [--time-limit=N] [--server=http_server_name]
|
|
| OPTIONS
| -------------------------------------------------------------
|   --run=/controller/method   Required   The controller and method you want to run.
|   --show-output              Optional   Display CodeIgniter's output on the console (default: don't display)
|   --log-file=logfile         Optional   Log the date/time this was run, along with CodeIgniter's output
|   --time-limit=N             Optional   Stop running after N seconds (default=0, no time limit)
|   --server=http_server_name  Optional   Set the $_SERVER['SERVER_NAME'] system variable (useful if your application needs to know what the server name is)
|
|
| NOTE: Do not load any authentication or session libraries in controllers you want to run via cron. If you do, they probably won't run right.
|
|
| Contributions:
| -------------------------------------------------------------
|    "BDT" (http://codeigniter.com/forums/member/46597/) -- Fix for undefined constant CRON_FLUSH_BUFFERS error if the --show-output switch is not set (11/17/2008)
|    "electromute" (http://codeigniter.com/forums/member/71433/) -- Idea for [--server] commandline option (12/25/2008)
|
*/

    define('CRON_CI_INDEX', '/http://localhost/opensystem/index.php');   // Your CodeIgniter main index.php file
    define('CRON', TRUE);   // Test for this in your controllers if you only want them accessible via cron


# Parse the command line
    $script = array_shift($argv);
    $cmdline = implode(' ', $argv);
    $usage = "Usage: cron.php --run=/login/index [--show-output][-S] [--log-file=logfile] [--time-limit=0] [--server=http_server_name]\n\n";
    $required = array('--run' => FALSE);
    foreach($argv as $arg)
    {
        list($param, $value) = explode('=', $arg);
        switch($param)
        {
            case '--run':
                // Simulate an HTTP request
                $_SERVER['PATH_INFO'] = $value;
                $_SERVER['REQUEST_URI'] = $value;
                $required['--run'] = TRUE;
                break;

            case '-S':
            case '--show-output':
                define('CRON_FLUSH_BUFFERS', TRUE);
                break;

            case '--log-file':
                if(is_writable($value)) define('CRON_LOG', $value);
                else die("Logfile $value does not exist or is not writable!\n\n");
                break;

            case '--time-limit':
                define('CRON_TIME_LIMIT', $value);
                break;
                
            case '--server':
                $_SERVER['SERVER_NAME'] = $value;
                break;

            default:
                die($usage);
        }
    }

    if(!defined('CRON_LOG')) define('CRON_LOG', 'cron.log');
    if(!defined('CRON_TIME_LIMIT')) define('CRON_TIME_LIMIT', 0);

    foreach($required as $arg => $present)
    {
        if(!$present) die($usage);
    }



# Set run time limit
    set_time_limit(CRON_TIME_LIMIT);


# Run CI and capture the output
    ob_start();

    chdir(dirname(CRON_CI_INDEX));
    require(CRON_CI_INDEX);           // Main CI index.php file
    $output = ob_get_contents();
    
    if(defined('CRON_FLUSH_BUFFERS')) {
        while(@ob_end_flush());        // display buffer contents
    } else {
        ob_end_clean();
    }


# Log the results of this run
    error_log("### ".date('Y-m-d H:i:s')." cron.php $cmdline\n", 3, CRON_LOG);
    error_log($output, 3, CRON_LOG);
    error_log("\n### \n\n", 3, CRON_LOG);


echo "\n\n";

?>

i placed it in /usr/cron.php directory where cron.php is the name of the file. i then added the following line in the crontab:

* * * * * /usr/cron.php

expecting that the cron daemon will execute the line which would run the cron.php file. i dont receive any mail. help me where i got lost.

NB:i used the * in every field cos i was testing, to see if it can send every minute.

2
Contributors
1
Reply
4
Views
6 Years
Discussion Span
Last Post by rch1231
0

I had a similar problem my self once. The problem is PHP scripts are designed to be run from a browser or web page and not the command line. When you think about it from that angle the answer is obvious. You either act like a browser or tell the system to use php to process the file.

Shell scripts in Unix (linux) normally have the information about what interpreter to use on the first line. For example:

#!/bin/bash
echo "Hello World"

So with that said you can solve a couple of ways.

  • Add a call to the php interpreter in cron:
* * * * * /usr/bin/php -q /usr/cron.php
  • Use a text based web browser like links to call the script as a web page.
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /usr/bin/links -dump http://mydomain.com/support/auto.php
  • And finally you add what is often called Shebang to the first line of the script to tell it to use php:
#!/usr/bin/php -q
<?php
        echo "First PHP CLI script\n";
        echo exec('ls -l\n') . "\n";
?>

That should cover it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.