Monday, December 28, 2015

Bash yourself in the background!

There are many occasions for wanting to run bash scripts in the background. The two that jump out at me are startup scripts and scripts to be run while SSH-ed into a remote server. This post deals mainly with the latter, though it should work for both.

First, create a bash script you want to run. I wanted to monitor the memory of a running EC2 server instance to ensure we didn’t have memory leaking from our app, so here’s my bash script:

# log/memory_profiler.sh
while true
 do
    date 2>&1 | tee -a /srv/application/my_application/log/memory
    free -m 2>&1 | tee -a /srv/application/my_application/log/memory
     sleep 1
 done

This writes the current datetime and the current memory profile to a log file in my app’s directory.

Then, I wanted to run this script from a detached shell session so that when I closed my SSH connection, it would continue running, and also so that I could continue working within the open shell session. To do this, I use a tool called “screen.” Here’s my command:

screen -d -m -S "Memory Profiler" bash log/memory_profiler.sh

Here’s what’s happening: We run the bash script in a detached (-d) shell session, named (-S) “Memory Profiler”. The -m flag ensures a new shell session is created, regardless of where the screen command is called.

Simple enough, right? Then, to view what’s going on with this detached screen session, you just have to reattach the shell session. If you only have one screen running, enter

screen -r

If multiple, you’ll have to specify the named session.

screen -r "Memory Profiler"

Exit as normal using ctrl+c or typing exit.

If you want to get really fancy, you can run this on a schedule - something I've written about previously.

(Lots of good info in the screen docs.)

Monday, December 21, 2015

Funky Ruby Class Method Syntax

I’ve run across this syntax a few different times but never grasped how it worked, and I guess never cared enough to look it up. Turns out it’s dead simple.

This:

class Person  
  def self.species
    "Homo Sapien"
  end
end

is equivalent to this:

class Person  
  class << self
    def species
      "Homo Sapien"
    end
  end
end

Thanks to Yehuda Katz for finally clearing that up for me.

Monday, December 14, 2015

Literally the coolest way to build an array

One of Ruby’s literal syntax shorthands, %i essentially loops over each word, converts to a symbol and adds it to an array. For example:

%i(dog cat mouse)
# => [:dog, :cat, :mouse]

Caveat: This syntax was added in Ruby 2.0, so don’t go trying it in any previous version.

Monday, December 7, 2015

Familiarize quickly in new code with this trick

Here’s a quick and nifty little trick that comes in extremely handy in a lot of situations, but particularly when dealing with an unfamiliar codebase, such as at a new job or working with a previously unused external gem.

In the rails console, enter:

ls Model 

and it will show all the class and instance methods, as well as associations and a bunch of other cool stuff for that model.

I’ve begun using this in place of these two:

Model.methods
Model.new.methods

Not only because it’s simpler with less typing but it else ends up being much easier to read.