open gnome terminal with several tabs and execute a few commands in every tab

27,184

Solution 1

This seems to work on my machine:

gnome-terminal --geometry=260x25-0+0 --tab -e "bash -c 'date; read -n1'" --tab -e "bash -c 'echo meow; read -n1' " --tab --tab

Please note, as soon as the processes executed by -e are done running, they will terminate. In this case, bash is loaded, runs whatever commands you pass to it, and immediately exists. I put in the read statements to wait for user input. This way those tabs won't close until you press a key, just so you can see it in this example. Without them, it would look as if only two tabs opened, because the other two would execute and close too quickly.

Solution 2

Save below as script.sh

#!/usr/bin/env bash

me=$(realpath $0)

commands=(
"echo"
"ls -l && echo 'yes'"
"top"
"ps"
)
len=${#commands[@]}

if [ $# -eq 0 ];then
    num=$len
else
    num=$1
fi

n=$((len-num))
command=${commands[$n]}
num=$((num-1))

xdotool key ctrl+shift+t

if [ $num -gt -1 ]; then
    sleep 1; xdotool type --delay 1 --clearmodifiers "$me $num; ${command}"; xdotool key Return;
fi

add script.sh to $PATH then run it

gnome-terminal -x script.sh

See Open a new tab in gnome-terminal using command line.

Solution 3

Since bash quits after the command completes, you can call another instance of bash after your command to get an interactive shell:

gnome-terminal  --tab -e "bash -c 'ps -ef; bash'" --tab -e "bash -c 'ls; bash'" --tab -e "bash -c 'top -n 1; bash'"
Share:
27,184

Related videos on Youtube

ekido
Author by

ekido

Updated on September 18, 2022

Comments

  • ekido
    ekido over 1 year

    This is what I want to accomplish:

    1. I want to open a gnome terminal with five tabs in it
    2. I want to run a set of commands (5 – 10 commands) in each tab automatically
      First tab: shall set clear-case view and after that execute one or more commands
      Second tab: shall login into a server and execute some commands
      Third tab: shall only execute some commands

    gnome-terminal --geometry=260x25-0+0 --tab -e "csh -c \"ct setview myViewName; cal\"" –tab --tab --tab (works ok, view is set but no command executed after that)

    I have tried to do it this way instead and running this in the script below:

    gnome-terminal --geometry 125x18-0-26 --tab -t "some title" -e /home/ekido/Desktop/MyScripts/myScript
    
    #!/usr/bin/expect
    exec gnome-terminal --geometry 125x49-0+81 –tab
    spawn ssh usert@server
    expect "password"
    send "*******\r"
    expect "user@server100:~>"
    send “some command\r"
    expect "user@server100:~>"
    send “some command"
    interact 
    

    If I remove the exec gnome-terminal --geometry 125x49-0+81 –tab rows from the example and call a script from some other file, it works fine -- I get logged in to the server and all commands executed. Can anyone help me solve this?

    To write a script that I call for every tab is not an option, since I will have 5 terminals with 5-7 tabs in each in the end, and that means it would be 25 to 30 scripts to write (cost more than it helps in my problem).

    • eyoung100
      eyoung100 over 9 years
      Not that I'm an expert in GNOME Terminal, but the tabs you create should be saved in a profile. After creating the profile, I believe you can script it. See Manage Profiles in the GNOME Help.
  • ekido
    ekido over 9 years
    thanks for your answer. Sure, I get to se the output of one command (in this case "date") but I would like to execute several commands, be able to se the output and then continue to write new commands manually in the same tab.
  • Blub
    Blub over 5 years
    it seems that if I close the terminal the underlying processes dont close automatically, is there a solution to this too? I would like to close the entire window and all tabs with their processes should get a kill
  • nyxee
    nyxee over 4 years
    everything else failed apart form this. Now, i would like to have some of the tabs open without a command. For example, i would like to have ten tabs open, but three if them could be running some commands like htop..
  • Scott - Слава Україні
    Scott - Слава Україні over 3 years
    This seems to be a very manual process — the question asks for a way to automate a task — and it doesn’t seem to accomplish what the OP wants.  Please describe how this answers the question. … … … … … … … … … … … … … … … … … Please do not respond in comments; edit your answer to make it clearer and more complete.
  • bluenote10
    bluenote10 about 3 years
    Note that -e has been deprecated in newer versions of gnome-terminal, which makes it more tricky to answer the question. To prepare for the deprecation, the best I could come up with is this solution.
  • BigBrownBear00
    BigBrownBear00 about 3 years
    How would you do this so the script also launches the gnome-terminal?