22

How do you prevent gnome-terminal from exiting after its given command has exited?

I'm calling gnome-terminal from a cronjob, in order to create a terminal accessible to the user. The terminal is initially given a specific program to run. e.g.

gnome-terminal --tab -e "/usr/bin/myprog"

This works fine, except that when "myprog" exits, so does the gnome-terminal. How do I keep it running, but just drop back to a terminal prompt?

Cerin
  • 50,711
  • 81
  • 269
  • 459
  • 6
    Another discussion of the same issue: http://stackoverflow.com/questions/3512055/avoid-gnome-terminal-close-after-script-execution – Aaron Altman Dec 16 '10 at 22:39
  • 1
    @Ignacio, it's a common term. Google shows over 100k results for it... – Cerin Dec 28 '10 at 21:29
  • @Ignacio, the terms "shell" and "terminal" are generally synonymous. If your vocabulary or technical environment differs, fine. Sorry for the confusion. – Cerin Dec 30 '10 at 19:32

4 Answers4

28

Try this:

gnome-terminal --tab -e "/bin/bash -c '/usr/bin/myprog; exec /bin/bash -i'"
Dennis Williamson
  • 303,596
  • 86
  • 357
  • 418
  • Very simplistic. Thanks. – Cerin Dec 17 '10 at 14:30
  • 1
    As of January 2020, the -e option in gnome-terminal still runs properly but throws out the following 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. >> Based on that information, I confirmed that you can run the following command without receiving any warning messages: <> – Member2017 Jan 31 '20 at 07:38
  • 1
    Just what I need. Only the command today is a little bit changed. Just to update: gnome-terminal --tab -- /bin/bash -c '/usr/bin/nasm; exec /bin/bash -i' – Agguro Apr 04 '20 at 15:36
21

alt text

Create a profile (i.e. hold), set "When command exits: Hold the terminal open" and then

$ gnome-terminal --tab --profile hold -e /usr/bin/myprog
Diego Torres Milano
  • 57,580
  • 7
  • 101
  • 124
  • There should be some way to create a profile with `hold the terminal open` option set via command-line only. – Omar Tariq Dec 03 '16 at 12:20
  • Depending on your gnome-terminal, you can use gconf or gsettings to set or get tha value of that property: `gconftool-2 -g /apps/gnome-terminal/profiles/Profile0/exit_action` – Diego Torres Milano Dec 05 '16 at 16:58
  • 2
    For me this keeps the terminal window open, but it doesn't accept any more commands. – jcanizales Feb 13 '19 at 22:09
3

Create a shell script like this:

#!/bin/bash
# myprog-wrapper.sh - runs /usr/bin/myprog and then starts a new bash session
/usr/bin/myprog
/bin/bash

Give the shell script execute permission.

Then set up your cron job to call this script instead of directly calling myprog:

gnome-terminal --tab -e "/path/to/myprog-wrapper.sh"

Replace /bin/bash with your shell of choice.

mob
  • 110,546
  • 17
  • 138
  • 265
3

You could use xterm or rxvt-unicode instead of gnome-terminal, both of which have the -hold option for this purpose.

ak2
  • 6,262
  • 27
  • 23