cpulimit

January 11th, 2010

cpulimit is a program that can be used to limit the peak cpu consumption of processes. It’s a piece of functionality that I would like to be standard. Sadly, cpulimit is a bit shaky – it works best if you start the process and then have cpulimit attach to it.

Following a Stream

January 9th, 2010

Following a stream being written to a file is surprisingly easy.

tail -f <file>

I didn’t know about the -f flag before. It definitely seems useful.

I always have to look up the procedure for debugging code in compiled mex extensions in Matlab, so here it is:

shell> matlab -Dgdb
gdb> run -nodesktop -nojvm
matlab> dbmex on
matlab> foo()   % Execute the mex function being debugged
gdb> break foo.cpp:17   # Set up breakpoints
gdb> continue

Reordering Screen Windows

January 5th, 2010

Screen windows can be reordered by giving windows new numbers. This is accomplished by Ctrl-A, : to open the command prompt and then giving the command :number <n>.

Dealing with nested screens

January 4th, 2010

The proper way to deal with nested screens (e.g. resulting from sshing from one remote computer to another) is to escape commands using Ctrl-A, as shown in this cheat sheat. To e.g. start a new window in the nested screen, use Ctrl-A, A, C .

Screen and X-Forwarding

December 17th, 2009

After a bit of tinkering with screen I found two major annoyances. First: X-forwarding does not work despite the -X flag being set when opening the ssh connection. Secondly: Shift-PgUp and Shift-PgDn do not work for scrolling (and scrolling is in general a pain).

The first problem seems to have a rather simple solution. Just set the DISPLAY environment variable in .bash_profile once a screen session has been detected.

if [ -n $WINDOW ] ; then 
  export DISPLAY=localhost:10
fi

I have now run into a system for which neither of the previous two methods mentioned seem to work. A third method is to use GNU screen. Roughly it is done by:

  1. executing “screen”
  2. running the command
  3. detaching the screen using Ctrl-A, Ctrl-D

However, there is so much more to screen than that. I have had my eye on it for a while, but never made the leap to using it consistently. A couple of handy guides to screen should give some idea of what it can do. I’m going to try out automatically running screen when logging in in an attempt to start using it all the time.

SSH Public Key Authentication

October 24th, 2009

A minor heads up when configuring ssh for public key authentication is that ssh on the local machine only checks ~/.ssh/id_rsa and ~/.ssh/id_dsa for private keys to use public key authentication. Hence, if you have multiple sets of key pairs then you might have to explicitly point to the correct private key using the -i flag of the ssh command.

Exporting from dia to LaTeX

July 10th, 2009

Exporting diagrams from Dia into LaTeX (with LaTeX fonts etc) is quite easy once you figure out how to do it. There are several TeX exporting options in Dia, but this is the only one that I could get to produce satisfying results.

Using Dia 0.97, or later, export the diagram as MetaPost. Then run mptopdf on the exported metapost to get a shiny pdf to include in the LaTeX document.

I previously made a note about how one can close standard input to detach a shell command from the terminal. I recently discovered the nohup command, which has a similar purpose.

The nohup command seems to emulate an attached process better. I ran across some obscure behaviour where ffmpeg would fail to decode a video file if standard input was closed, but not if it was left attached to a terminal or if nohup was used.

This is always a nice snippet to have around:

for i in `find . -name '*.ext'`; do perl -pi -e 's/foo/bar/' "$i"; done

It searches the current directory and all children recursively and replaces “foo” with “bar” in all files with names matching ”*.ext”.

The fold command is handy for formatting text into a fixed-width column (of e.g. 80 characters). I never seem to remember its name though.

AWT produces blank windows when used in conjunction with Beryl. The fix is to set the AWT_TOOLKIT environment variable.

AWT_TOOLKIT=MToolkit

Daemonize shell commands

April 18th, 2008

The following is how to daemonize a shell command and have it write standard output and error to an output-file.

shell-command <&- > output-file 2>&1 &

Replace “shell-command” with the actual command and “output-file” with the name of the file to write to.

Dumping mms streams

March 10th, 2008

It’s becoming common for sites to use streaming media without providing downloads. Luckily mplayer, a handy command-line movie player, comes to the rescue – allowing one to dump the stream to a file with the following command.

mplayer mms://foo.tld/bar.wmv -dumpstream -dumpfile bar.wmv