I am trying to add a php cron job to my website which is hosted on a remote server. The path of the cron job file is "/home/userid/domains/mywebsite.com/scripts/cron1.php". I have tried the following commands and none of them seem to execute the cron file:
/usr/bin/php /home/userid/domains/mywebsite.com/scripts/cron1.php
cd /home/userid/domains/mywebsite.com/scripts && /usr/bin/php cron1.php
My hosting account does not show any errors for the cron results.
However, if I move the cron file inside the public_html directory and add a cron job with the command "curl https://mywebsite.com/cron1.php", it works as expected. But I want the cron file outside the public_html directory and so I know I cannot use a curl command.
Please let me know what I am doing wrong or how I can get the cron job to work when the cron file is outside the public_html directory.
Thank You, Clement
Edit:
My directory tree is:
user
|- domains (755)
...|- mywebsite.com (755)
......|- data (755)
.........|- cronresults.php (644)
......|- scripts (755)
.........|- cron1.php (644)
Edit:
<?php
$path = dirname(__DIR__) . "/data/cronresults.txt";
file_put_contents($path, "random text\n", FILE_APPEND);
?>
An alternative is to use the lynx command (but you must have lynx installed, say yum install lynx )
1 * * * * /usr/bin/lynx http://mywebsite.com/cron1.php -dump
If you want to use PHP instead , you may try to change
<?php
$path = dirname(__DIR__) . "/data/cronresults.txt";
file_put_contents($path, "random text\n", FILE_APPEND);
?>
to absolute path (such as the following, but adjust it according to your real path)
<?php
$path = "/var/www/websitename/data/cronresults.txt";
file_put_contents($path, "random text\n", FILE_APPEND);
?>