24

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?

23

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
  • 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 '10 at 13:22
  • 1
    xterm -hold -e mycommand – Roger Pate Aug 31 '10 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, 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..." – Jukka Matilainen Aug 31 '10 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"
3

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

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.