0

Im trying to create cronjob from php, I added it to crotanb:

sudo crontab -u www-data -l:

*/1 * * * * /usr/bin/php /var/www/html/service_script/cronTest.php

cronTest.php

<?php
echo 'it works';
include_once('../core/external_init.php');

And this is the output:

it works

PHP Warning:  include_once(../core/external_init.php): failed to open stream: No such file or directory in /var/www/html/service_script/cronTest.php on line 3

PHP Warning:  include_once(): Failed opening '../core/external_init.php' for inclusion (include_path='.:/usr/share/php') in /var/www/html/service_script/cronTest.php on line 3

PHP Fatal error:  Uncaught Error: Class 'pluginClassManagement' not found in /var/www/html/service_script/cronTest.php:6

Stack trace:
#0 {main}
  thrown in /var/www/html/service_script/cronTest.php on line 6

This cronTest.php works when I run it from browser. If I change /usr/bin/php to /usr/lib/php I get this error instead:

/bin/sh: 1: /usr/lib/php: Permission denied

I dont really know what path should be here.

I have php7, i tried all kinds of thing to make this work, setting rights, make the script executable etc but now Im stuck on this and dont know what to do anymore

Honza
  • 323
  • 3
  • 14
  • 1
    When you run from cron it's better to use absolute path. If you run with cli the relative path will refer to your current working directory. As for the `/usr/bin/php` part, you can just put `php`, like `php /var/www/...` – frz3993 Aug 24 '16 at 15:29
  • @frz3993 An absolut path rarely is a good idea. It makes your code non portable without reason. A relative path is perfectly fine for this, that is what the include path setting is for. Note that you (typically) have separate php.ini files for http server and command line versions of php. That allows the definition of different include paths. That makes that setting specific to the local system, which makes perfect sense. – arkascha Aug 24 '16 at 15:35
  • `/usr/lib/php` does not make any sense. Keep in mind that you want to execute the php interpreter from the cron job. So you need an executable, a standalone "program" you can "start" or execute. Check where the php cli executable resides on your system. Use your systems software management system for that. – arkascha Aug 24 '16 at 15:37
  • @arkascha, thanks, I forgot about the include path. Because usually I will use `__DIR__` , `dirname()` or a combination of both to create the path. – frz3993 Aug 24 '16 at 15:52
  • @frz3993 Also questionable, since by that you prevent the option to change between different installed modules or libraries by means of the local configuration. But certainly better than using a hard coded absolute path in a script! – arkascha Aug 24 '16 at 16:05
  • Possible duplicate of [Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew Aug 27 '16 at 09:00

1 Answers1

1

Try it with an absolute include path (like /var/www/path/to/core/external_init.php). I'm pretty sure, it uses the wrong working directory when run as cron.

DBX12
  • 2,041
  • 1
  • 15
  • 25
  • Please see my comment above about this. To fix the "working directory" you should change it, either using a `cd` command, or the php cli option. But the better approach is to use the include path feature php offers for a good reason. – arkascha Aug 24 '16 at 15:38
  • @DBX12 thanks, that did the trick! I had to fix bunch of other errors, but this got it working. – Honza Aug 24 '16 at 16:29