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)
Related
I have a set a crontab in my ubuntu 16.04 to fetch data from /var/www/html/import/IMPORT-DATA.CSV through a PHP script.
*/5 * * * * php /var/www/html/cron/import-csv.php
Its working fine, and after fetching the data my PHP script will delete the file (/var/www/html/import/IMPORT-DATA.CSV).
I want to set up a script in Linux (either a crontab or something else) which can run my PHP script once if the IMPORT-DATA.CSV file uploaded in the directory /var/www/html/import/
There's a couple of ways I can think of off the top of my head:
Find out if your FTP server can be configured to trigger a script for you. (For example, pureftpd's upload-script function https://linux.die.net/man/8/pure-uploadscript , not all FTP server software can do this.)
Setup an inotify watcher perhaps with inotify-tools. (You could create your own with PHP as well (inotify extension), but that would probably negate any performance gain since you'd have an instance of PHP constantly running.)
And a sort-of third option: If you're merely wanting to avoid invoking PHP just to see that the file isn't there - you can chain to a bash file test before invoking your PHP script. The cron call still runs every 5 minutes, but only calls PHP if the file is there. (Note that I haven't exactly tested this, but pretty confident it would work.)
SHELL=/bin/bash
*/5 * * * * test -e /var/www/html/import/IMPORT-DATA.CSV && php /var/www/html/cron/import-csv.php
One way is you can distribute your tasks is 3 different scripts and chain them in cron.
*/5 * * * * php /var/www/html/cron/import-csv.php && /var/www/html/cron/YOUR_PERFORM_SOME_OTHER_TASK_PHP_SCRIPT && /var/www/html/cron/YOUR_DELETE-CSV-SCRIPT.php
&& will make sure that, the next script runs only if the previous
ran sucessfully.
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.
As implied in the title, the Cron Job is supposed to execute a php file (update.php, to be specific). The php file then writes to a csv file stored in the same directory.
I have the time set to * * * * * so that it executes every minute. The command is written as follows:
php -q /home//public_html/wallboard/update.php
I don't believe this is causing any errors, though it also doesn't seem to write to the CSV file. When I visit update.php in a browser, however, it executes and writes to the CSV file immediately. I'm not experienced with Cron Jobs and I'm sure there's an issue, but I don't know what exactly that issue is. Let me know if you have suggestions/questions. Any help is appreciated!
Current Command:
* * * * * usr/bin/php -q /home/<user>/public_html/wallboard/update.php
update.php:
<?php
include('lib/HelpDeskView.php');
include('lib/WallboardDisplay.php');
include('helpdesk.csv');
$helpdesk = new HelpDeskView();
$text="\r\ntest,test,test";
file_put_contents( "helpdesk.csv" , $text, FILE_APPEND);
Since your script resides in your public_html directory you can use wget for your Cron Job
wget -O - -q https://yoursite.com/wallboard/update.php
-O - output is written to the standard output in this case it will go to the email address you specify in CPanel
-q quiet mode
IMHO the best way is to contact support and ask them about command line syntax.
This is how I'm doing it at my linux server using cPanel.
This runs script.php which is stored in public root. Course, replace <username> in command line with your username.
At another server I'm using same command line with /usr/bin/php instead of php at the beginning of line, but I'm aware that not all servers use same command line. Some require php-cli in command line instead of php, some don't "like" -f argument, etc. So try various combinations.
To find more suggestions check out this SO topic too: Run a PHP file in a cron job using CPanel
Important thing: When trying different commands wait at least a minute (this case) to see if it works because Cron doesn't fire your script immediately.
Try to execute the same command in PHP CLI and check if it gives you any error, you might be missing some libraries or references required for CLI execution.
/usr/bin/php -d register_argc_argv=On /home/USERNAME/public_html/DOMAIN/artisan AMIR:HOME
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 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.