Cron not executing PHP script
You are most likely giving it the wrong php executable path :)
On MY system it would be:
* * * * * /usr/bin/php /var/www/cron/automatedScript.php
To confirm the correct path to use execute:
nm@vp:~$ which php
And this will return you the path similar to:
/usr/bin/php
Cron job is not running the php script
$_SERVER['DOCUMENT_ROOT']
is created by your webserver (e.g. Apache) before calling PHP. If you're running your script via a cronjob, you're bypassing your webserver and the DOCUMENT_ROOT
will not exist.
You should determine the location of your configuration file in another way. One way is to use PHP's built-in constants for file locations, like __FILE__
or __DIR__
:
$dir = __DIR__ . "/configuration.php";
__DIR__
refers to the folder containing the current file, so this will become /home/user/public_html/folder/subfolder/configuration.php
.
From what you're doing with $_SERVER['DOCUMENT_ROOT']
I assume your configuration file is one folder up from public_html
, so you could do something like this:
$dir = __DIR__ . "/../../../configuration.php";
crontab not running php script
The syntax * * * * * /var/www/html/testsession.php > /var/www/html/log
is valid.
Most likely since it is the final line in the crontab it is missing the newline. Cron requires a newline at the end of every entry; in other words your crontab must finish with an empty line.
From the "Diagnostics" section of man crontab
:
cron requires that each entry in a crontab end in a newline character. If the last entry in a crontab is missing the newline, cron will consider the crontab (at least partially) broken and refuse to install it.
You may want to replace the >
with a >>
so that new content is appended to the log file rather than overwriting it every minute, ie * * * * * /var/www/html/testsession.php >> /var/www/html/log
. This will still create the log file if it is not already present.
Your PHP file will also need the execute bit set and will need to start with #!/usr/bin/php
(or the path to PHP on your system) on the first line. Alternatively you could replace the cron line with * * * * * /usr/bin/php /var/www/html/testsession.php >> /var/www/html/log
to explicitly use the PHP interpreter to execute the script.
Cronjob not running php script
Can you share you error-message with us? I assume this will help a lot to find the issue.
I've shared my insights how to enable logging in a different post on stack overflow (see link below). This will explain how you can display errors in your cron execution:
https://stackoverflow.com/a/60250715/12880865
Please let me know if this helps you. If you'll get a proper error message, please share it with us so we can dig further into your question.
Cronjob not executing php?
Try requesting the file through your web server rather than calling the script via the command line PHP interpreter.
*/10 * * * * wget -q -O /dev/null http://localhost/gapi/src/test2.php
(-q
to suppress output, -O /dev/null
to redirect file output so it doesn't save it)
or using curl
instead:
*/10 * * * * curl --silent http://localhost/gapi/src/test2.php
The URL will depend on how your server is set up - you say it works through your browser at the moment so just use the same URL in the cron file.
php cron job not running
You need to use full paths in your scripts. Otherwise, cron
won't know where is that file.
so instead of
$my_file = 'file.txt';
use
$my_file = '/path/to/file.txt';
Probably you were getting file.txt
stored somewhere in /
.
Note crontab
runs in a limited environment, so it cannot assume anything regarding to paths. That's why you also have to provide the full path of php
, etc.
From Troubleshooting common issues with cron jobs:
Using relative paths. If your cron job is executing a script of some
kind, you must be sure to use only absolute paths inside that script.
For example, if your script is located at /path/to/script.phpand
you're trying to open a file called file.php in the same directory,
you cannot use a relative path such as fopen(file.php). The file must
be called from its absolute path, like this: fopen(/path/to/file.php).
This is because cron jobs do not necessarily run from the directory in
which the script is located, so all paths must be called specifically.
Related Topics
How to Use PHP Reserved Names for My Functions and Classes
Using Square Brackets in Hidden HTML Input Fields
Gmail Fsockopen(): Ssl Operation Failed Error with Codeigniter and Xampp
Why Are Floating Point Numbers Printed So Differently
Separate Space-Delimited Words in a String
How to Handle Double Quotes in String Before Xpath Evaluation
Password_Hash Returns Different Value Every Time
How to Redirect After Download in Laravel
Simple PHP Contact Form with Firebase Hosting
PHP Simplexml: Insert Node at Certain Position
Get All Instances of a Class in PHP
How to Pass Jquery Variables to PHP Variable
Redirect to Page and Send Custom Http Headers
Best Methods to Clean Up a Hacked Site with No Clean Version Available