You are not logged in.

#1 2019-03-03 17:56:59

Maniaxx
Member
From: .de
Registered: 2014-05-14
Posts: 261

[Solved]How to prevent a terminal to close on error

If i execute this the terminal remains open after 2 seconds:

gnome-terminal -- bash -c "echo test;sleep 2;exec bash"

But if i break the process within the 2 seconds with ctrl-c (that should be the same as an error) the window closes. I've tried 'set +e' so far but it doesn't help.
I have many bash scripts linked on the desktop (like pacman -Syu and so on) that i start with a double click. But whenever there is an error the window disappears that's kinda annoying.

Any idea how to keep the window open?

Last edited by Maniaxx (2019-03-06 22:34:28)


sys2064

Offline

#2 2019-03-03 19:47:06

eschwartz
Trusted User/Bug Wrangler
Registered: 2014-08-08
Posts: 2,853

Re: [Solved]How to prevent a terminal to close on error

set +e won't help, because bash -c 'some commands' is fundamentally a thing which runs a sequence of commands and then exits. Using exec bash is a workaround because bash *without* -c, will spawn an interactive terminal, but as you have observed, if the command line is killed by CTRL+C *before* it gets to the exec bash, then you never spawn an interactive terminal at all.

However, CTRL+C is *special* as it terminates the bash -c process in much the same way set -e does.
And, set +e does not have any effect on CTRL+C, because it's not a command exiting with a nonzero return code.

One way  you could do this is by using bash --rcfile.

Create a bash script with the following contents (let's assume you save it as ~/gnome-terminal-scripts/test-rcfile):

source ~/.bashrc
echo test;sleep 2;

Then invoke gnome-terminal as follows:

gnome-terminal -- bash --rcfile ~/gnome-terminal-scripts/test-rcfile

You should see it open a new terminal which echoes "test", waits a few seconds, then displays a new prompt. Use CTRL+C and it will kill the sleep, but still return you to the interactive prompt.

...

Other alternatives include using a terminal emulator like guake, which allows you to control tabs in a running instance. So:

guake -n -g -e "echo test; sleep 2"

would spawn a new tab in guake, select that tab, and run a specified command in it (but not exit when it is done).


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#3 2019-03-06 22:34:14

Maniaxx
Member
From: .de
Registered: 2014-05-14
Posts: 261

Re: [Solved]How to prevent a terminal to close on error

eschwartz wrote:

gnome-terminal -- bash --rcfile ~/gnome-terminal-scripts/test-rcfile

You should see it open a new terminal which echoes "test", waits a few seconds, then displays a new prompt. Use CTRL+C and it will kill the sleep, but still return you to the interactive prompt.

If i get it right... technically it doesn't "return to the interactive prompt" but rather continues in bash "invocation" chain (profile->)rcfile->prompt. And we hook into rcfile here.
Nice trick indeed. Thanks!


sys2064

Offline

Board footer

Powered by FluxBB