12

Need to write a shell script that opens byobu terminal with separate tabs. First line opens new byobu session and subsequent lines connect to that session and open new tabs. Its kind of automate opening terminal.

Ex -

byobu new-session -s "Server" "redis-server"

byobu new-window "redis-cli"

byobu new-window "sudo mongod --port 27017 --dbpath /data/db/rs0 --replSet rs0"

byobu new-window "mongo"

Problem here is when I run once this shell script it runs only first command and then stops. If I run it again then it executes the remaining lines with the message:

duplicate session: Server

What am I doing wrong here ?

Amit Yadav
  • 949
  • 8
  • 19

2 Answers2

7

I think you are missing the first line from your shell script. See if this works

#!/bin/sh
# byobu_launcher.sh ver 20170915122301 Copyright 2017 alexx, MIT Licence ver 1.0

byobu new-session -d -s $USER

# redis window
byobu rename-window -t $USER:0 'redis-cli'
byoby send-keys "redis-cli" C-m
byobu split-window -v

# mongod
byobu new-window -t $USER:1 -n 'mongod'
byobu send-keys "sudo mongod --port 27017 --dbpath /data/db/rs0 --replSet rs0" C-m

# mongo
byobu new-window -t $USER:1 -n 'mongo'
byobu send-keys "mong" C-m

# Set default window as the dev split plane
byobu select-window -t $USER:1

# Attach to the session you just created
# (flip between windows with alt -left and right)
byobu attach-session -t $USER

with screen you can do this by adding to the end of ~/.screenrc

screen -t redis-cli 0
stuff "redis-cli\n"
screen -t mongod 1
stuff "sudo mongod --port 27017 --dbpath /data/db/rs0 --replSet rs0\n"
screen -t mongo 2
stuff "mongo\n"
select 1

I mostly use screen and sometimes use tmux. I haven't used byoby.

Alexx Roche
  • 3,151
  • 1
  • 33
  • 39
  • Finally somebody answered, I am happy. Actually first line is already there, but thanks for answer :) – Amit Yadav Sep 15 '17 at 09:46
  • 1
    Don't you need to use `-t $USER:` in the `send-keys` commands? – m01 Mar 20 '19 at 13:40
  • 1
    @m01 You can, but its certainly not obligatory. (Though it does sound like good practice.) The `man` just says `-t name` "set the title" Give it a try with `-t this_example` – Alexx Roche Mar 21 '19 at 12:55
  • 2
    Looks like if `-t ` is not specified, the `send-keys` commands go to the last created session/window – m01 Mar 21 '19 at 13:16
1

I wrote this a while back, maybe it might be of some help There is little comments, but the functions have obvious names

#!/bin/bash

# create terminals and rename




function extract_session() {
    arg=$1
    [[ ! "${line:0:1}" = "[" ]] && echo Not a session block && exit 1
    [[ ! "${line: -1}" = "]" ]] && echo Not a session block && exit 1
    echo "${arg:1:-1}"
}

function new_session(){
    sess=$1
    if [[ $(byobu has-session -t ${sess} &> /dev/null; echo $?) -eq 0 ]] 
    then
      echo Session ${sess} already exists
    else
      printf "Starting new session %s\n" "${sess}"
      byobu new-session -d -s ${sess}
    fi
}

function new_window(){
    win=$1
    printf "Window %s: %s\n" "${win_id}" "${win}"
    if [[ ${win_id} -eq 0 ]]
    then
    byobu rename-window -t ${win_id} ${win}
    else
    byobu new-window -n ${win} 
    fi
    byobu select-pane -t ${pane_id} -T ${win}
}

function new_pane(){
    pane_name=$1
    printf "\t%s\t%s %s\n" ${pane_id} "${pane_name}" "${split}"
    byobu select-window -t ${win_id}
    byobu split-window ${split}
    byobu select-pane -t ${pane_id} -T ${pane_name}
    
}

while read line
do
    if [[ "${line:0:1}" = "[" ]]
    then
    # begin SESSION
    SESSION=$(extract_session ${line})
    new_session ${SESSION}
    win_id=0
    else
    pane_id=0
    # create windows with panes
    for pane in ${line[@]}
    do
        if [[ "${pane:0:1}" = "|" ]]
        then
        # create horizontal pane
        pane=${pane: 1}
        split=-h
        else
        # create vertical pane
        split=-v
        fi
        if [[ ${pane_id} -eq 0 ]]
        then
        new_window ${pane}
        else
        new_pane ${pane}
        fi
        pane_id=$((pane_id+1))
    done
    win_id=$((win_id+1))
    printf "\n"
    fi
    
done <<EOF
[guake]
ipython
sam |sam
ROOT iROOT
scp
TDOD
vpn
[joplin]
[RTERM]
sam
ROOT
man
[xterm]
monitor | htop | net
ROOT
man
[LTERM]
sam
ipython
ROOT
man
[DEV]
iPython |tail |run
run2
help
auto-scp
django
django-srv
[ansible]
playbook
inventory
[SSH]
ssh
[SSH2]
ssh
[SSH3]
ssh

EOF

The part at the bottom is the here document where you can add session-names, with panes. I wanted to add also certain commands that should be executed, but I've gotten there

oneindelijk
  • 606
  • 1
  • 6
  • 18