Linux Server Wrangler (Middle-Weight Class) /w touch of DevOps. Ex-Projectionist, Geek, Android Abuser & Gravely Gay! ;) Opinions noted here are mine, all mine!

boybu-and-screen-naming

I’ve long been a fan of screen, and more recently byobu as a means to provide a quick and easy-to-use interface on the top of it. Rather than large selections of terminal windows open, I have one big one now, and inside I’m connected to 10-40 servers at a time. The ‘tabs’ are named and it’s quick and easy to move around. Plus, when I’m out of the office, I can log into my PC remotely and pick up where I left off.

The downside for me as been the the naming of the tabs; always a little bit fiddly. I don’t always remember and its annoying to have to when I’m in a rush, needing to open multiple tabs quickly. However, there is a way to deal with this automatically.

Both screen and tmux (the two backends supported by byobu) understand an ASCII-escape command from the PROMPT_COMMAND environment variable. Therefore, with this some simple code in your ~/.bashrc, logging in and out of servers (and moving between users) will ensure that your tabs are always up-to-date (even when you log back out – the tab name will revert back to the old name using the PROMPT_COMMAND variable from the parent session).

PROMPT_COMMAND='echo -en "\033k${USER}@${HOSTNAME}\033\\"'

Be careful however with this: If you’re not inside screen or tmux, this will output extra characters onto your screen, so its best as follows:

if [[ "${TERM}" == "screen" || "${TERM}" == "tmux" ]]; then
PROMPT_COMMAND='echo -en "\033k${USER}@${HOSTNAME}\033\\"'
fi

I also need the ability to handle larger history, and with multiple people working on servers, and multiple terminals open on my own machine, appending the history, rather than overwriting it on close, is also important. As this uses the PROMPT_COMMAND to assist with this too, you can combine the two as so:

# Reconfigure some of the History settings, as I often have
# multiple windows open and running and therefore would like
# to keep the information from each of them. Therefore, set
# up Bash to append to the .bash_history file and expand it
# to 10,000 commands.
shopt -s histappend
HISTFILESIZE=10000
HISTSIZE=10000
# Also, don't save spaces and duplicates and Timestamp the
# commands.
HISTCONTROL=ignoreboth
HISTTTIMEFORMAT="%F %T"
export HISTFILESIZE HISTSIZE HISTCONTROL HISTTIMEFORMAT
# Append new entries to history on each command, and automatically name
# a tab when using screen or tmux
if [[ "${TERM}" == "screen" || "${TERM}" == "tmux" ]]; then
  PROMPT_COMMAND='echo -en "\033k${USER}@${HOSTNAME}\033\\";history -a'
else
  PROMPT_COMMAND='history -a'
fi
# Make sure all that is available to sub-shells
export PROMPT_COMMAND

 Add a Comment  

This article (boybu and screen naming) was written by , posted on , and listed under SysAdmin.

Tagged with , , , .