Using Linux Cron to drive wp-cron.php in WordPress

Here’s how I recently got Linux Cron to drive wp-cron.php file on one of my WordPress sites. This was an utter nightmare! But it now works (for me). Please be aware these are my notes. They are basically a reminder for me as to what I did. But if you are struggling and they help you then that’s great.

Be aware they may/may not give the optimum setup. All I know is that for some god-forsaken reason(s) this was unreasonably painful. I really do not know why this more-or-less trivial Linux/php setup caused me so much grief. But I don’t intend revisiting it to refine it or find out. Here’s what I did:

Background

WordPress uses (by default) a built-in pseudo cron system that relies on people visiting your site in order to operate. This has two distinct disadvantages.

  1. If you site gets few visitors then cron jobs are run rarely. This can be a problem in flagging up (for example) updates.
  2. If your site gets many visitors the cron jobs are run too often. You are continually looking for updates this can slow your site load time down.

If you control the server, or at least have access to cron then you can get round this by disabling the pseudo cron and using the system cron to fire off cron events to your WordPress site.

The Fix: Getting Linux Cron to drive wp_cron.php

This is for a site I have (not this one) which is on it’s own Apache2 server. So the changes in this case involve using the Linux crontab. If you are using shared hosting the setup is more-or-less the same, just use the cron facility in cpanel (assuming your host provides cpanels).

The first thing you need to do is disable the on-board pseudo cron. Do this by adding this line in file wp-config.php in your WordPress directory.

define('DISABLE_WP_CRON', true);

Now after you restart Apache2 with..

sudo service apache2 restart

wp-cron.php is now no longer being run using the inbuilt pseudo cron.

Now we need to set up a crontab for the owner of wp-cron.php and run it from there

Under Ubuntu the usual owner of the files that make up a wordpress install in /var/www has the name of www-data. This user is usually created when you install apache2.

www-data is a headless user with no shell or home directory. It’s done like that to make things a little more secure and difficult for anyone trying to hack your system.

You can change this default name by modifying fields APACHE_RUN_USER and APACHE_RUN_GROUP in file /etc/apache2/envvars if you want to. But I’ll continue using it here for clarity.

You create a crontab for www-data with

sudo -u www-data crontab -e

This drops you into an edit session using nano. There is a red warning stating that there are problems with the history file. Which is unsurprising as there isn’t one. www-data has no shell or home directory. So there is nowhere for user www-data to put one. Never mind. Ignore it

To get linux cron to drive wp-cron.php every (say) 15 minutes add this line in the nano editor

0/15 * * * * cd /var/www/<path to wordpress files>; usr/bin/php /var/www/<path to wordpress files>/wp-cron.php >>/<path to file writeable by www-data> 2>&1

You almost certainly do not need the path change (i.e. cd command) but as I said, it works for me. I also fully pathed php as I have assumed that www-data will not know where it is otherwise. But I don’t know that for certain. It works. I’ve had enough with tinkering with it. The other thing you need to ensure is that you have actually make <path to file writeable by www-data> actually writeable by www-data.

Save and exit nano. Nano seeks your permission to store the file in a temporary directory which it may do. But I also <believe> the system cron saves it away as well. Whatever. It works.

Notice I’m using php not wp-cli. That is because for some god-forsaken reason I could not get wp-cli to issue the command to run wp-cron.php from within the crontab. It worked perfectly outside of crontab. But not within it. I don’t know what I was doing wrong. Now I am past caring.

Also, you’ll no doubt notice that my php invocation of wp-cron.php is different to just about every other one I have seen relating to this issue on Google. Mostly other solutions I’ve seen which get Linux Cron to drive wp-cron.php add a parameter to the end of the wp-cron.php. Something like this:

/usr/bin/php wp-cron.php?doing-cron.

Why? (i.e. why the ?doing-cron bit ?)

Having the parameter did not work for me. So I ditched the parameter (i.e. i.e. the ?doing-cron bit.) . Then it worked.

I have no idea why that parameter was there in the first place. But I (initially) slavishly followed what I read. Only when it bit me did I remove it.

So (hopefully) wp-cron.php is now being run from crontab. But how do you know it’s working? More to the point how will you know in (say) two weeks time that it hasn’t fallen over for some reason?

Solution: You get it to send you an email once in a while just to show it is still running.

You need to add some simple code to your functions.php file in the root of your active theme. This also caused me much grief and suffering. Again, it is a trivial modification! And to a file I have added lots to in the past with little problem. Maybe I was just having a bad day.

This is what I added:

//-----------------------------------------------------------------
// Trigger cron from linux rather than use the 
// sub-optimal wordpress trigger
//-----------------------------------------------------------------
add_filter('cron_schedules', 'thisfunctionname');

function thisfunctionname ($schedules )
{
    /*$schedules['every_hours'] = array(
        'interval' => 3600, //in seconds
        'display' =>__('Every hours' ),
    );*/
    $schedules['2_minutes'] = array( 'interval'=> 120, 'display' => __('2_minutes' ),);
    /*$schedules['60_minutes'] = array( 'interval'=> 3600, 'display' => __('60_minutes' ),);
    $schedules['4_minutes'] = array( 'interval'=> 240, 'display' => __('4_minutes' ),);*/
    	
    return $schedules;
}

add_action( 'wp', 'custom_cron_job' );

function custom_cron_job()
{
    if ( !wp_next_scheduled( 'send_this_email' ))
    {
    	wp_schedule_event(time(), '2_minutes', 'send_this_email' );
    	/*wp_schedule_event(time(), '60_minutes', 'send_this_email' );*/
    	/*wp_schedule_event(time(), '4_minutes', 'send_this_email' );*/
        /*wp_schedule_event(time(), 'every_hours', 'send_this_email' );*/
    }
}

add_action( 'send_this_email', 'generate_this_email');

function generate_this_email()
{
    $email_subject = "Test 2min  subject";
    $email_content = "This is the 2m test";
    wp_mail( 'your-email-address', $email_subject, $email_content);
}

And that’s it. That’s what I did to get Linux cron to drive wp-cron.php. It will in turn generate an email to the specified email address at whatever time period you choose – (above settings it is just for testing – I’ve left some commented out alternatives). Obviously, do not forget to put your email address in the last line.

If you stop getting emails when you expect them – there is something wrong.

Now I’m going to have a beer (or three).

The rest of my Linux-ish posts are catalogued Here

Converting HEIC files to JPG or PNG on Ubuntu 18.04

HEIF stands for High Efficiency Image Format (file extension HEIC i.e. filename.HEIC). It is a new (ish) image file type.The HEIF image format builds images with a smaller file size and better image quality than the older jpeg standard. That’s why Apple has moved over to it as its default image standard on their new phones. Evidently Android will soon too. So how do you go about converting HEIC files into JPG? On Ubuntu 18.04 there’s a handy little package called heif-examples you just have to install it. As so:

sudo apt-get update
sudo apt-get install libheif-examples

Now to convert a file to (say) jpg on the command line its:

heif-convert filename.HEIC filename.jpg

That appears to convert the file to a jpg with 100% quality. With me I ended up with a jpg that was about 30-35% larger than the HEIC file. You can though reduce the size of the jpg by reducing the quality of the conversion. Like so:

heif-convert -q 75 filename.HEIC filename.jpg

The -q value can go from 100 (default) to whatever you can get away with. For me (and family snaps) 75 always seems a good compromise. Again with a short simple test my 75% quality jpg output file was smaller than the original HEIC file by about 20%.

Factfulness by Hans Rosling
Things ARE better than you think. Don’t believe the doomsters. Here is why they are wrong!
8500 5* reviews

I also tried the conversion to png files but I found that the resulting png file inflated the file size by 700%. I hasten to add though the image being used was not really suitable for png. But even so the image came out 5% bigger than just by using Gimp to convert the 100% jpg to png at level 9 compression. I also found I couldn’t change the compression on the png. Adding -q 75 or -q 9 (or whatever) did not affect the final png file size. This is probably my fault. If you know how to do this please enlighten me.

You can also go the other way and convert jpgs to HEIC files. But this needs a different tool from the same repository and in it’s simplest form you use it like this:

heif-enc filename.jpg

This will generate a HEIC file of the same name in the same directory

There are more options with this command than with heif-convert the full list is here. The main ones (imho) are:

  • -q Defines the quality of the output heif image. Like hief-convert this goes from 0 – 100
  • -L This makes the output HEIC file lossless and overrides -q (if it exists)
  • –no-alpha This (I think) removes the background making the image transparent.
  • -o Defines the output file name.

Interestingly, when I tested this command I found I got better compression when I specified the output filename.
i.e.:

heif-enc filename.jpg -o otherfilename.HEIC

Than when I just let it default. I don’t know why.

So there’s how to go about converting HEIC files into JPG files (and PNG’s too – with reservations)

Anyway here’s some serious respect to the guy(s) who wrote this converter.
You can find more about them here. The repository for this code with further information (especially on heif-enc) is on this Github Link.

My other Linux/Ubuntu Howto’s and Gotcha’s are on this link.

HP Compaq DC5800 Kernel Panic on Ubuntu: Fix

Some years back I inherited an old HP Compaq DC5800 Micro-tower. I happily ran Ubuntu 16.04 LTS on it for a long time. Then one one day I did the ritual updates (which included the kernel) and on rebooting, the DC5800 did a Kernel Panic and halted. I booted using the previous kernel and all was fine. The machine never managed to update the kernel again. If I tried, it suffered a Kernel Panic on reboot. I have (yesterday) found the solution to the problem. Below is a guide with pictures.

Continue reading

Need a PDF document word count? Here’s a simple approximation.

PDF documents are great but there may come a time when you are reading/reviewing one when you need to roughly know what the word count of the document is. By their nature PDF’s are usually full of charts, images and tables that make the task difficult. Most (all?) free PDF viewers don’t give the reader a word count in situ. So you have to use either expensive software or another method.

Continue reading

Bypassing Amazon but still published on Kindle? Simple! Here’s How.

Lets say you run a email newsletter. Maybe you would like to provide your readers with something a little less email-like and get your newsletter content/links/downloads published on Kindle. For arguments sake let’s say you want to send your readership a free e-book to read on their Kindle once in a while. It could be fiction or fact. The subject matter is immaterial.

Continue reading

Publishing using Amazon KDP. When Less is More.

Many indies use Amazon KDP. It is a great way to publish a book of any size.  We often associate more pages with more value and bigger royalties but that is not always the case. There are circumstances when publishing paperbacks on Amazon KDP where you can increase royalties by decreasing page count. A case when less is more!

Continue reading

A Story-line for Writers

Before I come to this “Story-line” for writers, I want to purposefully digress slightly and talk about an excellent TV series on Netflix called “Black Mirror”. It is relevant (honest!).

Black Mirror is a series without a unifying story. It is in effect a set of stand alone stories. Each story is different and could be put out as-is without any of the others. Each story stands alone on its own merits.

Continue reading

Libraries and Public Lending Rights

If you place your book (or your book is placed) into one or more public libraries in the UK then you can register for an annual payment under the Public Lending Rights scheme (known as the PLR – see here).

Public Lending Rights is a long standing UK government scheme. It recompenses authors for loans of their work from public libraries. While the income from Public Lending Rights will not set the world on fire, it is a rather satisfying confirmation that your work is getting noticed and read. I believe most other countries run very similar schemes.

Continue reading

The Tyranny of Amazon Ratings

Authors all crave Amazon ratings. A string of good ratings really can establish a book. This is especially true for ebooks. An ebook with an initial bad rating will die a sudden death. If the first rating is bad then it can kill free downloads. Let alone sales.

Most readers do not give a rating. Even those who really enjoy or significantly dislike the work usually just let it pass. Most of those who do give a rating do so because the book has had an impact upon them. They try to fairly judge the work on what they got for their money. Which is fair enough and what we all want.

Continue reading

Buying an ISBN in the UK

There are many head winds facing a self publisher. One of these is the cost of buying an ISBN in the UK. The price structure for buying ISBN’s strongly favours big companies while severely penalizing small publishers.

If you are publishing a single book and so just want one ISBN, you will get hit the hardest of all.

This is the price structure for buying ISBN’s in the UK as of today.

Continue reading

Niche Non-Fiction: Ebook or Paperback?

Nobody is going to get rich writing niche non-fiction books on local history or other similar narrow niche topics. But if you can get a some sales it will give you a great feeling. You will have provided a useful resource to the community.

If you are lucky, writing a niche non-fiction book might allow you to buy a few beers. But you most certainly should not give up the day job!  So if you want to maximise the number of beers what should you publish? An ebook? A paperback? Or both?

Continue reading

Zombie Novellas

NOTE: This is about novellas, short stories and other ebooks that do not sell.  It is not about the Zombie novellas as a genre!

OK, lets say your novella or short story sold a few copies then went into rapid decline like This One did. Now, two years on, instead of giving you that small but consistent income you were hoping for, it has dropped to a sales rank so high it resembles the number of dollars you were dreaming of making when you started writing.

What should  you do?

Continue reading

Sales Analysis: A Poor Selling Short Story

In the last post I displayed the statistics for the first of three short stories I wrote a  few years ago. The sales performance of this first ebook was hardly spectacular. But it took less than a day to write, format and then submit. It was also just an experiment. So taking all into account, it wasn’t bad. I made a couple of hundred quid. In this post I want take a real no-hoper and do some sales analysis.

Continue reading

Lifespan of a Short Story eBook.

In my last post (Here) I looked at how the ebook Novella and short story market appears to be overcrowded. I went on to surmise though that maybe it is not as bad as it first looks. Could it be that as most novellas and short stories seem to have a very brief shelf life? Which would mean the author of a new novella would be competing mostly with corpses.

If that is the case then there is some good news.

The good news is that the market is not swamped. Or at least, less swamped than we thought. Sadly though there is some bad news too.

Continue reading

Novella Sales are NOT Dead!

(It is just old novellas smell funny)

So how do novella sales compare with other ebook formats? Look at this graph I picked up from Data Guy on AuthorsEarnings.com. Like all of the Data Guys information, it is immaculate. Everything on his site is worth a read. (Frankly I’m in awe). There is no reason to doubt the graph below at all. But, I do not think it tells the whole story.

Continue reading

eBook Sales USA versus UK

Just to wrap up the last set of a short series of blog posts on how many ebook sales you can expect from a specified sales rank, I thought I would just compare the pluses and minuses of  sales rank performance in the USA and UK. The series started On This Link

We know that by far the biggest market for ebooks is the USA. Below is the relative quantity of ebook sales by Amazon in 2017 by English speaking region. Bear in mind Germany is a bigger market than the UK.  Japan is nearly as big. There will be more on these two markets in a later post.

Continue reading

Ebook Sales Rank (UK) The Long Tail

In the last post I looked at the relationship between ebook sales rank and actual sales for popular ebooks in the UK.  This post is about those ebooks that sell less well and how the sales rank relates to actual sales for them.

Over 400 million ebooks were shifted by Amazon USA in in 2016/17 whereas the UK managed just short of 85 million. So the ratio of ebook sales between the two behemoths is of the order of 5:1. Unsurprisingly the sales rank for the same ebook differs greatly between the two.

So, are the USA sales ranks totally independent of the UK store? I think so. Inevitably there will be books that sell well in the USA and badly in the UK and vice sa versa. So to have the same ranking for both would be self defeating.

Continue reading

Ebook Sales Rank for UK

The last couple of posts, starting with (This one) I have explored how ebook sales rank relates to number of ebooks sold. But that was for the USA. From the data that is out there I extrapolated to end up with a set of graphs showing how Amazon sales rank related to actual sales. Obviously this comes down to my interpretation but I think it stands up. Here I will try an explore the same issue with UK sales rank.

Continue reading

EBooks: The Long Tail Sales Rank in USA

In the last post (Here) I tried to graphically present how many ebook sales per month you could roughly expect for a particular sales ranking. This was fine for those shifting 1000’s a month. For those with a sales rank under 10,000 it was useless. The resolution on the graph was simply not up to displaying the highs as well as the lows. The long tail ebook sales rank in the USA just blurred into the x-axis.

Continue reading

eBook Sales Rank in the USA

This is the first is a series of posts on how ebook sales rank in the USA and the UK relates to the number and rate of ebook sales. While there are a number of  tools available that will give an approximate sales outcome for a particular rank, they are a little like fruit machines. You put in your sales rank. Pull the handle. Out pops an estimate of the number of books you need to sell to achieve that sales ranking.

I thought it would be nice to present this data graphically so we can all see what is going on right across the full sales rank spectrum. I intend going down to where a ebook is selling only a handful of copies a year. This is below the resolution of the fruit machines.

Continue reading