S
S
savage_me2020-02-12 17:44:55
PHP
savage_me, 2020-02-12 17:44:55

Why is the file created in /home/admin/ and not /home/admin/domains/mydomain.com/public_html/?

I create a CRON job in DirectAdmin.
/usr/local/bin/php /home/admin/domains/mydomain.com/public_html/1.php >/dev/null 2>&1

Content 1.php
<?php
file_put_contents( '1.txt', '1' , false);
?>

The script runs, but the file is not created in /home/admin/domains/mydomain.com/public_html/ but in /home/admin/.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Ruslan Fedoseev, 2020-02-12
@savage_me

because the cron is running as some user. in this case - from the user admin.
And by default, the current directory is its home.

W
Wexter, 2020-02-12
@Wexter

Because the working folder at this launch will be /home/admin, add to the script at the very beginning, then the working folder will be the one where the script is located
chdir(dirname(__FILE__));

V
Viktor Taran, 2020-02-12
@shambler81

Everything is absolutely correct,
cron works as root and, accordingly, root has a completely different home directory, environment variables, php version, etc., etc.
Solution
1. crontab -e -uUSER NAME - but there are also subtleties with PHP, since they are not quite the same as those specified in the apache nginx configs, because again you did not specify which php.ini file to initialize and which version of which PHP representation.
2. but there is a much simpler way
, this is a cron and it doesn’t matter under which user since we will download a page from the site, which means that environment variables will be loaded from the site, all constants and php will be from the site, the site engine, etc., etc. .
In short, here

50 15 * * /usr/bin/wget -t 1 -O - 'http://site.ua/script.php?p1=a&p2=b'

or
50 15 * * /usr/bin/wget -O /dev/null -q http://test.ru/test/index.php

in general, you got the idea
, so you just run the php file on the site as a regular client.
Manov on the Internet is darkness
https://www.ukraine.com.ua/faq/kak-zapustit-cron-c...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question