Beni
Beni

Reputation: 149

Crontab PHP execution

Crontab doesn't execute my php file.

I've set up my "crontab -e" as follows

*/1 * * * * /usr/bin/php /var/www/html/books_loader/cron_jobs/CronBooksLoader.php

As you can see I want it to run every minute. My php folder is as it is "/usr/bin/php". My "CronBooksLoader.php" is at the proper directory and it has all the necessary permissions(even 777).

The "CronBooksLoader.php" contains this code

<?php
namespace BooksLoader\Base;

require '../vendor/autoload.php';

$bookObj = new Books();

$loadFiles = $bookObj->load('../xmlData');
$result = $bookObj->upsert();

Which when I start in the browser(http://127.0.0.1/books_loader/cron_jobs/CronBooksLoader.php) successfully loads my data from the XML files in that directory.

I'm not 100% sure if the file is not executed or just that cron doesn't know what to do.

*******UPDATE*******

After all the comments bellow. Cron runs and creates logs. I get error

PHP Warning:  require(../vendor/autoload.php): failed to open stream: No such file or directory in /var/www/html/books_loader/cron_jobs/CronBooksLoader.php on line 4
PHP Fatal error:  require(): Failed opening required '../vendor/autoload.php' (include_path='.:/usr/share/php') in /var/www/html/books_loader/cron_jobs/CronBooksLoader.php on line 4

But I cant figure out how else to give a correct path to it. I want it to remain relative.

Upvotes: 1

Views: 383

Answers (3)

Huy
Huy

Reputation: 114

On certain shared hosting where they may have cPanel running, and using 1H, your crons could be running in a different environment. It would mean you are running in a jailed environment (like can't access files outside your home directory) or limiting your memory and such. See this page for details: http://docs.1h.com/index.php?title=Crond

The short of it:

Exclude an account from the chrooted environment

To exclude a certain user from the chrooted environment you just have to access the cron.exclude folder and create the following empty file:

/var/spool/cron.exclude/USER.chroot

and

Exclude an account from global limits

To exclude a certain user from the global limits you need to create the following file:

/var/spool/cron.exclude/USER.limits

Upvotes: 0

LSerni
LSerni

Reputation: 57418

TL;DR if you don't know better, use curl, lynx or wget.

The script execution takes place in two very different environments depending on whether you use a HTTP client or the PHP binary.

When you invoke it through curl, lynx, or wget, it executes within the web server's environment. When you invoke it through the PHP binary, it executes in the commandline environment (CLI).

One of the most obvious differences is that the work directory is different, therefore this line:

require '../vendor/autoload.php';

means two different things when you run it in /var/www/yoursite/htdocs or when you run it in /tmp (cron's work dir is usually there).

There are lots of other differences. Usually you would use a specific "harness" script that modifies the environment to make it mimic a Web request, and run your script through that. Or you would use a completely different approach in CLI scripts (different DB connection, etc.).

If you aren't prepared to do either, then it's way better to use curl.

Even then, verify that the hostname matches (the machine might not "know" what it's web site address corresponds to, or might not connect to it properly. You might need to either add the web name (e.g. "www.yoursite.com") in /etc/hosts with an address of 127.0.0.1, and/or add a listening port on the loopback interface and a 127.0.0.1 alias for the web site. Failing that, you'd notice that "curl http://www.yoursite.com/cron.php" does not connect, times out, or reports some HTTP error.

Also: in cron scripts, always be sure to add a "2>&1" redirection to capture any stderr messages and have them mailed to you:

* * * * * /usr/bin/curl http://www.yoursite.com/cron_jobs/CronBooksLoader.php 2>&1

(*/1 is the same as *)

=harness example

Not so much a harness than a modification to a file that needs to be executed in both contexts.

<?php
    if (php_sapi_name() === 'cli') {
        // Simulate a Web call.

        $dir   = dirname(__FILENAME__);
        // We must know how deep in the Web hierarchy this file is,
        // and the CLI does not know. We must tell it.
        // The root web directory has depth 0.
        $depth = 1;
        while($depth--) {
            $dir = dirname($dir);
        }
        chdir($dir);

        // Prepare a $_SERVER
        $_SERVER = array(
            'REMOTE_ADDR' => '127.0.0.2',
            // Add as needed.
        );
    }
?>

Upvotes: 2

murtuza hussain
murtuza hussain

Reputation: 498

Make sure cron is installed on your current machine.

On CentOS/RHEL:

yum install cronie

On Linux/Unix:

apt-get install cron

Try using cron on Linux/Unix with crontab -e

See answers about cron for more detail : https://serverfault.com/questions/449651/why-is-my-crontab-not-working-and-how-can-i-troubleshoot-it

Upvotes: 0

Related Questions