Persistent SSH sessions with screen

Do you ever need to kick off a long-running command while SSHed to a server, but be able to disconnect and reconnect at will? You can do this with screen.

Before doing anything, start a screen session:

screen

When you’re ready to put your work on hold, detach the screen:

screen -d

If you have a long-running command running, you can detach that screen from a different shell session by specifying the process id:

# look up the process id
ps aux | grep SCREEN
# detach that screen
screen -d <my_process_id>

You can now disconnect from the server safely.

When you reconnect, you can also reconnect to your screen session:

screen -r

 

How to rename a file in a File upload dialog on Mac

Windows users will scoff at this, but renaming a file in a File Upload dialog box on Mac is a surprisingly obscure action. Renaming is not available in the context menu, nor does the usual shortcut work.

Screenshot 2014-03-24 18.01.26To rename a file in a regular Finder window, you can select it and hit the Enter key. However, you can’t do this in a File Upload dialog as the Enter key has a different meaning.

To rename a file, select it, hit Cmd+I, open up the Name & Extension pane, and change the name.

Windows to Mac: Keyboard shortcuts

In December, I got a Macbook Air that I’m now using as a primary development machine. Before that, I was developing on a Windows 7 machine with heavy reliance on Git bash and Cygwin, and using Linux on the server. I’ve used Linux as a primary desktop at times, but found both dual-boot and VMs too much of a hassle.

I’m enjoying the Mac, but one of the biggest adjustments is keyboard shortcuts.

Here are some shortcuts that are specific to the Mac Terminal (and Terminal alternatives like iTerm 2):

[vtftable ]
Mac ;;; Mac Terminal ;;; Windows ;;; Action ;nn;
Cmd+Left Arrow ;;; Ctrl+A ;;; Home ;;; Go to start of line ;nn;
Cmd+Right Arrow ;;; Ctrl+E ;;; End ;;; Go to end of line ;nn;
Shift+Cmd+Left Arrow ;;; ;;; Shift+Home ;;; Select to start of line ;nn;
Shift+Cmd+Right Arrow ;;; ;;; Shift+End ;;; Select to end of line ;nn;
;;; Ctrl+K ;;; ;;; Clear line after cursor ;nn;
;;; Ctrl+U ;;; ;;; Clear line before cursor;nn;
[/vtftable]

Some general shortcuts:

[vtftable ]
Mac ;;; Windows ;;; Action ;nn;
Cmd+Down Arrow;;; PageDn ;;; Go one page down ;nn;
Cmd+Up Arrow ;;; PageUp ;;; Go one page up ;nn;
;;; ;;; ;nn;
Cmd+Space ;;; Windows key ;;; Start menu/Spotlight ;nn;
Cmd+Opt+Esc ;;; Ctrl+Shift+Esc ;;; Open Task Manager ;nn;
Cmd+Delete ;;; Delete ;;; Delete selected files ;nn;
;;; ;;; ;nn;
Cmd+Delete ;;; Delete ;;; Delete character in front of the cursor ;nn;
;;; ;;; ;nn;
Cmd+R ;;; F5 ;;; Reload current webpage ;nn;
;;; ;;; ;nn;
Cmd+Shift+3 ;;; ;;; Save screenshot to desktop/Dropbox ;nn;
Cmd+Ctrl+Shift+3 ;;; PrtScr ;;; Copy screenshot to clipboard ;nn;
Cmd+Shift+4 ;;; ;;; Save screenshot of an area of screen to desktop/Dropbox ;nn;
Cmd+Ctrl+Shift+4 ;;; PrtScr (SnagIt) ;;; Copy screenshot of an area of screen to clipboard ;nn;
[/vtftable]

Of course, there are many more shortcuts.

libdb2.so.1: cannot open shared object file: No such file or directory – … ibm_db.so

Got this error while deploying a Rails app on Nginx:

libdb2.so.1: cannot open shared object file: No such file or directory - ... ibm_db.so

This means that the ibm_db adapter is installed, but it can’t find the DB2 libraries. The issue is that IBM_DB_HOME and some other environment variables are not set.

The best solution is to make sure all users have db2profile loaded. Edit /etc/profile and add:

. /opt/dsdriver/db2profile

You should now reload your profile (. /etc/profile) and restart Nginx.

This assumes that you already have IBM Data Server Driver installed under /opt/dsdriver.

Linux command of the day: banner

banner can be a useful command for setting login and welcome messages (e.g via /etc/profile).

$ banner PRODUCTION
######  ######  ####### ######  #     #  #####  #######   ###   ####### #     #
#     # #     # #     # #     # #     # #     #    #       #    #     # ##    #
#     # #     # #     # #     # #     # #          #       #    #     # # #   #
######  ######  #     # #     # #     # #          #       #    #     # #  #  #
#       #   #   #     # #     # #     # #          #       #    #     # #   # #
#       #    #  #     # #     # #     # #     #    #       #    #     # #    ##
#       #     # ####### ######   #####   #####     #      ###   ####### #     #

The Mac implementation is less useful than the GNU/Linux one.

$ banner -w 40 PRO
          #                         #
          #                         #
          ###########################
          ###########################
          #              #          #
          #              #          #
                         #          #
                         #          #
                         ##        ##
                         ##       ###
                          ##########
                           ########
                             ####
          #                         #
          #                         #
          ###########################
          ###########################
          #             ##          #
          #          #####          #
                  ########          #
               ######### #          #
          # #########    ##        ##
          #########      ##       ###
          #####           ##########
          ###              ########
          #                  ####
          #
                    #######
                ###############
              ###################
            ######          #######
          ####                   ###
          ##                       ##
          #                         #
          #                         #
          #                        ##
          ###                     ###
           ######             #####
             #####################
               #################
                  ###########

 

JRuby for the Java .class is .java_class

I’ve been having a lot of fun working with a Apache jclouds in JRuby. All the examples for the API are in Java and Clojure, while online JRuby docs could be better, so there’ve been some interesting translation challenges.

I just had to re-Google what .class becomes in JRuby, so a quick note for the future.

Java:

template.getOptions().as(EC2TemplateOptions.class).keyPair("bluforcloud-test");

JRuby:

template.getOptions.as(EC2TemplateOptions.java_class).keyPair "bluforcloud-test"

 

Exploring Ruby and Python interactively

Both Ruby and Python offer great interactive shells, also known as REPLs (Read Eval Print Loops). These are handy for verifying snippets of code. You can invoke Python’s by simply running python or Ruby’s by running irb, jirb (for jRuby), or rails c (for Rails).

Sometimes, however, one can be mystified as to what one can do with an object or module. Lately, I’ve been finding the Ruby API documentation especially frustrating.

Fortunately, both Python and Ruby let you see what’s available. In Python, you can call the dir() function, while Ruby has the handy .methods() method.

Python:

>>> dir("some string")
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__','__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

And Ruby:

irb(main):003:0> "some string".methods
=> [:to_java_bytes, :upcase!, :ascii_only?, :lstrip, :upto, :lines, :encoding, :prepend, :scan, :==, :clear, :squeeze!,:chop, :next!, :casecmp, :start_with?, :split, :to_f, :center, :reverse, :sub, :byteslice, :&gt;, :upcase, :next, :strip!,:count, :sub!, :hash, :bytesize, :lstrip!, :to_sym, :&lt;=, :replace, :length, :swapcase, :gsub, :intern, :succ, :capitalize, :each_codepoint, :oct, :delete!, :+, :initialize_copy, :to_java_string, :match, :unpack, :index, :rstrip!, :*, :each_char, :gsub!, :to_s, :empty?, :size, :swapcase!, :ljust, :downcase, :rpartition, :to_str, :getbyte, :sum, :crypt, :partition, :reverse!, :=~, :force_encoding, :each_byte, :tr!, :inspect, :to_c, :rstrip, :succ!, :&lt;, :[]=, :valid_encoding?, :slice!, :slice, :insert, :tr_s!, :unseeded_hash, :squeeze, :dump, :===, :end_with?, :hex, :strip, :capitalize!, :bytes, :setbyte, :chop!, :each_line, :[], :encode, :include?, :chomp!, :&lt;&lt;, :encode!, :chomp, :rindex, :to_i, :&lt;=&gt;, :eql?, :tr_s, :chars, :codepoints, :delete, :chr, :to_r, :rjust, :%, :&gt;=, :concat, :ord, :tr, :downcase!, :between?, :handle_different_imports, :include_class, :java_kind_of?, :java_signature, :methods, :define_singleton_method, :initialize_clone, :freeze, :extend, :nil?, :tainted?, :method, :is_a?, :instance_variable_defined?, :instance_variable_get, :singleton_class, :instance_variable_set, :public_method, :display, :send, :private_methods, :enum_for, :com, :to_java, :public_send, :instance_of?, :taint, :class, :java_annotation, :instance_variables, :!~, :org, :untrust, :protected_methods, :trust, :java_implements, :tap, :frozen?, :initialize_dup, :java, :respond_to?, :java_package, :untaint, :respond_to_missing?, :clone, :java_name, :to_enum, :singleton_methods, :untrusted?, :dup, :kind_of?, :javafx, :java_require, :javax, :public_methods, :instance_exec, :__send__, :instance_eval, :equal?, :object_id, :__id__, :!, :!=]

 

Circular dependency detected while autoloading constant

I recently ran into this frustrating and intermittent error in Ruby on Rails 4 (JRuby, actually):
Circular dependency detected while autoloading constant

Googling turned up several articles advising one to abide by the Rails conventions, but that was not the issue.

The application I’m writing uses background threads. The problem shows up when trying to instantiate a Rails controller in one of them. Rails searches for object definitions dynamically, so when multiple threads are trying to instantiate the same object, there’s a race condition.

The fix is define a mutex for access control:

$thread_mutex = Mutex.new

And then use it in the threads when instantiating the controller, model, or some Ruby class:

mc = nil
$thread_mutex.synchronize do
  mc = MyController.new
end

Adobe password breach as the world’s greatest crossword puzzle

Adobe was recently breached and 150,000,000 user accounts were stolen. Adobe was following the one of the worst practices of password storage — reversible encryption (rather than hashing with a salt using a good, slow algorithm like bcrypt). A very, very old throwaway password of mine was among those leaked.

XKCD has referred to this breach as The Greatest Crossword Puzzle in the History of the World!

It was bound to happen eventually. This data theft will enable almost limitless [xkcd.com/792]-style password reuse attacks in the coming weeks. There's only one group that comes out of this looking smart: Everyone who pirated Photoshop.

With the help of LastPass’ Has Adobe Leaked My Password, let me illustrate why:

The following hints have been used by other people that share your password. This information could be used to determine your password as well.

  • Life, Universe, Everything
  • life?
  • DA
  • h2g2
  • hitchiker’s guide to the galaxy
  • yes
  • meaningoflife
  • theusual
  • everything
  • hitchhiker
  • dolphins
  • gta
  • a4
  • answer
  • meaning?
  • life
  • the answer
  • the question of life
  • HGTTG
  • meaning of life
  • the usual
  • life..
  • life the universe and everything
  • a2lae
  • the ultimate
  • Hitchhiker
  • What’s the answer?
  • hitchhikers?
  • Life the Uni and Every
  • life meaning and flower
  • common
  • douglas adams
  • a?
  • maiden
  • lotr no #
  • Adams question
  • Hitchhiker’s Guide
  • answer?
  • question
  • Life Meaning
  • adams
  • life universe everything
  • HHGTTG
  • the number
  • towel
  • typical
  • The Usual
  • How many roads must a man walk down?
  • Life, the universe, and everything
  • What is the meaning of life, the universe and all?

Would you care to guess what password the naive, young me used for Adobe?

Next steps

The specified bucket is not S3 v2 safe

I ran into this error when running ec2-upload-bundle:The specified bucket is not S3 v2 safe (see S3 documentation for details)This was due to uppercase letters or underscores. Later I also ran into an issue with periods in bucket names which showed up as this error message:ERROR: Error talking to S3: Server.AccessDenied(403): Access DeniedHere is an easy command to sanitize the bucket names:

sanitized_name=$( echo $name | tr [:upper:] [:lower:] | tr [:punct:] - )

It will lowercase all letters and convert all punctuation to dashes.