Error in Php using Cron in ubuntu

1

I have a script in php and I want it to run every minute for example, and in the crontab I have it like this:

cd /var/www/html/RestApi/ /usr/bin/php /var/www/html/RestApi/prueba.php

The thing is that if I execute it without being in the RestApi directory, the include I have in my php fails with this error:

  

PHP Warning: include_once (./ vendor / autoload.php): failed to open   stream: No such file or directory in /var/www/html/RestApi/test.php   on line 5 PHP Warning: include_once (): Failed opening   './vendor/autoload.php' for inclusion   (include_path = '.: / usr / share / php') in /var/www/html/RestApi/test.php   on line 5 PHP Fatal error: Uncaught Error: Class   'Illuminate \ Support \ Collection' not found in   /var/www/html/RestApi/test.php:12 Stack trace:    {main} thrown in /var/www/html/RestApi/test.php on line 12

can you help me?

    
asked by Ivheror 13.11.2018 в 10:51
source

1 answer

2

There is a predefined constant called __DIR__ (available as of PHP 5.3) that allows you to use the directory in which a script is hosted to avoid using "." in the relative routes or simply avoid using absolute routes:

<?php
include_once(__DIR__ . '/vendor/autoload.php');

This will allow you to include files independently of the working directory.

On the other hand, it seems that your line of cron is wrong. You must separate the cd instruction from PHP execution with a semicolon ( ; ) or a && :

min hora * * * usuario cd /var/www/html/RestApi/ && /usr/bin/php prueba.php

That way you will first be positioned in the working directory and then the script will be executed (I have omitted the absolute path in the execution because after correct positioning is not required and you ensure that it has been correctly positioned).

Doing it this way will allow you to make your script portable and change the route without having to modify the code to reflect new absolute paths every time you do it.

    
answered by 13.11.2018 / 12:48
source