I'm actually running a subprocess from a python program using Popen
proc= subprocess.Popen("xterm -e python script.py", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
And when this process is running on xterm, we can kill it using Ctrl+C, is there a way to send other signals using (Ctrl+Z and fg,...) to resume and continue the job?
Another solution would be running this process in another terminal, without using xterm -e is this possible?
Any other solution would be helpful.
Thanks
You could do it programatically in psutil
import psutil
p = psutil.Pocesss(proc.pid)
p.suspend()
p.resume()
With Python2.6+
# send ctrl+c
popen.send_signal(signal.SIGINT)
Related
I am trying to launch a python subprocess from excel using PYXLL, but it seems to have trouble launching the cmd window and running commands.
Below is a sample of what I am trying to run:
#xl_macro()
def test():
if 1 == 1:
xlcAlert("Next line nothing happens") #Popup appears
p = subprocess.Popen(r'start cmd /k', shell=True, creationflags=subprocess.CREATE_NEW_CONSOLE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
xlcAlert("{}".format(p.pid)) #p was never launched
I am trying to capture values from excel and pass them in a subprocess. This works when executing in my IDE: data is read from excel and then subprocess launches window. However, once adding the decorator to have it run as macro in EXCEL, the script will just stop once subprocess.Popen line is reached. Is there any way to launch a subprocess from pyxll?
After investigation, and thanks to Charles Duffy, Microsoft Office SandBoxing kills the shell subprocess. This has been implemented for security reasons in latest versions.
The simple solution is to run subprocess with shell=False and pass the args in a list:
p1 = subprocess.Popen(cmdlist, shell=False)
The Sandboxing will not terminate the process - python window will open while script is running.
The code below to kill a process opened using subprocess.Popen() works on Linux.
import os
import signal
import subprocess
# The os.setsid() is passed in the argument preexec_fn so
# it's run after the fork() and before exec() to run the shell.
pro = subprocess.Popen(cmd, stdout=subprocess.PIPE,
shell=True, preexec_fn=os.setsid)
os.killpg(os.getpgid(pro.pid), signal.SIGTERM) # Send the signal to all the process groups
Unfortunately, it does not work on Windows. The process refused to be killed. How to modify the code such that the process can be killed on Windows?
I am using Windows 10, Python 3.8 on Anaconda.
USE subprocess.Popen.terminate() TO TERMINATE A SUBPROCESS
In Python, external processes can be started easily using the subprocess module. For instance on Windows:
command = 'external_app'
my_process = subprocess.Popen(
command,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP,
shell=True,
stderr=subprocess.STDOUT,
stdout=subprocess.PIPE,
universal_newlines=True)
To kill the process, we can run:
os.kill(my_process.pid, signal.CTRL_BREAK_EVENT)
This works fine using the command-line interpreter of Python (python.exe). But if I like to start and stop processes from within a graphical Python application without a command-line window using pythonw.exe the problem occurs that I can’t stop the process with os.kill anymore.
How can I kill an external process on Windows with pythonw.exe?
I want to control running process/program by script in Python.
I have a program `linphonec´ (You can install: apt-get install linphonec).
My task is:
Run linphonec (I'm using subprocess at the moment)
When linphonec is running it has many commands to control this and I want to e.g use proxy list (command in linphonec).
Simple flow:
test#ubuntu$ > linphonec
linphonec > proxy list
How can I do this?
There are actually 2 ways to communicate:
Run your program with myprogram.py | linphonec to pass everything you print to linphonec
Use subprocess.Popen with subprocess.PIPE in constructor via keywrod-args for stdin (propably stdout and stderr, too) and then communicate for a single command or use stdin and stdout (stderr) as files
import subprocess
p=subprocess.Popen("linphonec",
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True) #this is for text communication
p.stdin.write("proxy list\n")
result_first_line=p.stdout.readline()
I'm trying to use python psutil library to create and kill process. My script starts an process, and then tries to kill started subprocess. I run same code under Windows and Linux. Under Windows everything works well. Under Linux psutils starts subprocess correctly(so started app is script's child and it executed with same privileges as the script, but when I try to kill the process psutil detaches from the process but not kills. Here is starting app code:
self.__proc = psutil.Popen(cmd, cwd=working_directory, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
After this I try to kill started child process:
self.__proc.kill()
self.__proc = None
I got same behavior using this:
while psutil.pid_exists(pid):
p = psutil.Process(pid)
if p is not None:
p.kill()
Can anyone explain why I can't kill process started by me? What I'm doing wrong?
I'm using Python 2.7.