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.
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";
PHP Script is not running via cron job
The problem was the variables of the SDK to connect with IBM Infomix database. Although I had set the PATH, it was necessary to export the environment variables from IBM Informix that I define when I did install the Informix SDK. The strange was that even with the PDO exeptions no one error was generate when I redirect error standard output with &>.
May exist an most elegant way to do that but I can't play with live environment. Follow that cron job that currently works:
SHELL=/usr/local/bin
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/IBM/informix/bin
14,29,44,59 * * * * root export INFORMIXSERVER=<data_source_name> && export INFORMIXDIR=<path_to_informix_sdk> && export INFORMIXTMP=<path_infomix_tmp> && export INFORMIXSQLHOSTS=<path_to_sqlhost_file> && export $PATH:$INFORMIXDIR/bin && /usr/bin/php /opt/project/script.php &> /var/log/project/task.log
php cron jobs not running after moving to php 7.2
I have found issue for this it was path issue, cron jobs were not including up file above directory
include(../config.php); // this file was not including
so I found solution and changed the path to this
include("/var/www/html/config.php");
now all cron jobs are working fine
Cron Jobs Not Running at all, Working manually
The __DIR__
would give the script working directory so when using require
it would always translate the path correctly
example:
<?php
define('DIR', __DIR__);
require_once(DIR.'../backend/backend.php');
?>
CronJob not running
Finally I found the solution. Following is the solution:-
Never use relative path in python scripts to be executed via crontab.
I did something like this instead:-import os
import sys
import time, datetime
CLASS_PATH = '/srv/www/live/mainapp/classes'
SETTINGS_PATH = '/srv/www/live/foodtrade'
sys.path.insert(0, CLASS_PATH)
sys.path.insert(1,SETTINGS_PATH)
import other_py_filesNever supress the crontab code instead use mailserver and check the mail for the user. That gives clearer insights of what is going.
cron job does not read & write to log file when executed the cron
Maybe you are specifying the wrong path.
Try this
/path/to/php -f /path/to/script.php >> /path/to/logfile.log
Related Topics
How to Capture PHP Output into a Variable
PHP File Listing Multiple File Extensions
Php: Check If a File Is Loaded Directly Instead of Including
File_Get_Contents Returns 403 Forbidden
Can't Send Email to Addresses at My Own Domain
Destroy or Unset Session When User Close the Browser Without Clicking on Logout
Storing Form Data as a Session Variable
PHP - Include a PHP File and Also Send Query Parameters
Mysql_Real_Escape_String VS Addslashes
Add Extra Meta for Orders in Woocommerce
How to Sort an Array of Utf-8 Strings
How to Use Open Id as Login System
Shorthand for Arrays: Is There a Literal Syntax Like {} or []
Instantiate a Class with or Without Parentheses