1

I have a python script that needs to be run in an interactive shell (it monitors for certain cancel key input events, which i dont need). I want to execute this script in the background, grab its output, then kill it after 5 seconds.

gnome-terminal -- "./script.sh > log.log"

or gnome-terminal -x bash -c "./script.sh > log.log"

doesnt work, because it's not an interactive shell...

I am using a standard ubuntu 1804 setup. How can i execute this script in an input-enabled terminal?

1 Answer 1

3

I believe using the subprocess library will help in what you want to do.

Here's an example (according to the question):


import subprocess
import shlex

command = shlex.split("<your command to run the script>")
process = subprocess.Popen(command)

try:
    outs, errs = process.communicate(timeout=5)

except TimeoutExpired:
    process.kill()
    outs, errs = process.communicate()

# Now you can write the output into a file, like so:

with open("path_to_log_file", "w") as log_file:
    f.write(outs)

Here's the docs for the subproecss library: https://docs.python.org/3/library/subprocess.html

I hope this answer was helpful :)

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.