27

I have a terminal command I run from an application launcher. This works great, however sometimes the command fails, so I'd like for the terminal to stay open so I can see the results. How can I do this?

0

9 Answers 9

26

Assuming your command is called mycommand, I'd change my launcher to run this:

gnome-terminal -e "mycommand|less"

If you want a more permanent, perhaps cleaner solution, open up gnome-terminal, go to Edit, Profile preferences and click the Title and Command tab. Change the "When command exits" option to "Hold the terminal open".

When you execute commands, it should now leave the terminal open when something runs.

Edit: If you don't really care about the terminal, you could just use xterm's hold flag:

xterm -e "mycommand" hold
2
  • 7
    To keep the default terminal 'when command exits' action, I suggest creating a new terminal profile, set it to stay open after a command exits, and pass gnome-terminal the --tab-with-profile=PROFILENAME option. You can have many profiles for different bahaviours this way :-)
    – invert
    Aug 31, 2010 at 13:22
  • 1
    xterm -hold -e mycommand
    – Roger Pate
    Aug 31, 2010 at 15:38
8

Your launcher is running a script right?

At the end of your script add

read -p "Press any key to exit > " -n1 junk
echo 

Then your script will wait until you choose to end it.

1
  • 1
    +1, and to show the prompt only if the command fails (returns a non-zero exit code) one could do command || read -n1 -p "Command failed. Press any key to exit..." Aug 31, 2010 at 12:50
8

In your .desktop shortcut, use this

Exec=gnome-terminal -x bash -c "YOUR_SCRIPT; exec $SHELL"

After your script is finished, the Bash process will replace itself with a new invocation of itself.

If you need to pass quoted arguments to your script, you have to escape the quotes:

Exec=gnome-terminal -x bash -c "YOUR_SCRIPT \"arg with spaces\"; exec $SHELL"
4

A slightly different approach from the other answers: run your command and if that fails, then spawn a shell. This way you don't have to hold the terminal open (which doesn't distinguish between command success or failure), and you might find that shell particularly useful in failure. To close it, just use Ctrl-D (EOF), "exit", Alt-F4 (or whatever your window manager uses to close windows), etc.

success-or-shell

#!/bin/sh
[ $# -eq 0 ] && {
  echo "$(basename $0): no command" >&2
  exit 1
}
"$@" || {
  echo "failed: $?"
  exec $SHELL
}

Place this file somewhere, such as ~/bin, then use "success-or-shell your original command" in your launcher.

0

This answer gives the best answer I've seen so far to doing what you want. They recommend making a script with the commands to execute and use it with the --init-file parameter (bash specific, but you could probably do the same for csh/tcsh/zsh, etc):

#!/bin/bash --init-file
commands to run

... and execute it as:

xterm -e /path/to/script
# or
gnome-terminal -e /path/to/script
# or
the-terminal -e bash --init-file /path/to/script/with/no/shebang
0

Use the watch command.

Format: watch -n60 myCommand, or watch -n60 "my command string"

  • the -n specified the number of seconds to wait between calls to myCommand; ex: the above command will call myCommand every 60 seconds, forever.
  • If the command is multiple words, put it inside double quotes: "my command string".

Let's say you want a Launcher which displays the disk usage, and stays open for the reader to see. I'd simply use this command inside the Launcher:
watch -n60 "df -h", which calls df -h now every 60 seconds.

I like this over the accepted answer of gnome-terminal -e "mycommand|less" because I'm running Xubuntu, which uses a different terminal by default.

0

Try right-clicking the launcher and entering properties.

There should be an option to keep the terminal window open enter image description here

In the .desktop file, there should be the entry X-KeepTerminal=true

0

This question has a good answer, that worked for me.

Long story short: every terminal emulator has a different way to keep its window open when a command exits.

If you want to use a specific terminal app, then you can add the appropriate command-line option it provides, if present, like roxterm --hold. Or for just your own use, you can toggle an option in the settings window for that emulator (like "keep window open after command exits").

If you want to use the default terminal app, (like via x-terminal-emulator or exo-open) then you need to use a trick in the script itself: open another shell at the end of it, like ...; bash.

This also works well for applications that have a "Custom Action" feature, like Application Finder (xfce4-appfinder) and Thunar. For example:

Thunar

Name: Show Path in Terminal
Command: exo-open --launch TerminalEmulator bash -c "echo '%f';bash"

Application Finder

Pattern: $
Command: exo-open --launch TerminalEmulator bash -c "%s;bash"

In the above examples, exo-open --launch TerminalEmulator opens the default terminal emulator, and everything after that is read as a command to run. bash -c runs a new bash shell process with the specified command. "...;bash" is the command, that when finished, runs a new bash shell process which keeps the terminal window open.

Another idea is to add sleep 36500d to the end of a script, which gives you 100 years to review the output.

You can also have your script listen for a terminal close signal, and override (or "trap") it.

Desktop Entry

Exec=exo-open --launch TerminalEmulator bash -c "function handle_sigint() { echo 'SIGINT received... but I will not close by myself.'; }; trap 'handle_sigint' 0; while true; do echo 'Running script...'; sleep 1; done;"

This all works for a script file. Unfortunately, I'm having trouble getting any of the above to work from a desktop entry file (.desktop).

0

AFAIK, The terminal won't be held open unless you use the "hold" option for your respective terminal emulator.

For example, for the xfce4-terminal, in order to make a launcher (menu item) that executes some command in a terminal window that is to be held open even after the command finishes its execution, one must use the following command for the launcher/menu item:

xfce4-terminal -H -e 'put command to execute here'

(-H is the hold flag that is required to prevent the terminal window from closing after command execution.

-e flag makes the terminal execute the command in the string following -e when the terminal starts up )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.