P
P
Pisarev_OS2018-02-16 18:02:30
linux
Pisarev_OS, 2018-02-16 18:02:30

Why is the php script not working on request from cron?

Faced some kind of mysticism, or it's time to go on vacation.
There is a script.php with the following content: In the cron there is an entry for it to be executed every minute: But the script is not executed. What was checked before finally going crazy: 1) crontab -l shows that this line is in cron 2) cron log shows that every minute it successfully requests this script 3) log ">> /tmp/temp. txt 2>&1" does not show any errors 4) when directly accessed from the browser - the script runs successfully 5) when accessed from the "php /www/site.ru/folder/script.php" console, the script runs successfully 6) restarted cron, rebooted server (Centos)
<?php file_put_contents('test.txt', 'TEST'); ?>
*/1 * * * * php /www/site.ru/folder/script.php
7) the script has root user rights 0644, also set for test 0777, it did not help
8) in the cron tasks above the line above the launch of this script there is a line to launch another type "*/5 * * * * php /www/site.ru/ folder/drugoe.php" - this script SUCCESSFULLY works on cron
9) changed the file name, threw the script into other folders, etc. - did not help
10) changed the interval - did not help
11) manually created "test.txt"
12) manually created "test.txt" and set permissions to it 0777
-----
13) !specified the wrong name of the script file - in the logs /var/log/cron didn't see any error.
I don't know what to think anymore :(

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Pugachev, 2018-02-16
@Stqs

the file in the script has a non-absolute path,
so when you run the script with cron, it writes everything correctly to the file,
only one cron knows where this file is

T
ThunderCat, 2018-02-17
@ThunderCat

For the future: PHP when called from cron (and from the console in general) uses settings different from PHP configured "out" through Apache.
Therefore:
1) All paths must be absolute, both to the script and inside it
2) Such global variables as $_SERVER, $_GET, $_POST and others will most likely be empty.
3) For debugging the rules, it will be ">> somepath/log.txt 2>&1", but if the script is not executed, nothing will naturally be written there (do not forget that here you need to specify the full absolute path).
4) Nothing prevents you from using a completely sane mvc and objects, it is enough to make a configuration file with the initialization of global variables "manually" and, in some cases, correct autoload. For this, I use a separate clibootstrap and cliController, which can only be accessed from the console)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question