Category Archives: Linux

WordPress: Quick Steps to Setup a Child Theme

WordPress has this neat feature called “Child theme” for a while now. I’m a big fan of this feature. It is important to use this when you are using a theme that you like but want some changes to it. If you work on the theme files directly, chances are you will overwrite the changes when new updates are pushed. I’ve done it before and since the child theme came out I’ve not had any issue with it anymore.

So, here I would show how to setup a child theme quick and easy. Say you are using “twentytwelve” theme as your primary theme. Now you (like me) want to make a change to the header file to add your own code – for example, some Google code to show translation bar. Here are the steps:

1. under your-wp-root/wp-content/themes/ create the directory you want to place the child theme files in

2. now go into your-wp-root/wp-content/themes/your-child-theme/ and create a file named style.css

3. in style.css you will need to put the following piece of comment:

/*
* Theme Name: YOUR CHILD THEME NAME
* Description: Modified 2012 Theme
* Author: Your Name
* Template: twentytwelve
* */

Line 5 is very important here. It tells WP which parent theme directory to use.

4. After the comment you will need to import the parent theme’s stylesheet in. You can either copy paste the entire style.css from twentytwelve directory, or put the following import code:

@import url(../twentytwelve/style.css);

5. Now you can copy header.php from your-wp-root/wp-content/themes/twentytwelve to your-wp-root/wp-content/themes/your-child-theme/. Make necessary changes to the file and you are good to go.

6. Last step (which you could have done before changing header.php file) is to activate the child theme. In WP admin, under Appearance > Themes look for the theme you’ve just created. Click on “Activate” to use it.

Only the modified files (like header.php) will be used from your child theme’s directory. You can always do these on your local machine, upload the directory via FTP and activate the theme; the steps above are just the way I do it – i.e. login via SSH and do these from command line.

For further reading you can click here.

 

Rotate Logs for Apache on Linux using logrotate

Using Apache’s access_log and error_log files are useful. Not only a system administrator should use it, but also a developer should grow habit of using these log files for debugging purpose. But sometimes for high traffic web site/applications these files can grow HUGE and real fast.

In real life, you probably need log file up to a certain point. If anything goes wrong and you know about it immediately you will probably need the current log file plus few from past to compare. In case if you find out about an issue with the site/application a week later – then you should really think about how to fix that problem.

Anyhow, this article is to show you how you can tune pre-configured logrotate on linux system for apache (HTTPD) to meet your needs. Every path I’m going to refer here are based on CentOS 5.6 OS.

Usually the logrotate configuration file is located under /etc/logrotate.conf. But logroate also has individual configuration for different linux processed. One of them is Apache. By default the configuration looks like this:

/var/log/httpd/*log {
missingok
 notifempty
 sharedscripts
 postrotate
 /sbin/service httpd reload > /dev/null 2>/dev/null || true
 endscript
}

To understand what each of the items mean please refer to http://tinyurl.com/ansqol

For my purpose I just added two lines to have this:

/var/log/httpd/*log {
size=1024M
rotate 5
missingok
notifempty
sharedscripts
postrotate
/sbin/service httpd reload > /dev/null 2>/dev/null || true
endscript
}

size=1024M – The logs should be rotated when it is 1G in size.

rotate 5 – The logs are rotated 5 times before it is removed.

Thanks!

10 useful Linux command line tricks

When working on Linux on a day to day basis, there are times when you need to perform some tasks from command line. You can run combination of commands to get the desired result. Following are 10 of many useful Linux Command Line tricks I use almost everyday.

1. print column 2 of all tsv files in a directory and pipe it to a file:

for i in `ls`; do tail $i | tr "\t" "~" | cut -d"~" -f2 >> titles; done

2. Find unique string from a file:

sort -u filename

3. Calculate number of lines in a file:

wc -l

4. Send email from command line with attachment:

mutt -s 'my subject' -a /var/log/httpd/error_log -- youremail@domain.com < /dev/null

6. Read a file, print UNIQUE value in a specific column (column 5 in this case)

cat re | tr "\t" "~" | cut -d"~" -f5 | sort -u

7. Generate alphanumeric password

echo `< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c10`

8. Allow MySQL access in IPTable for a specific IP

-A INPUT -p tcp -s FROM_IP (IP1) --sport 1024:65535 -d TO_IP (IP2) --dport 3306 -m state --state NEW,ESTABLISHED -j ACCEPT

-A OUTPUT -p tcp -s FROM_IP (IP2) --sport 3306 -d TO_IP (IP1) --dport 1024:65535 -m state --state ESTABLISHED -j ACCEPT

9. While files are being copied/moved to a directory, run the following command to count number of files in that directory (in loop) and print it – acts like a timer.

printed=0; while [ `ls /some/directory/ | wc -l` -gt 0 ]; do output=`ls /some/directory/ | wc -l`; if [ $output -gt $printed ]; then clear; echo $output; elif [ $output == $printed ]; then echo $printed; printed=$output; fi; done

10. Print date in specific timezone

TZ=EST date

WordPress: Digging into Missed Schedule Problem – Part 2

OK … since my last post on the WP’s missed schedule issue I’ve gone many directions and thought I found the root cause of the issue. Although I’ve not yet found the exact problem but I do have a solid candidate for the problem.

Like I mentioned in the Part 1 of this series WP picks the second portion for the scheduled time on its own and initially I thought that is the issue. So, according to my finding what WP does is the take the second bit from the time the request was made to save the article and uses that. So I thought if two posts are scheduled for the same hour, may be the time SCHEDULE button was clicked was same causing the conflict of timestamp overlapping while creating the cron array (which is returned by _get_cron_array function).

But that does not tell me why a post that is scheduled for a different hour and the only one to be published at that time is missed once in a while. So, that could NOT be the real reason (but a possibility). So I started looking more into the publishing functionality. After going through a lot of code I wanted to run a quick test. My test relies on the fact that the environment that the WP is running on has multiple authors (sometimes sharing the same user accounts) and probably publishing articles are the same time.

So as part of the test what I did was I’ve two sessions (as author/admin) running on two windows and set two articles to be scheduled (at the same time or different times). Then I click on SCHEDULE button on two windows at the same time. After repeating the same test several times I found that one of the posts I was scheduling is not updated in the wp_options table for the option_name “cron”. Therefore, when the wp-cron runs it only publishes the one that was added and the other one shows up as missed schedule. If the post, that did not get into the cron list, is updated later (i.e. just click on SCHEDULE button again even without modifying anything in the post) it will be added in the list.

So, now I am trying to find any other way this could happen. For the time being I am thinking of an alternate way to make sure the cron list is always up to date.