Subscribe RSS

Archive for April, 2009

Rewriting the Query String Apr 21

Sometimes you want to redirect the user to a new page based on a specific query string value. Heres  how to do that:


RewriteCond %{QUERY_STRING} foo=bar&bim=bam
RewriteRule ^horrible.php$ /farbetter.html? [R=302]

This redirects all traffic going to “horrible.php?foo=bar&bim=bam” to goto “farbetter.html”. Note that for all other querystring vars horrible.php is still available.

Software Engineering Apr 07

The old ones are the best. Ive seen this cartoon many times over the years. Its so true. Anyone know who did the original?

software engineering explained

Category: General Musings  | Tags:  | Leave a Comment
Bash filename string manipulation Apr 07

A common task in bash is to retrieve the details from a string containing a filename. This handy summary helps you quickly get the data you need from the variable.

Consider the following variable

FILE = ref/paper/some.ps.gz

then the following vars are available:

${FILE#*/} returns the path with the first directory removed: paper/some.ps.gz
${FILE##*/} returns the file name with all directories removed: some.ps.gz
$(basename $FILE) returns the file name with all directories removed: some.ps.gz
${FILE#*.} returns all extensions: ps.gz
${FILE##*.} returns the last extension: gz
${FILE%/*} returns the directories: ref/paper
$(dirname $FILE) returns the directories: ref/paper
${FILE%%/*} returns the first directory: ref
${FILE%.*} returns the path with the last extension removed: ref/paper/some.ps
${FILE%%.*} returns the path with all extensions removed: ref/paper/some

Originally from http://itb.biologie.hu-berlin.de/~benda/software/bash.html
Also see http://www.faqs.org/docs/abs/HTML/string-manipulation.html

Category: Linux  | Tags: ,  | Leave a Comment
Nice, Renice and niceness Apr 01

surfer-girl-hawaiiOne of the major benifits of linux is the way memory and processes are organised in such a way that it can be considered one of the best multi threaded operatioing systems available. Regardless of how well something can be scheduled the OS still only have a finate amount of resources to work with. My little nugget of joy today is a short description and usage by example of the nice and renice commands.

I want to write this article as i find the man pages for these commands incredibly confusing.

Niceness (as it is commonly known) is the priority that a particular task has from the perspective of the process scheduler in the kernel (i use “the” kernel as it is a common feature of most *nix like os’es)

Niceness is best imagined as a queue that ranges from -20 to 19

nice_diagram

As you can see from the little diagram, the lower the number the higher the priority.

Most userspace applications will start with a niceness of 0 which is the most favoured niceness that a non-root user can have. Running ps wauxfl you will see the niceness of an application along with some other information such as username and a tree view of processes.

There are two ways to start a process with a different nice level. The first is at program initial execution using the nice command.

nice -4 <command>

This command will start a process with a slightly lowered niceness of 4 (less favoured) and can be run as a a normal user.

nice --4 <command>

This command has to be run as a super user. Notice the double dash (- -) this identifies that it is a switch and will have a niceness of -4 slightly raising the priority of an application above that of non root applications.

The other option for modifying the niceness of an application is renice. This tool is ported from BSD systems and as such the syntax is slightly different. Renice will let you cnange the niceness of an application that is already running.

renice +4 <pid>

This example of renice will set the niceness of <pid> to 4 lowering it’s niceness.

renice -4 <pid>

Running the command as a super user is necessary to set the niceness to less than 0 just like nice. The niceness is set to -4 in this example.

So why would you want to know about this? I personally like nice and renice as it allows you to run long and potentially resource hungry applications without stealing cpu cycles from those applications that need them. I like using nice to assist me run backups during the day or if i need something done quickly.