G
G
gomerIT2020-11-18 23:25:58
PHP
gomerIT, 2020-11-18 23:25:58

How to setup cron to run php script?

Hello everyone, I ran into a problem with executing a script using cron.
test.php


<?php
define('ROOT', $_SERVER['DOCUMENT_ROOT']);
require_once ROOT . '/core/db.php';
...

If you go to the url, then the script will successfully work.
Put the following command for cron

/usr/bin/php /var/www/user/data/www/domain/modules/test.php

As a result of executing cron, it gives me an error:


PHP Warning: require_once(/core/DB.php): failed to open stream: No such file or directory in /var/www/user/data/www/domain.ru/modules/test.php on line 3
PHP Fatal error : require_once(): Failed opening required '/core/DB.php' (include_path='.:/usr/share/php') in /var/www/user/data/www/domain.ru/modules/test. php line 3

As I understand it, he is trying to connect /core/DB.php relative to the modules folder. How can I solve this problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikita Remizov, 2020-11-19
@niremizov

$_SERVER['DOCUMENT_ROOT']:
* when launched via cron/console (cli) - empty
* when launched via a web server - contains the path to the root directory of the server (probably in your case it is /usr/bin/php /var/www/ user/data/www/domain/)
* more here
To get the current directory, you can use:
* __DIR__ - returns the path to the current file (script)
* getcwd() - returns the current working directory
* more about the difference between the methods here

N
N, 2020-11-18
@Fernus

Use __DIR__ to get the current directory..and relative to it include scripts when run from the command line...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question