1

I'm trying to run a php file from a crontab. The aim of the php is to simply send an email to a user. Including the header files of my php framework. But the crontab seems to be having a problem with the paths. I've tried changing it to an absolute path.. see test cases below.

Using require_once(http://www.test.com/inc/header.php) and running php -f test.php to the command line results in:

PHP Warning:  require_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in /home/user/public/test.com/public/deploy/cron/test.php on line 3
PHP Warning:  require_once(http://www.test.com/inc/header.php): failed to open stream: no suitable wrapper could be found in /home/user/public/trybe-ing.com/public/deploy/cron/test.php on line 3
PHP Fatal error:  require_once(): Failed opening required 'http://www.test.com/inc/header.php' (include_path='.:/usr/share/php:/usr/share/pear') in /home/user/public/test.com/public/deploy/cron/test.php on line 3

Using require_once('../../inc/header.php'); and running php -f test.php to the command line results in the php file being carried out successfully.

But looking at my cron tab :

*/1 * * * * php /home/user/public/test.com/public/deploy/cron/test.php

And looking at the results in /var/mail/, it returns this error:

Message 23:
From user@server  Thu Jun 25 13:13:02 2015
X-Original-To: user
From: root@user (Cron Daemon)
To: chrismoore@ibrahimovic
Subject: Cron <user@server> php /home/user/public/test.com/public/deploy/cron/test.php
Content-Type: text/plain; charset=UTF-8
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/user>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=user>
Date: Thu, 25 Jun 2015 13:13:02 +0100 (BST)

PHP Warning:  require_once(../../inc/header.php): failed to open stream: No such file or directory in /home/user/public/trybe-ing.com/public/deploy/cron/test.php on line 3
PHP Fatal error:  require_once(): Failed opening required '../../inc/header.php' (include_path='.:/usr/share/php:/usr/share/pear') in /home/user/public/test.com/public/deploy/cron/test.php on line 3

It seems that the crontab is not able to run the php file as it would when running it from the command line. How can I get this to work?

Can you see what i'm doing wrong and offer a solution?

Thnnks

cwiggo
  • 2,541
  • 9
  • 44
  • 87
  • can you show your folder structure ? – jagad89 Jun 25 '15 at 12:24
  • 1
    Oldskool's answer is right - incidentally `require_once()` will always require a file path rather than a URL (unless you change that setting mentioned in the error message, which I wouldn't recommend) – d0ug7a5 Jun 25 '15 at 12:27
  • Have a look [here](http://stackoverflow.com/questions/6649104/require-once-php-error) – jagad89 Jun 25 '15 at 12:30
  • @jagad89 i'm not the best at command line.. is there an easy way to do show you my folder structure? – cwiggo Jun 25 '15 at 12:41
  • @jagad89 my php file is stored in /home/user/public/test.com/public/deploy/cron/test.php – cwiggo Jun 25 '15 at 12:51
  • 1
    @Chris Have you tried Oldskool's suggestion? It looks perfect. – jagad89 Jun 25 '15 at 12:55
  • @jagad89 yes. Tried that after replying to your comment. Cheers for commenting! – cwiggo Jun 25 '15 at 12:56
  • can you try `require_once( './../../inc/header.php');` note first dot. I am just wondering will it work or not !!!? – jagad89 Jun 25 '15 at 12:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81533/discussion-between-jagad89-and-chris). – jagad89 Jun 25 '15 at 13:01

2 Answers2

9

Try including the file with it's full path by using the __DIR__ magic constant:

require_once(__DIR__ . '/../../inc/header.php');

That way, it will be required with it's full path and work from any directory without having to change to the working directory of your PHP file first.

Oldskool
  • 34,211
  • 7
  • 53
  • 66
  • I think my heads leaking. Thanks for the prompt reply. I've upvoted and marked this as the answer. Thanks again. – cwiggo Jun 25 '15 at 12:54
0

Another way would be changing your crontab to

*/1 * * * * cd /home/user/public/test.com/public/deploy/cron && php test.php
JAyP
  • 75
  • 5