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.
Related
So, i have a script that calls 2 other scripts that back up devices.
Problem is, If i call the main script manually that opens up the other two in a screen session, everything works fine.
The 2 scripts it calls basically sends data to an sql server via a php script.
IE
I have the line
$link = mysql_connect("server ip", "username", "password", "database");
Running manually (Which runs from my user directory) it works fine.
If i have the cron job run, it will run fine to a point when it gets to the php section of the script it gets the error
PHP Fatal error: Call to undefined function mysql_connect() in /export/home/myname/my_Scripts/php_script_its_running
So, being that it works correctly if i manually run it, and not when i run it via cron, I am assuming it is either running a different php instance due to it being run by cron or something oddball like that? or, idk, not 100% familiar with cron jobs yet.
So while i was debugging it more, it looks to be using a different version of php than what my user account is using, 5.1 comparred to 5.4
Not sure if there is an easy way to change it to use my user accounts php setup? Unfortunately i have no root access to the box.
Answer
So, i ran a working script, and added phpinfo(), grabbed the path information and added that to the cronjob so it would overwrite the defaults.
so the top of the cronjob file looks something like
SHELL=/bin/bash
PATH = /usr/kerberos/bin:/app/php-5.4.3/bin
Answer So, i ran a working script, and added phpinfo(), grabbed the path information and added that to the cronjob so it would overwrite the defaults.
so the top of the cronjob file looks something like
SHELL=/bin/bash PATH = /usr/kerberos/bin:/app/php-5.4.3/bin
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.
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!
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've set up a cron job to run. It executes a php file which is named cronj.php
But it doesn't work and cron job notification I get is:
/root/website/myworld/blabla/cronj.php: line 1: ?php: No such file or directory
And line 1 in that file is simply a php tag <?php I don't know how
Cron is executing the file as if it was a shell script. Normally you would put in a shebang line (like #!/usr/bin/env php) at the top of the file so that the shell knows how to invoke it, but PHP doesn't like it - as it outputs everything outside its tags. Thus, instead of this:
0 3 * * * /mypath/myscript.php ...
try this:
0 3 * * * /usr/bin/env php /mypath/myscript.php ...
or use #Ravenex's trick.
EDIT I was just rightly admonished for assuming PHP behaves in a consistent way. Apparently, shebang does work in PHP. My apologies to #chess007.
We use cron to run nightly tasks in a php facebook game. We do it by using curl like this:
/usr/bin/curl http://www.ourdomain.com/page.php
If I remember right we had some issues using localhost to try to avoid external lookups. Also we tried using php command line execution, which mostly worked but caused a few strange bugs.
Try to call the web url (http://.....).
It's apparently not parsing it as an PHP script.
Edit:
Please show use the cronjob you used, to verify my hunch was right.
Use this to set your cron and also give email address in your cron setting Cpanel so that you get an email when cron runs successfully
wget -O - http://YOURSITE/cron.php