Screen

January 19th, 2010

I love when I find useful built-in commands that I never knew about. When starting out with Linux, I heard about the typical heavy hitters like grep, sed, and awk. Then I discovered some variations on common commands, such as htop and less. And then every so often, someone mentions a command in passing that causes me to say “Wait… what is that?”

Screen is one of those sorts of tools. I was watching a VNC demo of one of our team’s sprint reviews and was distracted by the fact that he had a tab bar on the bottom of a terminal. It turns out that was just part of the coolness of “screen”.

Screen is basically a window manager that runs in a terminal, letting multiple shells run within the single terminal. I’m sure there are other features that I’d find awesome, but there are two that immediately stood out.

As I mentioned, screen allows you to run multiple shells inside of a single terminal. Instead of starting up a handful of separate SSH sessions to a server, I now just use screen to manage multiple shells inside of a single connection. Each call to screen creates a new shell, with a simple emacs-like syntax to switch between them:

ctrl + a + n    #  next
ctrl + a + p    #  previous
ctrl + a + 0    #  select screen 0

One side note on this, by default the tab bar displaying the running shells isn’t displayed. Adding the following to ~/.screenrc enables a visual indicator of the current and possible shells that are open:

hardstatus on
hardstatus alwayslastline "%{-b gk}%-w%{+b kg}%50>%n %t%{-b gk}%+w%< %= %{w}%D %M %d %C%a"

While useful, it can still get a little confusing when a bunch of shells are opened. The title can be set by:

ctrl + a  A

The second major piece of awesomeness is the ability to detach from a running screen and reattach later. Going back to the SSH example, this lets me leave some shells running, detach, completely disconnect from the server, and then reattach to the screen later (with everything left exactly as it was).

Again, an emacs-like syntax for detaching from a running screen:

ctrl + a + d

Later, to reattach to the running screen detached from earlier:

screen -dr

Like I said, I’m not trying to make this post sound like I’ve discovered some hidden black magic in Linux. It’s just an attempt to let new users know about this awesomeness.

Devil’s Pie

December 9th, 2009

I’ve been using Gnome for almost a year and a half now, and while I have grown to love a lot of the advantages it has, there are still some things I miss about FVWM. From a keyboard shortcut standpoint, many of them were alleviated when I discovered Gnome Do. However, I still found myself missing sticky, transparent shells that would give me a constant view into certain data, be it htop or tailing some files. It was really quick to set up in FVWM, but I had trouble finding a way to remove window decorations from windows in Gnome.

Devil's Pie + Gnome Terminal = WinClick for 1680×1050 version

Sure, there are other ways to get the data I’m looking for, but having text rendered directly on the desktop like that just looks damn cool to me. It also intimidates the crap out of non-geeks when they see my office, which I fully admit is a big plus.

I came across a package called Devil’s Pie that gave me a lot more control over windows. Although I was looking for something that would let me actively select a window to give it certain properties (sticky, no decorations, etc), Devil’s Pie instead passively detects new windows and applies settings to them. It’s a mentality shift from my initial thoughts, but I think over time this model will actually be really useful. Plus, it has “pie” in the name, which is just awesome.

With a combination of gnome-terminal profiles/flags and Devil’s Pie configuration, I was able to get my desktop to automatically populate my desktop with all sorts of data. The following description of how is kinda wordy, but it’s really not complicated at all.

Install and Configure Devil’s Pie

sudo yum -y install devilspie
mkdir ~/.devilspie
touch ~/.devilspie/console.ds

Devil’s Pie reads all configuration files (I believe they have to end in .ds, but don’t quote me on that) in the above directory on startup. Those files define the rules that will be applied to new windows that are created (there’s also a way to ask it to apply rules to all existing windows as well). For this example, I put my rules in a file named console.ds.

(if 
(contains (window_name) "console" )
(begin 
(undecorate)
(pin)
(skip_pager)
(skip_tasklist)
)
)

There are two main parts to the above configuration. The first is the matching expression to decide if a window should have the rules applied. In this case, I indicate to apply the actions if the window name contains the string “console” (there are also options for “is” and “matches”). I’ll cover where that rule comes into play in a bit.

The actions do the following:

  • undecorate – This is primarily what I was going for, I want the window decorations removed. This includes the title bar and border, though you can still use the shortcut key (alt+F8 by default) to resize a window without decorations.
  • pin – Make this window available on all workspaces. There are also attributes to control putting a window on a particular workspace, which could be useful in my case since I use 12 workspaces and am, well, pretty anal retentive about having certain applications in certain workspaces.
  • skip_pager – Don’t let the window show up in the pager. It removes the clutter in my pager since I don’t consider this a “real” application, just a status-type window.
  • skip_tasklist – A little more useful than skip_pager, this keeps the window off the taskbar, for the same rationale as above.

Needless to say, I’m only scratching the surface of all of the actions supported. I haven’t found a good way to find this list yet; the man page says the -l flag will list symbols but when I try it, it just doesn’t do anything.

Run Devil’s Pie

devilspie

Not much to say about that. I’ll talk more about enabling a debug mode later, but I mention it now to say it’s not done through flags. You’ll need to restart it every time you change your confguration files, but once you’re set add it to System -> Preferences -> Startup Applications and you’re good to go on reboots.

Create Gnome Terminal Profile

To summarize above, Devil’s Pie will take care of removing window decorations and keeping it on the visible workspace. We still need to configure Gnome terminal to add some slickness to how it looks. Since I don’t want this slickness on all of my terminals, I created a new profile named “console” with the following changes:

  • Background – Transparent background, with the slider set all the way to none.
  • Scrolling – Scrollbar is: Disabled

That gives us a completely transparent shell, reflective of its use to simply throw data at me.

Start the Terminal

Now we just need to make sure we start the terminal such that it will use the right profile and will trigger the window detection from Devil’s Pie. Here’s where the matching expression from above comes into play. I don’t want all of my terminals to have no decorations, just specific ones I want to treat in this fashion. When starting the terminal, tell it to use a specific window title that will match our Devil’s Pie rule from above.

gnome-terminal -t console --profile=console --geometry=140x20+1680-0 -e htop

A quick explanation of above:

  • -t – Sets the title of the terminal window. This value should match up to the window_name value from the Devil’s Pie configuration.
  • –profile – Tells the terminal to use the slick console profile in all of its transparent goodness.
  • –geometry – Initially size and place the terminal on my screen.
  • -e – Run a specific command when it starts, in this case htop.

My screenshot above simply uses a similar call to gnome-terminal, passing a tail command to -e and different geometry to get the log tail on the top of the screen.

Two other notes on Devil’s Pie:

There is a UI for editing the configuration files: gdevilspie. It’s handy because you can easily see the different types of matching expressions and actions, however the files it generated didn’t run for me. For some reason, even with one matching expression it still added a blank condition and an “and” statement and closed the conditions block too early. I haven’t looked to closely yet to see what is wrong.

Enabling debugging is as simple as adding another configuration file. I added one named “debug.ds” to my ~/.devilspie directory that contained simply:

(debug)

Once that is in place, running the devilspie executable from the command line output some really useful window information whenever a new window was created.

Command Line Fu

July 3rd, 2009

This link seems to be being passed around the blogs, but I first saw it at pjp news.

http://www.commandlinefu.com

It’s a collection of user uploaded and ranked useful commands. The idea is similar to the two entries I wrote on shell tricks but on a much bigger and cooler level. Just quickly poking around the first page of the site has already shown a number of things that are going to come in really handy. They also support all the usual useful channels like RSS and twitter, both of which you can use with a filtered threshold for user votes to limit the incoming commands to the truly awesome. There is some serious black magic to be learned on this site.

Shell Tricks – Volume 2

March 29th, 2009

There is always new cool stuff to learn, especially when it comes to using a command line. It’s always been interesting to me to see scripts and random commands my coworkers use since there is inevitably some arcane yet handy command I never knew existed.

A few months ago I posted Shell Tricks, a pretty random set of short cuts and tricks I use on a regular basis from a shell. Pretty much immediately after finishing it I started this post as a way to keep a running track of things I picked up after the original post. I figured once the list got to a reasonable size I’d clean it up and post it.

  • Change to the last directory – I forget where I picked this up, but I’ve been using it for as long as I can remember. To switch back to the previous directory you were in, run cd -. For example:
  • -> cd code/rhn/spacewalk/
    -> cd ../satellite/
    -> pwd
    /home/jdob/code/rhn/satellite
    -> cd -
    /home/jdob/code/rhn/spacewalk
  • Insert the last argument – While I use the up and down arrows regularly to scroll through previously executed commands, sometimes I need to execute a new command on the same argument I previously used. Typing the escape key and then period will fill in the last argument from the previously executed statement.
  • This is less a shell trick and more of a “if you never knew this existed, it rocks and you want to install it.” My coworker Partha introduced me to rlwrap a few months ago. It came out of a frustration that sqlplus didn’t offer a way to use the up and down arrows to look through a history of SQL I’ve typed. That made it really annoying if I typed out a long ass statement only to find I made a typo early on. Now, if I run rlwrap sqlplus I get all of those nice user input features.

    I’m not sure how clear that is to the reader, so I’ve included a snippet from the rlwrap man page:

    rlwrap runs the specified command, intercepting user input in order
    to provide  readline’s line editing, persistent history and completion.
  • Cowsay – Cowsay is an example of why geeks are pure awesome. When you run cowsay, you give it a string of text and it outputs ASCII art of a cow saying it. I’m not kidding. I know it’s available to both Fedora and Ubuntu distributions, and I’m sure many others offer it. For example:
    -> cowsay Geeks are awesome
     ___________________ 
    < Geeks are awesome >
     ------------------- 
            \   ^__^
             \  (oo)\_______
                (__)\       )\/\
                    ||----w |
                    ||     ||

    In case you were thinking it couldn’t get any more full of win than that, believe it or not it actually supports a pretty detailed series of arguments. For instance, -d renders the cow dead (look at the eyes):

    -> cowsay -d Dead cow
     __________ 
    < Dead cow >
     ---------- 
            \   ^__^
             \  (xx)\_______
                (__)\       )\/\
                 U  ||----w |
                    ||     ||

    There are also options for tired cow, greedy cow, stoned cow (my favorite), and others.

    It gets even better. There are other templates besides cows. For the Canadians:

    -> cowsay -f moose Moose
     _______ 
    < Moose >
     ------- 
      \
       \   \_\_    _/_/
        \      \__/
               (oo)\_______
               (__)\       )\/\
                   ||----w |
                   ||     ||

    A rather detailed dragon v. cow fight:

    -> cowsay -f dragon-and-cow Dragon and Cow
     ________________ 
    < Dragon and Cow >
     ---------------- 
                           \                    ^    /^
                            \                  / \  // \
                             \   |\___/|      /   \//  .\
                              \  /O  O  \__  /    //  | \ \           *----*
                                /     /  \/_/    //   |  \  \          \   |
                                @___@`    \/_   //    |   \   \         \/\ \
                               0/0/|       \/_ //     |    \    \         \  \
                           0/0/0/0/|        \///      |     \     \       |  |
                        0/0/0/0/0/_|_ /   (  //       |      \     _\     |  /
                     0/0/0/0/0/0/`/,_ _ _/  ) ; -.    |    _ _\.-~       /   /
                                 ,-}        _      *-.|.-~-.           .~    ~
                \     \__/        `/\      /                 ~-. _ .-~      /
                 \____(oo)           *.   }            {                   /
                 (    (--)          .----~-.\        \-`                 .~
                 //__\\  \__ Ack!   ///.----..<        \             _ -~
                //    \\               ///-._ _ _ _ _ _ _{^ - - - - ~

    And of course a sheep.

    -> cowsay -f sheep Sheep
     _______ 
    < Sheep >
     ------- 
      \
       \
           __     
          UooU\.'@@@@@@`.
          \__/(@@@@@@@@@@)
               (@@@@@@@@)
               `YY~~~~YY'
                ||    ||

    I’ll stop there. Trust me, there are more. This is what happens when you let geeks have free time.

  • The sl command is arguably even cooler than cowsay. I can’t even fully do it justice in a blog entry, so I’ll leave it to the reader to install and see for yourself. My understanding is that it was meant as a way to mess with noobs who typed “sl” instead of “ls”. Not sure how legit that is, but I like the explanation. :)

Shell Tricks – Volume 1

November 5th, 2008

I saw a post on Slashdot about “Stupid Unix Tricks” that talked about common things you take for granted in Unix or just flat out forget are there. With my RHCE exam coming up, it got me thinking about how many aliases and shell scripts I’ve written for myself that I use daily that won’t be there on the exam. Between those and my preference for emacs over vim, I need to reacquaint myself with a vanilla Red Hat Enterprise Linux installation so I’m not constantly stumbling over my habits on the exam.

Below are a list of either my favorites that I use constantly (some aren’t mind blowing but just a look into my habits) or ones from the Slashdot article that seemed particularly useful or cool.

  • Colored ls – If your shell doesn’t include this by default, this alias will use color for all of your ls calls.
  • 1
    
    alias ls='ls --color=auto'
  • jardump – For each JAR file found in the current directory or any child directories, output its contents to a file named jardump.txt. Very useful when using maven as it can be run in the maven repository directory to get a full listing of all classes in all JARs in the repository.
  • 1
    
    find . -name '*.jar' -exec unzip -l {} \; > jardump.txt
  • Change to a real directory from a symlink.
  • 1
    
    cd `pwd -P`
  • Commands can be executed on a remote machine through SSH without actually logging in. This is especially useful if you use key based login instead of passwords.
  • 1
    
    ssh [user]@[server] [command]

    For example:

    1
    
    ssh professorjay@novasurv.com ls /var/www
  • Human readable listing of disk space.
  • 1
    
    df -h
  • List the contents of a zip file without actually unzipping it (tip: pipe it to less to scroll the contents if they are long).
  • 1
    
    unzip -l [zip file]
  • Extract a file in a zip to standard output rather than writing it to the disk, letting you read a file without unzipping the zip file. Again, you can pipe this to less to scroll the contents of the file.
  • 1
    
    unzip -p [zip file] [full path to file in the zip]

    For example:

    1
    
    unzip -p dom4j.jar META-INF/MANIFEST.MF | less
  • A coworker introduced me to tee, which reads from standard input and writes to standard output and to a file. I use it when building so I can see the build messages in my shell like normal but also have a copy stored somewhere. The easiest way to explain it is with an example:
  • 1
    
    make | tee /tmp/make-results.txt
  • Add a new supplementary group to a user without having to respecify all of the user’s existing supplementary groups.
  • 1
    
    usermod -aG [newgroup] [user]

Find and Run Script

October 31st, 2008

I have a number of utility scripts I’ve written that I keep shared between all of my Linux machines. Mostly stuff to automate tasks I find myself regularly doing.

One such script is femacs, which is short for “find emacs”. More descriptively, the idea is to combine the call to search for a file with actually opening the file in emacs. The script is pretty simple:

1
find -name $1 -exec emacs {} \;

And it’s usage is what you’d expect:

1
femacs index.jsp

Despite it’s usage of the find command’s ugly syntax, it suffers from one very large flaw. If there are multiple files that match the given file name, they will all be opened in their own emacs process. It hasn’t been an issue since I’ve been careful to only use it when I know there is a single file I am looking for.

A coworker apparently was doing the same thing (except he is a vim user). He finally got tired of the limitation and took the time to make a much more robust version of the script. His blog entry on the change can be found here. It wouldn’t take much to adapt this to virtually any application you’d want to run with this sort of functionality.

Fun with grep

September 25th, 2008

A few days ago, I was trying to get a list of all of the error codes we use in our XML-RPC exceptions. They all seemed to follow the pattern of calling into a superclass’ constructor, specifying the error code as the first parameter. For example:

1
super(2754, "kickstartKeyAlreadyExists", ...

I used my normal, admittedly a bit naive, approach:

1
find -name '*Exception.java' -exec grep 'super(' {} \;

In other words, find all classes that end in Exception.java and execute grep on them. The problem with this approach is the output:

1
2
super(1025 , "notActiveSatellite" , ...
super(2301 , "invalidPackageArch", ...

It matched what I wanted it to, but it obscures the normal grep output of showing the file name. After some asking around in the #spacewalk Freenode chat room, we came up with two different ways to achieve what I wanted.

1
find -name '*Exception.java' | xargs grep 'super('

I’ll leave it to the reader to check the man page for xargs for more details. As a quick summary, it’s a way of building up a command line based on an input. It’s syntax is also a ton nicer than using the -exec syntax of find.

A more to the point approach that doesn’t use find is:

1
grep 'super(' -r ~/code/rhn/spacewalk/java *Exception.java

One last approach that was suggested is similar:

1
grep 'super(' **/*Exception.java

The problem with the above is that it doesn’t work in bash; the original suggester likes to be weird and use zsh.

For the record, the output of the above looks as follows:

1
../xmlrpc/InvalidPackageException.java:  super(2301,

That’s just a snippet that would fit on the page correctly. The important aspect is that it shows both the file name and what was matched. In most cases, that’s significantly more helpful. :)