cron job not executing the php script - php

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.

Related

Cron Jobs Not Running at all, Working manually

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');
?>

PHP Cron Page Not Working

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.

set up Cron job to run PHP script

I know this question has been asked before, but none of the answers are working for me.
I'm trying to run a simple PHP script every night at midnight. I created a file called "autoDelete.php" that contains just this code:
<?php
include 'my-database-connection.php';
mysql_query("DELETE FROM meetings WHERE indexDate < NOW()");
?>
I know this script is working because if I navigate to it in a browser, it does what it should.
I then set up the Cron job (via GoDaddy cPanel) to run every minute, with a command to run the script using this:
* * * * /usr/bin/php -q /home/username/public_html/autoDelete.php
However, this is not working. I suspect this has something to do with whatever precedes the "/home" in the command.
Some things to check:
1) cron jobs' default "working directory" is the home directory of the user ID they're running under. That'd most likely be /home/username in this case. That means if you have any relative-pathed include/require commands, they're going to be relative to /home/username, NOT /home/username/public_html. Make SURE that all necessary files are accessible.
2) You're simply assuming the query call succeeded. That's exactly the wrong the thing to do. Calls to external resources (DBs in particular) have exactly ONE way to succeed, and a near infinite number of ways to fail. ALWAYS check for failure, and treat success as a pleasant surprise.
Combining these two (failing to include your connection script, and failing to check for failure), and you end up with what you've got: "nothing" happening. At least try something like
mysql_query(...) or die(mysql_error());
^^^^^^^^^^^^^^^^^^^^^^
The error message will become your script's output, and get emailed to the controlling account's mailbox.
I've had issues in the past with running PHP scripts in a cron job when trying to invoke the PHP binary directly. My solution was to use wget in the cron job since the script was servable by Apache anyway (0 0 * * * wget url/of/script.php). Apache already has the right PHP environment set up so might as well just ask it to handle the job.

php script works fully when called manually, but won't run from cron job

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!

Running PHP script from cron

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.

Categories