I have three scripts I need to run when I start up my Ubuntu machine, they start services I use in my development environment.
To do that, I manually open three terminals and type in the commands.
Is there any way to create a script that will open three terminals and execute one command in each of these? (Each command should be in a separate terminal window so I can see their output).
or
or
Pretty much
To make the terminal stay when the command exits:
In konsole there is a
--noclose
flag.In xterm, there is a
-hold
flag.In
gnome-terminal
, go to Edit -> Profile Preferences -> Title. Click the Command tab. Select Hold the terminal from the drop-down menu labelled When command exits. You should create a new profile for that and execute withInstead of hard-coding
gnome-terminal
,konsole
, et cetera, use the Alternatives system. The program that executes the default terminal emulator is:On my system, it opens a new instance of Konsole every time I execute this command.
Luckily, the terminals seems to support the
-e
option for executing a command (I verified it forkonsole
andgnome-terminal
). Arguments after the command are passed to the invoked command. Bash refuses to stay open in my terminal, an additional script is needed to get a terminal:If you've saved the previous script as
/home/user/hacky
and made it executable, you would run your scripts with:The full path is required and
/home/user/hacky
has to be executable.My previous attempt to run a script in a new terminal window can be found in revision #2, it was before I realised arguments can be passed to
x-terminal-emulator
.UPDATE 17 FEB 2020: this answer is now perhaps obsolete.
Consider clicking this link and using this other answer of mine instead: Open Terminal with multiple tabs and execute application
Aided by @nickguletskii's answer, and my own comment under his answer, and inspired by @grabantot's upvote of my comment, here's my preferred way to do it, especially when I want the terminal to stay open so I can then manually use it.
Ex. usage: this is really useful to add to your startup programs so this script will run, open a terminal, create and name a tab in the terminal, and run a command for you. Or, you can just add a symlink to this script to your desktop. I use this type of approach so I can double-click a single icon on my desktop and have it open up a bunch of terminals (with various tabs named according to what work I'm going to do in them) and programs to set up my programming environment, for instance, for daily work.
Here's a contrived example, which opens up a single tab, titles it "test", then runs the simple command
cd /etc; ls
inside it. The$SHELL
part at the end forces the shell to stay open so you can then see its output and continue using it (I learned this somewhere else on Stack Overflow):Here's a more complicated example which opens up 3 separate tabs in the same gnome-terminal. This is exactly the type of thing my desktop shortcut does so I can open up a bunch of programming windows at once:
Here's a breakdown of that command above:
gnome-terminal
= open up a gnome-terminal--tab
= open up a unique tab for what comes next--title="tab 1"
= title this tab "tab 1"--command="bash -c 'cd /etc; ls; $SHELL'"
= run thebash -c 'cd /etc; ls; $SHELL'
command, which is a command I just made up as an example; here's what it does:bash -c
says it is a bash 'c'ommandcd /etc
= 'c'hange 'd'irectory into the "/etc" pathls
= 'l'i's't contents of this directory$SHELL
= this cryptic tidbit is required to keep the shell open so you can work with it. If you want the shell to open, run your command, then close, simply remove this part. I, however, want the tab to stay open so I can go make programming magic. :)--tab
part to produce tab 2, then again for tab 3. Customize to your heart's content.Screenshot:
Quite simply-
This is enough for other commands that do not need to display anything in terminal. But here one has to see the status displayed.
So, it needs to run in a terminal window
The other post intended
[]
to be a placeholderHere "NAMEOFTHEPROFILE" is to be replaced with the name of the profile that "Holds the terminal when the command exits".
commenting for the answer by Lekensteyn. I know this is a old post, but for anyone who finds this useful (as I just did) Instead of making another "hacky script" with just put a function inside the script you are calling
Call your script with "x-terminal-emulator -e /path/to/script hacky_function optional arguments here"
Don't forget to put "$@" at the end of the script
Update to what I use now (2022)
Almost a decade late to the party but, here's my answer using Python.
In the
.gif
below I launched the program from an existing terminal with screen recorder running to show what it would look like at login:I wrote a python program for this answer. There are some extra features not requested by OP but beneficial to me:
gnome-terminal
tabs.gedit
and last five opened files in separate tabs.The python program:
Note you may have to tinker with the variable
BASHRC_TIME
on your system to make program run faster. I have a lot of functions running in my~/.bashrc
and yours may run a lot faster.I've planned on writing this for many years but never got around to it until now.
I got to chain multiple tabs like this, while avoiding warnings caused by using
--command
, which will be deprecated soon:Use the screen command and -d detach from an existing screen session, and reattach here -m force a new screen session -S create a named session instead of using the default name