16

I have a script which runs in the background (without terminal windows or TTY) and occasionally does something.

I now want it to do another thing when it does something and it is to open a Gnome Terminal window and execute 2 commands. Actually, I only need to execute 1 command but I want to make the terminal window stay open so I can see the output of the command. The command prints both on stdout and stderr and its output changes over time, so just writing it to a file and sending some kind of notification wouldn't do the job very well.

I can get Gnome Terminal to open a window and execute 1 command:

gnome-terminal -e "sleep 10"

I chose sleep as the long-running command for simplicity. However, when adding another command, no terminal window opens:

gnome-terminal -e "echo test; sleep 10"

What's the solution to this?

21

gnome-terminal treats everything in quotes as one command, so in order to run many of them consecutively you need to start interpreter (usually a shell), and do stuff inside it, for instance:

gnome-terminal -e 'sh -c "echo test; sleep 10"'

BTW, you may want the window to stay open even after commands finish their job, in such case just start new shell, or replace a current with the new one:

gnome-terminal -e 'sh -c "echo test; sleep 10; exec bash"'
| improve this answer | |
  • 3
    # Option “-e” is deprecated and might be removed in a later version of gnome-terminal.# # Use “-- ” to terminate the options and put the command line to execute after it.# That error happens now. But if you replace -e with --, the new terminal that comes up gives an error. How to correctly use --? – eric.frederich Mar 12 '19 at 13:09
  • 7
    @eric.frederich based on this answer to a question at stackoverflow, I believe the syntax should be gnome-terminal -- /bin/sh -c 'echo test; sleep 10' – wxyz Mar 26 '19 at 6:19
7

As of January 2020, the -e option in gnome-terminal still works, but throws the warning

# Option “-e” is deprecated and might be removed in a later version of gnome-terminal.

# Use “-- ” to terminate the options and put the command line to execute after it.

You can run the two commands without warning with

gnome-terminal -- /bin/sh -c 'echo test; sleep 10'

And, as mentioned in this answer, if you want the window to stay open afterwards, you can do

gnome-terminal -- /bin/sh -c 'echo test; sleep 10; exec bash'

*answer to a similar question

| improve this answer | |

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.