So I have an issue where my cron job isn't running. I've never used cron before so it might just be me not knowing how to set this up properly, but I don't think it is. I set up the cron job in cpanel, and it runs to my main website directory where "test.php" is located.
It works absolutely fine if I manually go to the page(By entering the domain/test. But doesn't work at all with cron, so I know this issue isn't with the function but with the cron. I currently have it set to run every minute, just for testing purposes. Any help would be greatly appreciated, thanks :)
Cron:
* * * * * /usr/local/bin/php -q /home1/website/public_html/test.php
PHP
<?php
require("../backend/backend.php");
$id = "4127684511423";
sendChannelMessage($channelid);
?>
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');
?>
Related
I have an Apache server that I'm hosting from.
I have a php page that sends emails based on some script that looks like this:
<?php
chdir(dirname(__FILE__)); //need this line so cron works! cron doesn't know
the relative file paths otherwise.
require_once 'core/init.php';
$db = DB::getInstance();
$company = new Company(1);
require 'added-assets/plugins/phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
if($company->find_yesterday_appts(1)) {
.... email based on query....
From cpanel I have the cron job set to run every day at 12.
0 12 * * * php -q public_html/personnellonline/email_yesterdays_appts.php
core/init.php contains my connection string.
There are no errors I was told on the server, but no emails are ever sent when the cron runs. If I go to the page directly though then the query runs and emails are sent!
I once solved this issue by adding:
chdir(dirname(__FILE__)); //need this line so cron works! cron doesn't
know the relative file paths otherwise.
But not I'm back to square one. Any thoughts on what could be the issue?
When you say "I go to the page directly", how exactly are you getting things to work? Does running the PHP command you showed us work without using cron?
If you are loading the page through a web server, that is not the same as executing the file through PHP as you are doing in your cron command. If your script needs to be run through the web server, try using curl to load the URL that works in your cron command.
The only other thing I could think of, if running the PHP command actually works when not run through cron, would be that you may need a specific user to execute your cron command.
GracefulRestart, I changed the command to use CURL which I never used or heard of. It works now, thank you. I'm learning about CURL now as I read online.
ArtisticPhoenix, you are correct. I should move this outside the public area of my domain. I will do this!
The following works now:
curl -s "https://www.example.com/email_yesterdays_appts.php"
Thank you for your comments and help.
I have a PHP script that essentially copies a zip file, unzips it, and then copies the resulting text files to a MySQL database.
This works perfectly when I call my php page manually, but this needs to be done every day, so I want to set this up as a cron job.
I have a cron job that works, and I have verified that by calling a very simple script, but it fails when trying to run my full page.
I have tracked this down to two areas of code. Firstly:
date_default_timezone_set('Europe/London');
I understand that I can set this up through an htaccess or php.ini file, so I am not concerned about this one.
However, the second area of code that is stopping the cron job from running is:
$localzipfile = "myfolder/myzipfile.zip";
$localzipfilefullpath = "/homepages/20/dXXXXXXXXX/htdocs/sub/folder/" . $localzipfile . "";
$localpath = pathinfo(realpath($localzipfile), PATHINFO_DIRNAME);
$zip = new ZipArchive;
$res = $zip->open($localzipfilefullpath);
if ($res === TRUE) {
$zip->extractTo($localpath);
$zip->close();
}
Again, this all works perfectly when I run the code manually, so I know everything is in place and works. All paths are correct, and the zip file unzips as expected.
It is only when I try to run this as a cron job that it fails and doesn't unzip the file.
I have seen several other SO questions about php files that run manually but don't run as cron jobs, but none that relate to the unzipping of a local zip file.
UPDATE:
I have managed to log the error output from the cronjob, and it is reporting this:
"Cannot instantiate non-existent class: ziparchive"
I don't understand this, though, as the code all runs without issue when I run it from the browser?
Are you using relative pathes for $localzipfile and $localpath?
If yes, try to use absolute ones or write a shell script that chdirs into the PHP script's directory before running PHP.
The paths to your file need to be absolute, for example:
$localzipfile = "/home/myfolder/myzipfile.zip";
$localzipfilefullpath = "/var/www/html/homepages/20/dXXXXXXXXX/htdocs/sub/folder/" . $localzipfile;
Are you sure that the user running cron has permissions to access both directories? Cron might run under a different user which cannot access your /myfolder or /html dir, so make sure to give the users apropriate rights. You can see those two answers Answer1 | Answer2 I posted some time back on how to set up permissions.
Finally managed to get to the bottom of this, after much testing and investigating!
Thanks to all who gave suggestions, but it seemed that when I was running the script through a browser, it was running at PHP version 5.4, but when the cron job was running, it was running at version 4.9!
Changing my cronjob calling script from:
0 * * * * /usr/bin/php
to
0 * * * * /usr/bin/php5
solved the zip problem, and the cronjob now unzips my file correctly!
I am trying to run a PHP script through a cronjob. I already did this hundred of times, but now it's not working and I cannot figure out why.
I created a script called update_db.php in /var/www/html/ When I run the script by hand:
php /var/www/html/update_db.php
everything works fine. When I put this into a cronjob, it does nothing. My cronjob:
* * * * * /usr/bin/php /var/www/html/update_db.php
I tried to put a bash script in front of it that calls the PHP script, but, again, it only works when calling by hand, not from a cron.
There are no errors in the syslog. Also no mail in /var/mail. I restarted cron already, but no effect.
I use ubuntu 14.04.
Can anyone help me?
Is * * * * * php /var/www/html/update_db.php not working? You shouldn't need to use /usr/bin/php.
Also, check to make sure crons are running on your current system and that your files/directories have the appropriate permissions to be run by the cron.
CRON "should" be logging.
Check the /var/log/cron, looking for your script errorrs or otherwise.
some implementations of cron need a full on restart - I have personanlly never had this issue, but I know fellow admins who have spent far too much time chasing fault, when a simple restart did the trick.
today i learned about cron jobs, i opened SSH and followed along with the 1and1 cron job tutorial, the tutorial file and instructions worked fine however when i did the same steps but with my own PHP script it didnt work, below is the cron job command i used
* * * * * /usr/bin/php /path-to-webspace/heal.php
and below is the heal.php file, this file works as intented without cron as i tested it beforehand
<?php
include('onedirectorydown/database_connection.php');
$resetAllHealth = "UPDATE users SET Health = Health + 1000";
$executeAH = mysqli_query($dbc, $resetAllHealth);
?>
i want it to execute every minute as im just testing it to see if it works but it doesnt, however the sample in the 1and1 tutorial worked and i basically followed along exactly, i just the file contents to whats in the heal.php
could someone tell me why it is not executing?
Common issues with CRON jobs are that required include files aren't working properly. For example:
include '/database.php';
Might try to include /var/www/database.php when executed from the browser, but might try to include /home/username/database.php when executed from the CRON job of the appropriate user.
Consider replacing calls with
include $_SERVER['DOCUMENT_ROOT'].'/database.php';
and seeing if that helps.
Looking at your last comment, maybe you don't have the right mysqli extension installed into the php.ini file for cli (usually under /etc/php5/php.ini).
Remember: "apache" scripts and "command line" scripts have two different php.ini files.
I wonder whether someone may be able to help me please.
I've been working on this for days, looked at a whole host of documentation, but I can't seem to get it right.
I'm trying to run a php script as a cron job, the file is contained on a server under the following address:
mapmyfinds.co.uk/development/cronfile.php
I understand the numeric vales or * that I need to enter at the beginning of the line of code but it's what comes after that which I'm struggling with.
I'm using a Linux server with 1and1, and at the moment my line of code reads:
* * * * * /usr/local/bin/php /development/cronfile.php. I've tried changing it to usr/bin/php, than changing the second part of the code to mapmyfinds/development/cronfile.php, all without any success.
I just wonder whether someone may be able shed some light on this please so I can get the job to run correctly.
Many thanks
Did you specify the user you want to run the script with ?
The correct syntax is
* * * * * root /usr/bin/php /path/to/your/script.php
For every minute, runned as root.
Also, like lexalizer proposed, try running your script manually from the shell, to see what is it outputting.
Try to get the full production PHP path by using the output from phpinfo(); Also put the full path to the cronfile.php file, not just the relative one.
Finally, before running the cron job, try to run that command manually to see what it does/outputs.