linuxbashshellssh

Avoid gnome-terminal close after script execution?


I created a bash script that opens several gnome-terminals, connect to classroom computers via ssh and run a script.

How can I avoid that the gnome-terminal closes after the script is finished? Note that I also want to be able to enter further commands in the terminal.

Here is an example of my code:

gnome-terminal -e "ssh root@<ip> cd /tmp && ls"

Solution

  • Let gnome-terminal run bash and tell bash to run your commands and then start a new bash:

    $ gnome-terminal -- bash -c "echo foo; echo bar; exec bash"
    

    explanation:

    gnome terminal runs bash ...

    $ gnome-terminal -- bash -c "echo foo; echo bar; exec bash"
                        ^^^^
    

    which runs your commands ...

    $ gnome-terminal -- bash -c "echo foo; echo bar; exec bash"
                                 ^^^^^^^^  ^^^^^^^^
    

    and then reexecutes bash.

    $ gnome-terminal -- bash -c "echo foo; echo bar; exec bash"
                                                     ^^^^^^^^^
    

    gnome terminal will not close if something is still running. in this case the second bash is still running. this makes gnome terminal not close and you can interact with bash inside gnome terminal as normal.

    if the commands are many or complex you can put them in a script:

    $ gnome-terminal -- bash -c "./scripttorun; exec bash"
    
                                 ^^^^^^^^^^^^^
    

    advantage: you have the script ready to be run manualy outside of this gnome-terminal construct.


    alternative:

    you can also reexecute bash in the script directly

    Prepare scripttobash:

    #!/bin/sh
    echo foo
    echo bar
    exec bash
    

    Then run:

    $ gnome-terminal -- ./scripttobash
    

    the advantage is the gnome terminal command became quite simple.

    the disadvantage is that the script now always runs a second bash. which means you cannot run the script independently. well actually you can but the second bash might cause confusion.


    alternative:

    use --rcfile to run a custom startup configuration containing your commands.

    example somercfile:

    source ~/.bashrc
    echo foo
    echo bar
    

    Then run:

    $ gnome-terminal -- bash --rcfile somercfile
    

    bash will stay open afterwards. but i am not entirely sure about other side effects this might have.


    for completeness:

    there is an option to keep gnome terminal open after executing the command. but you will not be able to interact anymore. just read the output.

    1. go to preferences (hamburger button -> preferences)
    2. go to profiles (i recommend to create a new profile for this case)
    3. go to command tab
    4. set "when command exits" to "hold the terminal open"

    if you created a new profile you can use it like this:

    gnome-terminal --profile=holdopen -- ./scripttorun
    

    closing words:

    Every method has it's quirks. You must choose, but choose wisely.

    I like the first solution. it does not need extra files or profiles. and the command says what it does: run commands then run bash again.

    All that said, since you used ssh in your example, you might want to take a look at pssh (parallel ssh). here an article: https://www.cyberciti.biz/cloud-computing/how-to-use-pssh-parallel-ssh-program-on-linux-unix/