Nov 102009

So you have a process that’s spewing out gigabytes of essentially useless output, what do you do with the output? That’s easy redirect it to

/dev/null

. But what if you want to inspect that output periodically to ensure that your buggy process is still going? Well, put on your *nix tool-belt and lay some pipe.

An understanding of named pipes will make this process easier to understand, so here goes. Named pipes are exactly what they sound like, instead of using the | (pipe) character to send output from one process to another you create a reusable named pipe or fifo with the mkfifo command:

$ mkfifo output
$ ls -l output
prw-r--r-- 1 user user 0 2009-11-10 17:10 output

The p in the first field indicates that this is a named pipe. So now you can test out your named pipe with something like the following:

$ echo "testing" > output &
[1] 16834
$ cat < output
testing
[1]+  Done                    echo "testing" > output

As you can see the pipe blocked the echo command from completing until the other end of the pipe was attached.

So that is a little tutorial on named pipes. More detailed information can be found here.

Back to our original problem, multiple outlets on the named pipe. I’ve created the following test case to demonstrate the problem. You have a program that is generating copious amounts of information, say debug level 100. To demonstrate that my program is

cat /dev/urandom | hexdump -C

. This generates an endless stream of garbage parsed through hexdump to make it terminal friendly. I’ve created the output fifo (named pipe) and I’m going to redirect the output there:

$ cat /dev/urandom | hexdump -C > output &
[1] 16856

Now to plumb the pipe to /dev/null

$ cat < output > /dev/null &
[2] 16859

So now our urandom to hexdump process is running full speed as you can see from top:

$ top -b | head -n 15
top – 17:21:51 up 7 days, 14:17, 1 user, load average: 1.06, 0.38, 0.14
Tasks: 101 total, 2 running, 97 sleeping, 1 stopped, 1 zombie
Cpu(s): 0.0%us, 0.0%sy, 0.0%ni, 96.4%id, 0.0%wa, 0.0%hi, 0.0%si, 3.6%st
Mem: 1048796k total, 776472k used, 272324k free, 337340k buffers
Swap: 2097144k total, 0k used, 2097144k free, 289420k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
16861 user 20 0 5152 728 604 R 79 0.1 1:21.14 hexdump
16860 user 20 0 5164 688 556 S 20 0.1 0:23.24 cat
40 root 15 -5 0 0 0 S 4 0.0 0:25.54 events/3
1 root 20 0 4100 892 608 S 0 0.1 0:00.66 init
2 root 15 -5 0 0 0 S 0 0.0 0:00.00 kthreadd
3 root RT -5 0 0 0 S 0 0.0 0:00.10 migration/0
4 root 15 -5 0 0 0 S 0 0.0 0:00.02 ksoftirqd/0
5 root RT -5 0 0 0 S 0 0.0 0:01.40 watchdog/0

Now I want to inspect the output pipe without interrupting the primary process. This solution eluded me because running a cat will probably freeze your terminal for at least a few seconds and just give you way too much, it's like an uncapped fire hydrant. The fine folks at ##linux on irc.freenode.net came up with dd which is exactly what we need here. With the dd command you can specify exactly how many bytes you want off of the pipe.

$ dd if=output count=1
a1 f8 27 66 43  76 90 e8 8f fa 7b 58 9c  |.|7..'fCv....{X.|
160d1040  34 8b 1f 70 ca 97 a1 3d  2c 68 e9 8f dc cc da d4  |4..p...=,h......|
160d1050  e7 f3 28 33 4a a1 d2 5b  d8 cc 38 58 c8 97 90 1c  |..(3J..[..8X....|
160d1060  01 46 0b 0e ac b8 ea e1  3c d6 b7 8c 0d b9 bb 0b  |.F......<.......|
160d1070  70 25 02 94 a7 32 3d a4  c9 73 6a 16 3c 33 1a 24  |p%...2=..sj.<3.$|
160d1080  41 32 d8 7b 60 01 33 50  b3 4c 7c 26 2f 1c dd 0a  |A2.{`.3P.L|&/...|
160d1090  23 a2 2d f3 05 df 30 9b  20 b1 7a 98 f9 9b 67 51+0 records in
1+0 records out
512 bytes (512 B) copied, 0.0120007 s, 42.7 kB/s

Pretty cool, huh? Now we can get fancy with the watch command and create a nice status console to watch our job in a nice controlled fashion:

$ watch -n 0.5 -d 'dd if=output count=1 status=noxfer 2>&1 | grep -v records'

Well, I thought this was a pretty cool and elegant solution.

Nov 032009

OpenSolaris kernel with GNU Utilities? Werd…http://nexenta.org Establishing ipv4 forwarding and NATing right now.

Oct 292009

Sweet, my 19J6 tubes came in the mail today. GE, look like they’re circa 1955 :) I’m too lazy to take a picture, maybe tomorrow.

Oct 272009

The parts are slowly arriving for my starving student headphone amp. All of the electrical components are here, just waiting for the tubes, sockets and my sweet enclosure.

Oct 212009

So Matt,

I went to my local electronics (rhymes with Retardo Stack that hardly sells any electronics just stupid crap that no one wants) cell phone shop tonight to purchase the IR blaster for the Slingbox that I’m missing. Of course they don’t have it, this really didn’t surprise me as that store has become a just a glorified cell phone shop, more on that later. I ask the PFY (pimply faced youth) if they have IR blasters, and surprisingly he knows what an IR blaster is but tells me that they don’t have them. He then in a snarky PFY tone says “Maybe you can build one”…I’m standing right in front of the only redeeming thing about this store, the kiosk containing the raw electronics components that you can build a radio from scratch with. I think the guy saw my shirt and thought “Well this genius works at Rackspace so he probably thinks he knows what he’s doing.” I look back at him and say “I think I will”.

So I start going through the bins and gathering the parts for my challenge. Lets see; I need an IR emitter, check; some resistors of various sizes, I can’t calculate resistance in my head and I have no idea what the voltage coming out of the slingbox is so I get the mega jumbo 500 pack, check; some 20 guage wire, check; a 3/32″ mono headphone plug, check; and while I’m at it I pick up a sweet little clamp/magnifying glass holder thingy and a new soldering iron.

Supplies

I get all of this junk home, move the TV to the M-i-L room, kiss the kids goodnight and get started on my project. The first thing I do (after cleaning all of the junk from the last failed project off of my desk) is figure out what size resistor I need. Google is my friend, I search for LED Resistor Calculator and input what I think is a good voltage, 5V. USB is 5 volts and I’m going to be testing this thing with USB off of the bat, plus there is no way that the Slingbox is sending more that 5 volts to the IR blaster. The calculator does it’s magic and tells me that I need a 38ohm resistor, cool I have a 39ohm in my pack. Googling for the color codes on resistors I come across this site and determine that I’m looking for orange white black gold. I dig through half a billion resistors and find my beauty.

39ohm_resistors

So now I have to solder the resistor to the long end of the LED (I am a terrible solderer BTW, but I did discover tonight that a nice soldering iron makes all the difference). I solder up the LED to my +/- wires and now have to test this bad boy. I figure out which are the power leads from an interchangeable USB cable with my handy dandy volt meter.

USB_Voltage

Sweet, so now I wire up the LED to the USB and hook up my web cam. Back in nam I learned that cameras can see infrared while the human eye can’t. Don’t believe me? Take a remote and point it at the camera on your iPhone, watch the LED as you push the buttons on the remote through your phone and marvel at modern technology. With my sweet new vice thingy I point the camera directly at the LED and give it the sweet USB juice.

IR_blaster_usb_off IR_blaster_usb_on

Good grief! That is bright, but my contraption works! Awesome! So now I solder my 3/32″ phone plug to the other ends of the leads and get ready to test it out with the Slingbox. I fire up the player on one of my Macs, connect via VNC and start pressing buttons. As you can see the stupid Slingbox is only putting out a fraction of the 5 volts that I’m getting from USB.

low_voltage_off low_voltage_on

So I hook up my volt meter directly to the Slingbox and the darn thing is pushing out about 1/2 a volt. I check my calculator and it says that I’m doing it wrong and I need more voltage. LIAR! I decide to swap out the 39ohm resistor with a 2.2ohm resistor, red red gold gold.

2dot2ohm

Cool, my soldering skills are improving. Hooking the contraption up again I’m much happier with the results.

2dot2off 2dot2on

Sweet, you now have a custom IR blaster cable, bye the way you owe me $432.75 for the parts :) I hope your happy and stop saying all of that bad stuff about me, like coffee pot. To the PFY I would like to point out that this is why I work at the Rack and you work at the Shack LOL :)

Enjoy your Slingboxity swine,
Love Dave

P.S. The one thing I realized right as I was finishing the uber custom IR blaster cable was that I could have probably done all of this with a 99 cent mini to 3/32″ converter. Oh well, I had fun and learned something.