wordpress cron slowing site issue fixed

Many webmasters are facing wordpress cron slowing site issue. WordPress made in PHP, it has so provision for run the background job on certain time. WordPress made provision in CMS for task scheduler. For doing this for every request wp-cron.php is executing.

wp-cron.php is a virtual cron job executing file. This file may be checking for WordPress updates, sending email notifications, etc. WordPress will continue execute the wp-cron.php on each and every page load or request.

Due to execution on every request. Due to this, extra resource usage on your server. This cron job can take a minute or so to run, depending, on the size of the website. If your website doesn’t have a lot of visitors, it may not be an issue.

I have blogs which has huge traffic. Site slowness will impact your search engine ranking and site visitors.

What can be done to maximize the efficiency of the WordPress cron jobs? You can tell the WordPress installation that you’re going to handle the execution of the wp-cron.php file. We need to just use the following steps:

Open the wp-config.php file for editing and add the following line:

define('DISABLE_WP_CRON',
true);

I am using Linux VPS which has centos 6 version. So I started cron service using following command.

/etc/init.d/crond start

After that use following command to installation of cron

crontab -e

Cron has a specific syntax which is very easy to understand:

  • Minute
  • Hour
  • Day of month
  • Month
  • Day of week
  • Command to execute

Just copy and paste following code in your cron file. Following code will executing after every 1 hour instead of executing on every request of wordpress. Wp-corn.php will be executed after every 1 hour.

5 * * * * /usr/bin/wget https://purabtech.in/wp-cron.php?doing_wp_cron > /dev/null 2>&1

There are more samples which we can use in your cron file. Some samples as follows:

# hit this url to run the wordpress cron process every hour of every day

# this command will run at 12:05, 1:05, etc.

#curl https://purabtech.in/wp-cron.php > /dev/null 2>&1

#wget https://purabtech.in/wp-cron.php?doing_wp_cron > /dev/null 2>&1

above simple trick will solve the wordpress cron issue. I applied above changes to my site and this wordpress cron slowing site issue fixed.

Shared Hosting

If you are using shared hosting than using FTP program, you can change and replace wp-config.php file first. Use following steps:

1) Login to our control panel, manage your service and in the service controls area, click the Cron Jobs link.
2) Click the add new cron job button at the top right.
3) In the “Add New Cron Job” area, click the “Common Settings” drop-down menu select the option for “Once an hour”.
4) In the “Hour” drop-down menu, select “Every 6 hours”.
5) In the “URL” field, type in the following:

https://purabtech.in/wp-cron.php?doing_wp_cron

Note: do not forget to change domain instead of purabtech.in.

How to use the cron job with WordPress

In linux OS and windows OS cron jobs been used for backgroud tasks, many times you need to execute the some background tasks based on time. In wordpress creating the cron jobs is very easy. For creating the cronjob you can use the wp_schedule_event() event. You can run the cron based on time defined.

In linux and mac box adding cronjobs are different then using in wordpress. WordPress itself supports the adding cronjobs events or tasks. Here in this article I explained about using cron job with WordPress.

CRON job with WordPress

You can pass following parameters:
hourly
twicedaily
daily

<?php
add_action('wpapi_hourly_event', 'do_this_daily');

function wpapi_activation() {
    if ( !wp_next_scheduled( 'wpapi_hourly_event' ) ) {
        wp_schedule_event(time(), 'daily', 'wpapi_daily_event');
    }
}
add_action('wp', 'wpapi_activation');

function do_this_hourly() {
    // do something daily
}
?>
cron job with WordPress
cron job with WordPress

For more detailed information you can visit
http://codex.wordpress.org/Function_Reference/wp_schedule_event

How to set cron jobs in Linux

If you are programmer then you many times heard about cron jobs. Who are new in to programming or new about using cron jobs they always think, how schedule job in linux box.

How to set cron jobs in Linux

How to set cron jobs in Linux
How to set cron jobs in Linux

For many purpose we can use the cron jobs. Like sending emails, taking Mysql or database backup, taking file system backup or any other purpose.

What is Cron?

Cron is basically Linux program which will run on only windows.

Cron is a program that enables you to execute a command, or a script with a sequence of commands, at a specified date, time or at set intervals. The commands or scripts that you want cron to run are defined in a file called crontab, and every user has their own independent crontab file. Cron is a system program that is running all the time and is similar to the Windows scheduler which allows you to run commands/programs at predefined times and intervals.

We can pass following parameters to Cron file.

Minute = Minute of the hour, 00 to 59. * Will indicate every minute (details later)
Hour = Hour of the day in 24-hour format, 00 to 23. * Will indicate every hour (details later)
Day = Day of the month, 1 to 31. * Will indicate every day (details later)
Month = Month of the year, 1 to 12. * Will indicate every month (details later)
Day = Day of the week, 3 chars – sun, mon, tue, or numeric (0=sun, 1=mon etc)…. * Will indicate every day (details later)
Task = The command you want to execute

Now I will show you how to use the cron in Linux.

#su

#ROOT PASSWORD

#crontab -e

This will open the corn file from linux

Put you options in this file. For example

* * * * php  /var/www/html/project_name/script.php

Above script will fun after each minute.

00 18 * * php  /var/www/html/project_name/script.php

Above script will run at 6PM of day

More commands about crontab

#crontab -l

This will list cron file and all tasks which are present in cron file.

#crontab -r

This command will delete all cron jobs from cron file.

Using following command will show the all error log about cron file.

#tail -f /log/cron/error_log