Running Shell Commands in Ruby
One of the major benefits of unix is the quantity of many simple programs that one thing great. As a developer for unix, you must learn how to leverage these commands into your own scripts. Here is how to do this in Ruby.
Say you script wants to call on the unix command to view the amount of freespace on your disks.
df -h Filesystem Size Used Avail Capacity Mounted on /dev/disk0s3 37G 23G 14G 62% / devfs 110K 110K 0B 100% /dev fdesc 1.0K 1.0K 0B 100% /dev
To add this to Ruby all you need to do is to add a line of code (note the back-ticks):
disk_free = `df -h`
That takes the output from the command and puts it into a String. So an example script that will show the “df -h” output could be
#!/usr/bin/env ruby def disk_free diskfree = `df -h` puts diskfree end disk_free
How simple is that!
Now even though it would be easier to just use the real “df” command for this, you could easily write a Ruby script to call on other scripts and use all of Ruby’s powers to modify the output. That is how I made the Server Status Sidebar, calling “uptime” and regex to get the correct string I wanted.
Happy coding.
Eric
(Found this tip by using Safari)
Server Status Plugin
I just got a new sidebar plugin developed for Typo here. It shows the uptime and load average of the server it is running on.
It should work on any server that allows the “uptime” command to work. I have tested it on OSX(Darwin) and Debian 3.1 so far and it works on both.
I am going to hold on to it for a bit to test it out some more but will add the patch to Typo soon. (Main stopper is my diff isn’t coming out right.)
Anyways see it in action over here on my sidebar and if you want to grab the copy, it is at my dropbox. The diff comes from REV 657, even though it doesn’t say.
This plugin, plugin code, and all it’s goodies will be released under the MIT License (same as Typo).
Eric
Back on Typo
Well I am back on Typo after going to Wordpress for a bit. The reason I left was because after about 12 hours of being up my website would stop responding. Lighttpd was the only thing that logged it but it was a weird log message
2005-09-20 11:59:12: (server.c.937) connection closed - write-request-timeout: 9
And it seemed to do this on a static image (typically the header image). So I went from Typo/Rails/FastCGI/Lighttpd to Wordpress/PHP/FastCGI/Lighttpd. But then I found that it happened in WP too (the header image file again).
I tried almost every config I could for the website and decided that my lighttpd or FastCGI was wrong. So I updated to the latest lighttpd source and gave WP/PHP another go.
Still broken, so I started to tear up my server trying to find the solution. Then I remembered that when I was mirroring large files, the download would just timeout. The only thing that would fix it was removing the bandwidth throttle.
server.kbytes-per-second = 50
After dumping that one line of code, I restarted my server and crossed my fingers. Well Wordpress stayed up over 26 hours, even with ab hitting it hard. So next I setup Typo on a subdomain and hit it hard and long. Typo had over 100,000 pages in it’s cache after a few hours, but it stayed up and pretty responsive even with development mode and over 25 concurrent connections.
Now it is about 24 hours latter and Typo and Wordpress were ok. So I now am running Typo again, my favorite { CMS | blog } ever.
Now to see what goodies TRUNK has for me.
Eric, back with AJAX
Choosing Names in Your Database
At work I was working on a simple Ruby on Rails project that is basically going to use CRUD to store, edit, and view information, something simple. I kept getting really weird errors with my scaffolding. After much mangling done with my database and code I found the problem was that I named a field in a table “System”.
This field was supposed to keep track of the Computer System so System was a nice shorthand for it. Well my shorthand cost over 6 hours of headaches. Turns out by using System, rails tried to create a new instance of it to reference the table but there already was a System in the namespace… namely “System” as in Kernelspace. So when my scaffold called System.send to try to read the data from the database it was actually calling the kernel of the , not the desired result you see.
After much googling and research I came across some links for my fellow rails developers (and myself for when I forget again). These are links to rails’ reserved words, the sql reserved words, and then just some words that should not be used in tables (ActiveRecord reserved words?)
Reserved Words in Ruby on Rails on the RoR wiki
Search for reserved words in various SQL database engines.
Eric
Apple's FileVault
Recently I turned on the “FileVault” option on my Mac. For those who don’t know FileVault is a preference that lets a user encrypt their /home directory. It is encrypted and the user sets a password to access it. Then when the user logs in their /home is decrypted on the fly as it is used.
Well I was looking at something eariler and noticed that the “Get Info” screen changed. Now not only is there the “where” area but also the “Disk Image” area. Mine said “/Users/.eric/eric.sparseimage”.
For the non-UNIX junkies, the dotted directory means it is hidden. So first thing I did was fire up a terminal and check out why I had a hidden directory. Come to find out there is an encrypted spareimage in there that is over 4GB. Ever more curious I then fired off a ‘df’ to see what was going on. Turns out my /home folder was mounted off a disk I didn’t know I had, but was the same size as my hard drive. Hmm these look connected.
Well to sum up, when you turn FileVault on it actually makes a hidden home directory for you, creates an encrypted sparceimage in there for you and then tranfers all your files over there. Then to finish up it mounts that image as your /home, thus making it appear nothing has changed. Kinda good idea. So for all those who have been using custom rsync’s to backup their computers (myself), can now just sync one sparceimage and it is already encrypted and setup corrctly.
Eric