How to keep the terminal open after executing a C program?

2

I'm trying to run a C program from within a python code.

cmd = 'gnome-terminal --command=./myprog'
subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)

The code executes and the terminal closes.

I want to keep the terminal open after the execution is completed without having to add any extra code in the C file e.g. prompt the user for some input.

Is there a workaround for this?

Pallab Pain

Posted 2015-04-26T22:58:25.620

Reputation: 72

Answers

4

To keep the terminal opened until a key is pressed:

gnome-terminal -- bash -c "ls && read"

To keep the terminal opened until exit:

gnome-terminal -- bash -c "ls && bash"

Replace ls with the command you want to execute (your compiled executable file).

Note

If -- does not work, try the old (deprecated) -x approach instead:

gnome-terminal -x bash [...]

Peque

Posted 2015-04-26T22:58:25.620

Reputation: 721

Of course, but I don't seem to have enough reputation points to upvote an answer as of now. I'll definitely upvote once I meet the criteria. :) – Pallab Pain – 2015-04-28T17:06:38.297

@pallab.pain: hahaha. Ok, don't worry then, its just that many times there's people registering just to make one question and then they leave it opened (without an accepted answer) forever. You can't accept the answer either with low reputation? (there should be a green tick bellow the upvote/downvote buttons) That way people will know the problem was solved and will be able to spend their time on other non-answered questions. ;-) – Peque – 2015-04-29T07:33:42.187

Doesn't seem to be an option -c for gnome-terminal – CodyBugstein – 2017-01-22T05:27:49.640

@CodyBugstein: -c is for the bash command. ;-) – Peque – 2017-01-22T12:01:12.230

@Peque how can I stay in same terminal(newly opened) and run other commands – mtkilic – 2018-09-05T20:13:05.087

@mtkilic Try gnome-terminal -- bash -c "ls && bash". – Peque – 2018-09-06T07:37:21.460

@Peque Looks like this worked for me subprocess.Popen(['gnome-terminal', '--', 'bash', '-c', 'ls && whoami && cd /tmp && bash'], env=os.environ) but I dont like how I have pass all my commands in one line of code. – mtkilic – 2018-09-06T13:58:32.953

this works, but I can't seem to get back to a command line. What I would want is to be able to get back to the command line with ctrl-c, and if I wanted to re-run the command I would use bash history / up arrow. – MrCholo – 2018-12-15T01:34:30.737

0

Since you start it with Python try this:

import time
#Your Code here
time.sleep(5) # delay for 5 seconds 

Coretool

Posted 2015-04-26T22:58:25.620

Reputation: 113

0

I can't be sure 100% since I'm on a Mac without a Gnome setup, but this should work: add ; read to your command to be executed, so after executing the command you need to press Enter before closing the window:

cmd = 'gnome-terminal --command="./myprog ; read"'
subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)

I tested this with xterm instead of gnome-terminal, it works there.

Teun Vink

Posted 2015-04-26T22:58:25.620

Reputation: 2 107

Yeah, it didn't work with gnome-terminal. – Pallab Pain – 2015-04-28T03:37:17.377