I am developing a plugin that needs a piece of code to run every hour even if no visitors have been on the site that day.
The goal is to have a plugin that i can activate and that just works without asking the clients to set up a cron-job inside of their server settings.
From what i understand wp-cron is not an option here since it only activates on page visit, i would love to hear some advice on this!
I hope i provided enough info, thanks in advance!
What you can do is use native cron. The native cron job is:
wget -q -O - https://example.org/wp-cron.php?doing_wp_cron >/dev/null 2>&1
Set it to run either every 5, 15, or 30 min depending on amount of server resources you have. I recommend a minimum of 30 min. Make sure to replace https://example.org with your domain and protocol (ssl or non ssl).
If you are using the command line (crontab) and/or the cron service doesn't allow you to change the time automatically. This is the full cron job:
*/5 * * * * wget -q -O - 'https://example.org/wp-cron.php?doing_wp_cron' >/dev/null 2>&1
Replace 5 by the interval you want the cron to run (every 30 min, every 15 min etc.). In this example, the cron is ran every 5 min.
Related
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
Is it possible, to run my PHP code without opening it?
For example, I have "test.php" which includes the following SQL code:
`INSERT INTO Humans(Name) VALUES ('Ric')`
I want to run this code in every 30 seconds, but the problem is that I can't open it every 30 seconds. :)
You can schedule a task if you're using windows, or a cron job if you're on linux.
For these cases, it is for the majority of web hosting solutions available CRON function (automatic call PHP scripts at a specified time).
The question is whether your web hosting provider allows calls PHP scripts in so short a time as 30 seconds as you say.
Under Linux, cron tasks cannot run every 30 seconds as the cron granularity is only 1 minute.
If you want you can have a workaround by create a script that executes every minute but runs the command twice
whatever.sh
#!/bin/sh
cd /the/directory/where/the/script/is
php task.php
sleep 30s
php task.php
Make sure it's executable
chmod +x whatever.sh
Then setup cron to execute the script every minute
* * * * * /path/to/whatever.sh
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
We're building an application that uses data from an api. We don't want to proces all this data because it's too much. We want to capture the data 4 times per day. Therefore we need a php script that saves this to the database without a button or human help.
We know how to get the data and understand how to upload this to a database. We don't know how to do this timed and if this is even possible.
We tried to do this with this code, but the page needs to be refreshed to echo.
if (date('His') == 114300) {
echo date('His');
}
A PHP script only runs when it's called via a request from a browser or another client, or when run through the command line.
You could, in theory, create a script with infinite execution time, and have that running. The script would need to check the server time and make the requests. The problem would be that it'd stop if the server restarted, etc. It's also highly inefficient.
Just call your script with a cronjob, four times a day, e.g. like so:
0 */6 * * * curl -k http://example.com/download.php >/dev/null 2>&1
The /6 means every six hours. The curl command calls the script and >/dev/null discards the output.
The best way to do this is by creating a cron job that runs 4 times a day
0 13,14,15,16 * * * /usr/bin/php path/to/cron.php &> /dev/null
your path/to/cron.php will be run at 13:00, 14:00, 15:00 and 16:00
http://www.thegeekstuff.com/2011/07/php-cron-job/
If you have a server (standalone / vps, with shell access), install a cron job.
If you have cpanel access, find the cron job setting.
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