Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
552 views
in Technique[技术] by (71.8m points)

open terminal run command python

I am trying to open a terminal and run a command in it. I am using

os.system("gnome-terminal -e 'bash -c "exec bash; MY_COMMAND; exec bash" '")

This opens up a new terminal, but the command is not executed.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The exec command replaces the currently running process with a new one, so if you have an exec in a list of commands to run, as soon as exec is run, nothing else will run. So you're replacing 'bash -c "exec bash; MY_COMMAND; exec bash" ' with bash, and then nothing after the exec bash is running. Try this instead:

os.system("gnome-terminal -e 'bash -c "MY_COMMAND" '")

or if you need a terminal to stay open, try this:

os.system("gnome-terminal -e 'bash -c "MY_COMMAND; sleep 1000000" '")

of if you want the terminal to stay open and be in a bash shell, try this:

os.system("gnome-terminal -e 'bash -c "MY_COMMAND; bash" '")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...