cron jobs fails run php script sometimes - php

I am using php file_get_contents function to get some data from other websites.Also i use cron jobs to run that script automatically.The cronjobs works fine but sometimes fails to run.
this is my command in cron jobs (in cpanel):
*/10 * * * * /usr/bin/php -q public_html/include/imp.php > /dev/null 2>&1
this command should repeat every 10 minutes.but not work sometimes even for 48 hours. it's ok when i run that php script manually.
thank you.

I had a similar issue, the script ran fine when the URL was visited directly, and would run sometimes, seemingly randomly. Turns out it was host server resource issue. Hostgator wont run a cron job when I'm connected via SSH with Putty on 2 computers.
From their help pages
"SSH access is limited to two simultaneous connections on Shared and Reseller plans.
Note: Any cron jobs configured will require one of these sessions to be available in order to run, since cron jobs run under the same shell as SSH."
When I close the SSH connection, the cron job immediately runs.
Maybe your host has a similar rule?

Related

How to run a cron job without using exec or system function in PHP?

Gist
I am trying to run a cron job in PHP. But the exec and system functions didn't work. (Because the server administrator doesn't allow me to use these two functions due to the security reasons) However, I still need to use PHP to run it, because the running time is decided by the formula and it's uncertain. As a result, how can I run the cron job without using exec or system function in PHP?
My thought
The only way I have come up with is adding the cron job via the cPanel, which is set to “once per minute.” That means setup a cron job for every minute to check the target PHP page whether it has anything to do at that moment.
Issue
There's a possible issue if it always checks the page every minute per day, won't it damage the host? (Maybe it will cause a damage to CPU or maybe occupy the memory space, etc.) If so, how to improve it?
You can call a bash script containing a call to PHP-CLI and then the file:
#!/usr/bin/php
/path/to/my/php/file.php
Go to the command line of the server and type "which php". Replace /usr/bin/php above with that which is returned. Then you call this bash script in your CRON.
As you say you have a cpanel server, im guessing its a linux box. You run a cronjob from a linux box by typing crontab -e and then to run a php script. */5 * * * * /usr/bin/php /path/to/file.php to un it every 5 minutes.

Created a plugin for cron job in wordpress but how to connect it with linux without anyone visiting the site

I have worked on creating a plugin for setting a cron job. However it runs only when a person visits it. Can we link it with linux so that it opens the site and the cron job is done without anyone visiting the page. How can we use wget for this if the wordpress is set up locally?
Do this following steps to get your server cron going, enable server cron from plugin is not possible as of now (as i understand).
Disable wp-cron.php
You can disable WP-cron by modifying the wp-config.php (located in the folder where WordPress is installed). Open the wp-config.php file, add a new line after
define('DISABLE_WP_CRON', true);
Set Up a Linux Cron
Warning: It is important that you familiarize yourself with how cron
jobs work. You need to have a working knowledge of Linux commands
before you can use cron jobs effectively.
To set up a Linux cron job:
Log into your cPanel.
In the Advanced section, click Cron jobs.
Under Add New Cron Job, select the time interval. HostGator recommends that you do not set the interval lower than 15 minutes.
Set the cron command to the following, replacing yourwebsite.com with your actual domain name:
wget -q -O - http://yourwebsite.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
The above command tells the Linux server to run wp-cron via wget, which will trigger the wp-cron.php script to do it's job on your schedule instead of on each page view.
Click Add New Cron Job to set the cron.
In order to test out the new cron, simply wait for the elapsed time period for the cron to run. In the event that the cron does not run, please review the steps listed above to ensure that you have completed all steps correctly.
CREDITS:
http://support.hostgator.com/articles/specialized-help/technical/wordpress/how-to-replace-wordpress-cron-with-a-real-cron-job
You can schedule a cronjob.
$ crontab -e
*/1 * * * * curl -I http://example.org/wp-cron.php?doing_wp_cron

drupal 7 How can I get cron jobs to run automatically

I have installed the elysia cron module and setup the cron job.
So far I'm running cron jobs mannually. And it is quite time consuming because I have other things to do than run cron every 2 hours. I read the handbooks on cron configurations, but didn't get much, since I'm not a PHP literate person.
How can I make my drupal run cron automatically??? So that I don't have to come back to my site every 2 hours.
You can use the crontab. Usually this can be accessed on your server by running crontab -e in the shell and then adding an entry to specify how frequently to update your site. For example,
0 * * * * wget -O - -q -t 1 http://{your_drupal_server}/cron.php
would run every hour. Replace {your_drupal_server} with the url of your server. The command assumes you have wget installed as well.
These sites may be helpful:
Drupal's page on setting up crontab -
https://www.drupal.org/node/23714
Wiki's page on cron -
http://en.wikipedia.org/wiki/Cron

cron job on webserver

the last days I have researched about cron jobs. First I want to tell you abaout my problem.
I want to run a php script on my webserver every minute without loading the page. this php script inserts some data via yql. I read that cron is just working with unix/linux. my server runs on unix (wait for it). is it now possible for me to let my script run while sleeping in bed (computer off) just by the server side? do I got this all right? if yes, I also have to know how the path have to be look like in my command part in my schedule, because every example contents something like "* */1 * * * /usr/bin/wget http://www.example.com/cron.php", but why there is /usr/bin/wget in the path (that means, this cron is running just from my computer system not from the webserver) and how to put my schedule on the server and better where. my server have the root public_html where my index.php is inside. please give me an example of the schedule and how to fire my php data called "to_fire.php" that puts the data on my database via mysql.
I hope that some of you know what I exactly mean by own experience. im kinda confused by this command part and how to let it run from just the webserver and not my computer system.
thanks
wget is a unix command to visit a web URL. curl is another command working similarly. You could also use php path\to\script.php to run, as Gigawatt mentioned. There is no big difference from all of them
* */1 * * * is to set cron timing.
/usr/bin/wget is the location where wget on your server
Yes, once cron set, you can leave it alone. As long as server is living, the cron will run as scheduled. You can even set cron to send you an email, but that's no applicable to your case as running per min is to short.
Running cron per min seems not common. If you are doing something like realtime update, consider other solution.
Cron can be set on cPanel.
You need to config your cron script to accept request from same server only, to avoid others access this URL.
Some resource for cron
Configuring cron jobs in cPanel http://drupal.org/node/369267
Backup Databases using a cron http://wiki.lunarpages.com/Backup_Databases_using_a_cron
Configuring cron jobs using the cron command http://drupal.org/node/23714
Managing Cron Jobs with PHP http://net.tutsplus.com/tutorials/php/managing-cron-jobs-with-php-2/

long time cron job via wget/curl?

I am working on cron jobs for my php app and planning to use cron via wget/curl.
Some of my php cron jobs can take 2-3 hours. How to let php work 2-3 hours from cron tab ? Is it good practice to run such long time jobs via cron wget/curl ? Anyone got experience on this ? I also have email queue and i needs to be run every 10 seconds, but cron tab is minute level. Any suggest on this case ?
Thanks for reading.
When you use wget/curl, you are requesting for a page from a webserver. Every webserver will have a time out period, so this might time out.
Also some of the hosting providers may stop the process running beyond certain minutes ( basically done to control the rogue threads).
So it is not advisable to schedule using wget/curl if the job takes more than few minutes.
Try scheduling it using actual scheduler. You can run php from command line
php [options] [-f] [--]
[args...]
php command should be on the path.
You can use the following at the start of your script to tell PHP to effectively never time out:
set_time_limit(0);
What may be wiser is, if crontab is going to run the script every 24 hours, set the timeout to 24 hours so that you don't get two copies running at the same time.
set_time_limit(24*60*60);
Crontab only allows minute-level execution because, that's the most often you should really be launching a script - there are startup/shutdown costs that make more rapid scheduling inefficient.
If your application requires a queue to be checked every 10 seconds a better strategy might be to have a single long-running script that does that checking and uses sleep() occasionally to stop itself from hogging system resources.
On a UNIX system such a script should really run as a daemon - have a look at the PEAR System_Daemon package to see how that can be accomplished simply.
I've just run into the problem with a long running PHP script executed via cron and wget. The script terminates after 15 minutes due to the default timeout in wget.
However I believe this method does in fact work if you set up the correct timeouts. The PHP script you run needs to be set to have an unlimited run time. Best to set this at the start of your long running script.
set_time_limit(0);
When using wget you also need to remove its timeout by passing -T0. e.g.
wget -q0 -T0 http://yourhost/job.php
Be very careful not to overload your server with long running scripts.
If using wget to call the long running cron job, consider some of it's defaults that aren't desirable in this situation. By default it'll --tries 20 times if no data is read within --read-timeout of 900 seconds. So at a maximum set tries to 1 to ensure the cron job is only called once, since you of course wouldn't want a long running script called multiple times while it's still running.
wget -t 1 https://url/
If on cPanel and you don't care for any output emails after each cron job run, also use the following flags -O and -q to hide output, and redirect errors with 2>&1.
wget -O /dev/null -q -t 1 https://url/ 2>&1

Categories