Praj's Blog

Apache Virtualhost Entries Work After a Graceful Restart

Turns out that you can apply virtual host changes in Apache config (httpd.conf) with a graceful apache restart, rather than a full restart which forcibly kicks (which means kicking users off your web server).

Here’s how to do a graceful change:

  • Change the httpd.conf file accordingly
  • Check your config with sudo service httpd configtest (make sure you get syntax OK)
  • Issue sudo service httpd graceful (most Linux variants)

Note quite a few other Apache changes take effect with a graceful restart. Well worth trying this first, before resorting to a full restart.

PHP Syntax Check

Ever had to make a change to a PHP file on the fly only to discover you made a syntax error? There’s an easy way to make sure you don’t have any syntax errors in your changes: run php -l on the file through the command line:

$ php -l yourfile.php

Any syntax errors are reported on the screen.

If all is well, you’ll get this message:
No syntax errors detected in yourfile.php

Grep and replace a text string in matching files

After a bit of searching and trying a few different methods, I’ve settled on the following commands to find and replace a text string in all files that match the search string identified through grep.

Here’s the full command (adjust the values of ‘searchtext’, ‘matchtext’ and ‘replacetext’ accordingly).

$ grep -r -l 'searchtext' . | sort | uniq | xargs perl -e "s/matchtext/replacetext/" -pi

Here’s a break down of how it works from left to right:

$ grep -r -l 'searchtext' .

Finds and lists all files in the current directory (and all subdirectories recursively) that match the value of ‘searchtext’.

$ | sort | uniq

This pipes the grep results to sort which sorts the results. The results are then piped to uniq to filter out any duplicates

$ | xargs perl -e "s/matchtext/replacetext/" -pi

Unique grep results from previous commands are piped to xargs so that the perl command can be executed against them.

The perl -e flag executes a regex search for the string ‘matchtext’ and replaces it with ‘replacetext’ using “s/matchtext/replacetext/” on each file it receives from the grep results. The -pi flag edits the file in place – i.e., saves the change of replacing ‘matchtext’ with ‘replacetext’ in each file. Note you can use variations on -pi to save to a new file if that is what you are after.

Short Posts

I’m intentionally trying to keep my posts short. The younger generations get a lot of flack for this, apparently our short attention span means we don’t read much anymore and ignore things because we can’t focus. I disagree, if anything, I blame mass-media and advertising for diminishing our attention spans. But enough on that.

The argument is silly. Think newspapers (back in the day people used to get their news on printed sheets of paper), and very few people would actually read an entire newspaper. The general approach was to skim for interesting headlines, read a paragraph or two, and then decide if it was worthwhile continuing. Now think about how that applies to the whole Internet. There’s a lot of stuff out there, so most people, understandably┬ádon’t want to read a rambling essay.

Another thing about the Internet (and it’s true advantage) is that you can read a few paragraphs about something, and even if that’s the end of the article, you can research and find out more on the relevant topic. You simply don’t need to write all that will ever be written on a subject anymore.

I know you’re young and I know you’re not afraid. But when it’s time to go is that what you’ll say (I’m not afraid)?

Sprung Monkey (What’s that you say?)

On Multitasking

There are lots of ideas out there on productivity and multitasking. Many take a black-or-white view; it is either a good thing or a bad thing.

My take is that you have to understand what multitasking means. Just like a computer, multitasking gives the illusion of doing more than one thing at a time by quickly context-switching between tasks. The catch, is that in a human mind, context-switching causes significant lag. This lag is referred to by terms such as “losing focus”, “getting distracted”, “wandering”, or even “procrastinating”. However, a lot of multi-taskers may not even know that they are suffering from this problem.

Context-switching costs productivity in cases where you need significant focus. Basically things that involve high-level brain activity like creating or problem solving. You know, the stuff that matters. Multitasking in this area is a terrible idea. It leads to a common syndrome – instead of doing one thing well, you attempt lots of things, and get nothing done. Then, when you stop and reflect, you are left wondering where your day went. This is where the single-task, single-focus movement can have a powerful change in your life. Remember that society is making us all into multitaskers by dividing and conquering our attention (the quintessential example being advertising).

There is however another stream of activities where multitasking can be a really useful thing – and this is when you are doing mundane activities. Think stuff like chores, watching TV or waiting in line. Here, you can become more efficient by “grouting” these activities with something useful like reading or listening to a book, doing some exercise, or responding to straightforward emails. The key being to recognise and take advantage of these opportunities.

I subscribe to the belief that the most productive people don’t necessarily work harder than the average person, they are simply smarter about what they do and when.

Plural conventions

Memorability is a key component of software development. So please, when you are developing software, pick a plural convention and stick with it. If you are going to call your database table “users” (since it holds many user records), then refer to your model with the same plural. If your user object is an instance of one user, then please refer to it singularly as “user”. The same rules need to apply throughout your design and implementation.

The key is that it should be easy to remember the convention once you know the rules (and the less rules, the easier it is to remember them!). I don’t mind what rules you use. The problem, and reason behind so many developer flame wars, is inconsistency. We think in patterns and systems, and nothing irks us more than when those patterns and systems break for no good reason.

Multitail

If you spend a lot of time watching and working with log files on Linux systems, then you’ll love Multitail.

It’s a simple install with yum –

$ sudo yum install multitail

I’ve only just discovered it, and here’s some of the things I really like about it:

  • Forwards/backwards scrolling, great for terminal windows (f/b keys)
  • Search (/ key)
  • Syntax highlighting, including apache, mysql, log4j and other log formats (c, then S for scheme)
  • Press F1 to get more help

Mac Finder navigation shortcuts

Two really handy keyboard shortcuts for navigating around a file system on a Mac.

  • In finder you can press cmd+shift+G to go to any location directly.
  • In an file dialog windows, press the / key to specify the save location or starting location.

This makes it so much easier to set where to go or save things than trying to use the standard finder GUI.