How can I make a script that opens terminal windows and executes commands in them?
gnome-terminal -- command
or
xterm -e command
or
konsole -e command
Pretty much
terminal -e command
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 with
gnome-terminal --window-with-profile=NAMEOFTHEPROFILE -e command
Instead of hard-coding gnome-terminal
, konsole
, et cetera, use the Alternatives system. The program that executes the default terminal emulator is:
x-terminal-emulator
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 for konsole
and gnome-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:
#!/bin/sh
"$@"
exec "$SHELL"
If you've saved the previous script as /home/user/hacky
and made it executable, you would run your scripts with:
x-terminal-emulator -e /home/user/hacky your-script optional arguments here
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
.
Quite simply-
#!/bin/bash
/etc/init.d/ccpd status
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
#!/bin/bash
gnome-terminal -e "/etc/init.d/ccpd status" --window-with-profile=NAMEOFTHEPROFILE
The other post intended []
to be a placeholder
Here "NAMEOFTHEPROFILE" is to be replaced with the name of the profile that "Holds the terminal when the command exits".