Wednesday, September 7, 2011

Sticky session on Linux

Today i stumbled upon the slides from a talk given at Confoo 2011 by Sean Coates called "Fifty Tips, Tricks and Tools ...in one talk". Most of the stuff was not new to me, but still informative. But slide 56 gave a very usefull snippet "Sticky shell start", which allows you to login and be placed where the last shell operated.

You should place the following snippit in your ~/.bashrc or ~/.profile

cd () { builtin cd "$@" ; pwd > ~/.pwd ; }
cd "`cat ~/.pwd`"  

To me this is very usefull since i often jump between different servers and often need to go to the same place every time. so this saves me a lot of typing.

Tuesday, August 16, 2011

Quick tip: Save without permissions in VIM

Like many others I use VIM for my daily work. And like many others I often forget to open files with the right permissions (i.e. sudo vim ...).

To save a file that you need special permissions for, do the following:

:w !sudo tee %

this will allow you to save the file even though you do not have the right permissions (You need to have sudo rights of course).

More tips can be found here.

Friday, March 18, 2011

Edit existing SVN commit messages

I tend to forget things easily and for that reason I often go to my trusted old friend, Google to find stuff. But at times this can be cumbersome since Google results tend to be very cluttered, especially when trying to find tech stuff.

So I took up the advice of @lornajane and write down my finding i my blog, mainly because it is easier to find it again. And "Yes", I will forget it again and I will have to find the stuff again.

Sometimes the fingers work quicker than the brain and the eyes. I my case this often shows in log messages when committing new code. So here it is. If you want to change an exsisting commit message, use the command below.

svn propset -r N --revprop svn:log "new log message" URL

N is the revision number and URL are the repository URL to the fil you changed.

NOTE you need to be administrator of the repository, in order for this to work. I mainly use Google Code for hosting code and if you are the repo owner, you are the administrator.

The above example where found here.

Friday, January 14, 2011

Navigating BASH history

Today I came across this blog post by @lornajane describing how to navigate your BASH history using Ctrl-r. I have used this functionality in the past, as it is very useful when trying to find past commands or for doing repetitive tasks with small modifications that are to small to be automated. But when working in the terminal all day, I find this approach a bit to inconvenient, i.e. I have to think to much ;-)

So I have switch to another approach which works a lot better for me. I use the up and down keys for searching back and forwards in my BASH history. I have added the following to my .bash_profile:

# make bash autocomplete with up arrow
bind '"\e[A":history-search-backward'
bind '"\e[B":history-search-forward'

This enables me to quickly "auto complete" the commands I am typing. Although this approach has some drawbacks as well. If you have pressed the up or down key, you wont be able to further narrow your search. For me this is not a problem, since most of the time I only need to go back a few commands in the history, so a small narrowing of the suggestions are fine for my needs. But if you need to search further back in your history or not quite sure what you are actually looking for the Ctrl-r approach will probably be better for you.

Wednesday, October 6, 2010

file_get_contents acting very slow

Lately i have been working on a simple REST-like service interface to an existing product at work. I admit, I found some code online that I modified. No need to do work already done, right? ;-)

The problem arose when i tried to call my brand new web service. I got extremely long waiting time (about 15 secs to precise), before the resource was returned. The code I used look like this:

$result = file_get_contents('http://example.com/service/');

As simple as it gets. When I tried calling the service directly from my browser, the result came back instantly.

I turned to my old friend Google to look for an answer. Most of the answers i got was that the DNS lookup was slow. So i tried my script from all the different servers I have access to, but with the same result.

The solution was quite simple, when I finally found it. In my REST-like service I set the follow header:

header('HTTP/1.1 200 OK');

By doing this I force the connection to use HTTP 1.1. This is pretty standard, but apparently by doing this I use the default behaviour of HTTP 1.1 and that is to keep the connection alive. The same as setting:

header('Connection: keep-alive');

So the connection started by file_get_contents() is keept open, until it times out. Timeout is set to 15 secs on the server!! So the solution was to put the following header in my service:

header('Connection: close');

A w00p w00p the result was returned instantly.

A quick tip if you run into this problem, and the service is not your own, is to set the header yourself. You can do this by giving a context to the file_get_contents. This can be done by doing the following:

$opts = array(

'http'=>array(
header' => 'Connection: close'
)
);
$context = stream_context_create($opts);

$result = file_get_contents('http://example.com/service/', false, $context);

This will force the connection to close immediately after the resource have been retrived. Al option that can be set in a context (for HTTP) can be found here: http://php.net/manual/en/context.http.php