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.
Related
I have the following command:
php /var/www/html/XYZ/api_new/XYZ-API/src/public/fetch_events/fetch_events.php
This runs perfectly from the command line. However, I want it to run as a cron job every 15 mins, so I added the following entry to my crontab:
15 * * * * php /var/www/html/XYZ/api_new/XYZ-API/src/public/fetch_events/fetch_events.php
This however, does not seem to work at all. It does not even show up in my system logs (all my other cron jobs do show up).
Any ideas?
There's not really enough information to answer your question, but here are some things that might help.
Sometimes cronjobs don't have context for where to find these programs, so do which php and replace the full path with the php command.
/var/log/cron should have a record of it running, but if there's a problem, you might not know why. Try appending the following to the end of the cron line to aid in debuging:
>> /tmp/fetch_events.log 2>&1
This file will probably contain some hints as to what's going on when the cron does fire.
I'm making a website with PHP5, here's my setup: the website is hosted on a Raspberry Pi running the Raspbian (Debian) OS. I'm using Nginx as a web server with php5-fpm 5.4.39. My website is made using the Atomik Framework and my script is made with the ConsoleKit plugin (it's a little specific but the problem may be not).
So I have my script, and I run it with php /path/to/index.php <command>. It works really great when using it within my shell, but when I try to make cron execute it, it prints me an error saying that $_SERVER['REQUEST_METHOD'] is not defined but that's something I can solve by forcing this variable to some value. After that, just nothing happen and I don't know what to do. I tried everything: different commands, running the command in an outside bash script, with PHP she-bang, none of them works.
Do anyone have an idea of what could cause this? Maybe a different command that cron could run could fix the problem? Is the fact that "no user" (I guess, I'm not a Linux expert) is running the script a problem?
You could trigger the php code through http like this in your cron:
30 11 * * * /usr/bin/wget http://www.example.com/cron.php
To debug the problem, you will need to get access to the logs.
*/1 * * * * full_path_to_your_php_script.php >> full_path_to_your_log_file.log 2>&1
Then check the full_path_to_your_log_file.log file
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.
I have a CakePHP script that should hopefully be run by a cron job. It runs fine from the command line, but seemingly not from the cron. The cron line is something like:
*/2 * * * * cd /path/to/app;../cake/console/cake do_update
The script itself - and this is the bit that I think may possibly be too wacky, to the extent of throwing off the cron - loops through a subset of a Realtors table in the database, using the system time to decide which 50-record slice of the database to update:
$realtors = $this->Realtor->find('all',array(
'conditions'=>array('Realtor.zone_id'=>1),
'order'=>array('Realtor.num DESC'),
'limit'=>50,
'offset'=>date("i")*25
));
So my question(s) is/are - is there anything I'm doing here that would obviously throw the cron job for a loop? And, perhaps more importantly, is my method of splitting a database into manageable chunks over the course of an hour crazy? (I'm pretty much a programming newbie, so I try a lot of things without knowing whether they're good practice or not.) Can anyone suggest a better way of looping through and updating large numbers of database records via a cron, that prevents the individual queries from being too huge for the system to handle?
EDIT: not only does it always work from the command line, it also works when run by cron script on a different server. I guess there's just something messed up on the particular server, so it seems doubtful there's a code-related solution! I'll just accept an answer from among the useful cron-related insights below...
Generally, you'd want to put all your command line stuff into an shell file
/path/to/cake/console/cron.sh
#!/bin/sh
cd /path/to/app
php ../cake/console/cake do_update
*/2 * * * * sh /path/to/cake/cron.sh
Normally I don't care about where the application actually starts (if i'm not using files) and just do
*/1 * * * * php /my/path/my_php.php
I'd try to put "cd /path/to/app;../cake/console/cake do_update" in bash script and run the script in cron instead. But I'm not sure if it'll help.
Try following the manual, it's working great for me:
http://book.cakephp.org/view/1110/Running-Shells-as-cronjobs
I'm not that familiar with cron, but I didn't think you could run two commands together like that.
You might want to try:
*/2 * * * * cd /path/to/app && ../cake/console/cake do_update
This will cause the two commands to run together (since each command does return a number to indicate success or failure, and in the event of failure, an error code)
I am assuming your PHP file also includes the necessary #! line at the start pointing to the PHP interpreter, and that the file permissions allow for execution from the command line? (i.e. you can just literally run that statement above and it works)