A startup script for Byobu
Byobu, is an excellent tool when you have to SSH into a virtual machine that you use regularly for development. Every morning I come in, I do a vagrant up on the project I’m going to be working on for the day. After that I usually have to go about setting up different terminals for my layout. I typically have one terminal for the server log, one for a Python shell, and another for other shell commands I need to run. I always wanted to automate that, and I did. Here’s how… I use Byobu tmux instead of screen btw (use byobu-select-backend to set this).
if [ -z "$(byobu list-sessions | grep code)" ]
byobu-tmux new-session -d -t code # creates a new detached Byobu session
byobu-tmux rename-window server-log # Byobu comes up with one window, so rename that
byobu-tmux send-keys -t 0 'python manage.py runserver_plus 0.0.0.0:8000' 'C-m'
# Create a new window for my Python shell and rename it...
byobu-tmux rename-window shell-plus
byobu-tmux send-keys -t 1 'python manage.py shell_plus' 'C-m'
# Create a window for random shell commands
byobu-tmux rename-window bash
# Enter Byobu
byobu-tmux attach -t code
For starters, we created a detached session using new-session -d. We need a detached session because once Byobu fires up, the subsequent commands won’t be interpreted until Byobu exits. So, detached means we’re still able to send more commands. The rest is pretty straight forward, we create new windows and rename them. The send-keys function is used to send ‘keys’ to the shell window. You can specify the target window with -t index. By default the active window will receive the keys. Finally, there’s a ‘C-m’ at the end of each key stroke since that’s a shortcut for Enter for all the Emacs fans out there.