16

I'm developing a program that has a button. When pressed, I want to open a terminal that runs:

sudo apt-get update

I'm using:

os.system("gnome-terminal -e 'sudo apt-get update'")

This works fine. The only problem is that when the update is finished, the terminal closes. What can I do to leave the terminal open?

Gringo Suave
  • 27,899
  • 6
  • 81
  • 73
mika29
  • 161
  • 1
  • 1
  • 5
  • See this post: http://stackoverflow.com/questions/3512055/avoid-gnome-terminal-close-after-script-execution – rodion Sep 27 '11 at 19:54

4 Answers4

18

You could do this:

os.system("gnome-terminal -e 'bash -c \"sudo apt-get update; exec bash\"'")
jterrace
  • 61,216
  • 21
  • 150
  • 193
  • 1
    And probably is the same with konsole (if I use KDE)? – mika29 Sep 28 '11 at 09:09
  • os.system("gnome-terminal -e 'sudo apt-get update'"). Not sure why you need the extra 'bash' when this works just fine. – mastash3ff Dec 17 '15 at 16:28
  • 1
    @mastash3ff - read the question. "when the update is finished, the terminal closes". The extra bash call makes it stay open. – jterrace Dec 18 '15 at 01:59
  • is it possible to do this with your termial of choice? For example, I am using alacritty, but making the substitution "gnome-terminal"="alacritty" does not work. – Andres Mejia Mar 14 '20 at 20:51
4

There are a few choices:

  • add ; read -p "Hit ENTER to exit" to the end of the command line.
  • add ; sleep 10 to the end of the command line to wait a bit, then exit.
  • Configure gnome terminal:

    Go to the "Edit" menu and click "Current Profile". Click on the "Title and Command" tab. In there, there is a setting called "When command exits". Change it to "hold the terminal open". You could also create a new profile.

Gringo Suave
  • 27,899
  • 6
  • 81
  • 73
0

You can remove the -e:

os.system("gnome-terminal 'sudo apt-get update'")
tripleee
  • 158,107
  • 27
  • 234
  • 292
Pablo Alejandro
  • 491
  • 1
  • 8
  • 19
-1

import os

os.system("Your command") You can also pass custom command as custom variable For example:

cmd_to_run = "ls -lat"

os.system(cmd_to_run)