Programmatically open tab in gnome-terminal, execute command, and have tab stay open

12

10

I am trying to write a command that will launch a few terminal tabs, execute something in each tab, and have each tab stay open after the command in finished, so I can look at the output and type more commands in each tab

something like this:

gnome-terminal --tab -e "ls -a" --tab -e "ls"

but the problem with this is that the tabs close as soon as the "ls" commands finish. Does anyone know how to make the tabs stay open?

Mark

Posted 2010-08-03T23:57:03.453

Reputation: 123

Answers

11

Gnome-terminal can either execute a command or open a shell, but not both.

There is a workaround to do both by encapsulating the command and subsequent invocation of the shell into one command.

$ gnome-terminal -e "bash -c \"echo foo; echo bar; exec bash\""

For more alternatives read my answer to a similar question on stack overflow: https://stackoverflow.com/questions/3512055/avoid-gnome-terminal-close-after-script-execution/3531426#3531426

lesmana

Posted 2010-08-03T23:57:03.453

Reputation: 14 930

10

If you have xdotool and wmctrl installed, then the following shell script might work:

#!/usr/bin/env bash

window="$(xdotool search --class gnome-terminal | head -1)"
xdotool windowfocus $window
xdotool key ctrl+shift+t
xdotool type "$*"
xdotool key Return

I use it like this:

$ run-in-new-tab 'ls -l'

I found this idea on Trustin Lee's blog.

Jim Blandy

Posted 2010-08-03T23:57:03.453

Reputation:

Actually wmctrl is not required. – nedim – 2016-07-14T10:11:45.147

Thanks. This works pretty well. I have to add a sleep 1 after the ctrl+shift+t to get it to work for me though. I'll accept this if nothing else comes up soon – None – 2010-08-04T05:07:03.113

Yeah - i found i had to have sleep 2 to make it work 100% of the time. Great solution though - i'm now opening 8 tabs, which ssh into various servers, start various processes etc, using this: my morning setup is now totally scripted :) – Max Williams – 2013-02-21T10:00:02.057