Creating konsole instance with subprocess does not release process

77 Views Asked by At

I am attempting to open a terminal instance using subprocess, but I can't find how to add the & parameter for the bash command

In bash, the following command works as required

konsole --workdir /home/jeff/projects &

but when I run the following command the system opens the new Konsole window, but the window that I use to run the command is still busy - the process is not released

konsole --workdir /home/jeff/projects

In my python program I have the following code

import subprocess
subprocess.run(['konsole',
                '--workdir',
                '',
                '/home/jeff/projects',
                ])
print('finished')

When I run the code, the program hangs and the never reached the print('finished') command

How do I get the & into python subprocess command?

If I run the analogous command for Gnome terminal I do not need the & on the command line

gnome-terminal --working-directory=/home/jeff/projects

and the python code works and releases the calling terminal

subprocess.run(['gnome-terminal',
                '--working-directory=/home/jeff/projects',
                ])
print('finished')
1

There are 1 best solutions below

0
On

You can achieve what you want by using Popen:

#!/usr/bin/env python3
from subprocess import Popen
import time

p = Popen(['konsole',
           '--workdir',                                                                                                                                                                                                                                
           '/home/jeff/projects'
           ])
print('finished starting konsole')
time.sleep(30) # ... do other stuff while konsole is running
print('terminating konsole')
p.terminate()  # terminate konsole if needed