Posts Tagged ‘Linux’

screen(1) tips

Wednesday, August 5th, 2009

Screen is a window manager for console based environment. Its killer feature is the ability to detach the current session and resume it (reattach) it at will at arbitrary time. Another useful feature is that it multiplexes the multiple windows in a single one, this used to be way more important before the GUI terminals such as gnome-terminal, mrxvt, konsole.. become popular. Now you can simply do that by opening a new tab. Another thing which it can be used to to have two (or more) people attach the same screen so one can guide the others (-x does this).

alias sl='screen -ls'
alias sr='screen -D -R'

This are two rather handy aliases, especially the second one, it allows you to do “sr im” and create the screen session named “im” if it doesn’t exist, but only detaching it and attaching it if it does exist. This is better than screen -x because if you reattach from a machine with different resolution screen -x will leave the old settings, while -D -R will properly handle the resizing.

Ever left some longer running script/process/something at home and than wished you could control it via ssh? Fear not, retty is here to rescue! To demonstrate this:

$ irssi
# switch to new tab
$ screen -S irssi
$ retty `pgrep irssi`
/redraw

To explain what is going on here, first we start a normal application in one tab, this could be any other thing, like a script doing heavyweight processing. In the new tab we start the screen session, this is an optional step, it allows us to be able to later on detach/reattach the session at will. Inside the screen session we hijack the irssi PID. The last step, /redraw is only necessary because we are dealing with complex ncurses stuff which does weird stuff when it’s in operation :) . For the same reason you have to run “reset” in the first tab if you want to keep using it normally. This by itself isn’t really useful, however you can use the exact same procedure to reattach something via ssh.

Screenshot

This is probably the feature i use most in screen, apart of the attaching/detaching sessions obviously. The ability to name particular window, it makes it so much easier to pick the window you want/need. Also after reattaching a session after a day of inactivity you don’t have to cycle through windows to see what is there. To name a window use “^a A”.

oscon 2009

Friday, July 24th, 2009
open_source_communism
Image by jagelado via Flickr

Let’s first begin with the pricing, the prices for hobbyist are umm, insane, almost 2000$. Insane for someone like me who ain’t sponsored by a company or anything. :| But they were kind enough to provide access to Expo hall and BoF sessions for free, kudos for that. Expo hall is fairly big, from top of my head that would be 40 booths, presenting just about everything. Microsoft is for example presenting Windows 7 and has “bad vista” stickers, which kinda surprised me given the enterprise nature of Microsoft. They have possibly the most cool exhibit, the guitar hero on xbox (with guitars!). Not only that, but they have the only stickers (in the expo hall) i find worth putting on my laptop!

From the other folks linux found has a booth, they are celebrating the 10th anniversary and giving away free t-shirts. Their mission is to help open source with non technology, like how to become a non profit entity and how to raise founding. EFF also has a booth there, i bought their t-shirt, it’s pretty cool, has “defend bloggers’ rights” on it :) . I couldn’t help it but get a feel that the entire thing is too enterprisey, for example the open solaris guy that did talk at intel booth, his way of talking was screaming “marketing“. Also gear6 booth attracted my attention, but there was only marketing girl there who couldn’t go beyond “we offer active active replication” (she had no idea what that means) :) . Lopsa had a booth there as well, i never imagined that such organization exist.

Lot’s of cool persons, i met with @kreneskyp, we were supposed to have a hacking session. Judging by results one could say it was more of a drawing session than anything else :) . However the truth is far from that, we got to discuss how to implement the redundant main worker. In theory we solved all problems (but one!), now it has to be implemented. Also i’ve got to know muhc better pydra’s internals, overall it was very productive hack session. Note to self, always bring whiteboard markers!

bash tips’n'tricks

Saturday, June 27th, 2009
Bash
Image via Wikipedia

Lets begin with a link, this is really REALLY REALLY good resource for learning bash (especially the BashFAQ).

Imagine this, you wrote a script for some thing, now that is ready for production you put it in the crontab and let it run from there, BUT in certain conditions that script fails. Assuming the error ain’t trivial the way i tackle the problem is to add something ala this to the top of the script.

exec 2> log.`date +%Y.%m.%d.%H:%M`
set -x

The first statement redirects stderr to a certain file, the second statement enables “xtrace” printing commands as they are executed. The undesired side effect of this is that ALL stderr is redirected to that particular file. The alternative to this would be to do proper logging, but if your script is long enaugh that you need proper logging there are pretty good chances that you shouldn’t be doing it in the bash in the first place ;) .

Ever found yourself wanting to do some simple processing of some command and wanting to use the result?

echo some string | while read; do foo='boo'; done; echo $foo

This echoes nothing as $foo is empty. What happens in the background is that pipes spawn the subshell, so the variable $foo is assigned in the subshell, and when the loop terminates subshell is closed and normal shell is execution is continued, which doesn’t know anything about $foo.

A solution for this is called “process substitution”, to the best of my knowledge it is exactly the same as pipe, only without the subshell part. As a side not, ksh doesn’t spawn subshells with pipes.

while read; do foo='boo'; done < <(echo some string); echo $foo

Let’s talk about dotfiles, specifically about those initializing the shell, in bash that would usually be .bash_profile and .bashrc, what they can be used for is to customize you bash on every startup. The difference is that .bash_profile is executed when a login shell is started (for exmple when you ssh to a box or do “su -”), while the .bashrc is executed when nonlogin shell starts (usually when you start mrxvt/xterm/konsole/…). Why the f**** do we have distinction between the two modes? No good reason really, it’s because we were doing it this way for 20+ years, why break the tradition? :/

[[ $PS1 ]] && exec bash -l

Having the above in .bashrc is the way around this annoyance, it allows you to have all your configuration in .bash_profile. The only thing it does is execute a login shell (thus triggering .bash_profile execution). The condition makes sure that the login shell is executed only when you are in interactive mode (bash by definition/man page set’s PS1 only for interactive shells). A non-interactive shell is spawned when you use scp.

Reblog this post [with Zemanta]
Blog Widget by LinkWithin