For example, I opened power-shell manually in windows.Now I know process id of it. So I want to run commands on the process using python.
Open power-shell manually.
Find its process id.
Execute commands using its process id.
Please mention code to do it **
You can do all that with the subprocess python standard library https://docs.python.org/2/library/subprocess.html
Related
I'm using Python's subprocess to spawn new processes. The processes are independent of each other and output some of the data related to the account creation.
for token in userToken:
p = subprocess.Popen(['python3','create_account.py',token)
sleep(1)
I'm trying to find a way to get the output of each of the Python scripts to run in the separate VSCode terminals to clearly see how the processes are running.
For example, in VSCode you can split the terminals as in the screenshot below. It would be great if each of the processes would have its own terminal window.
I've also checked that you can run tasks in VSCode in separate terminals as described here. Is there a way to launch multiple subprocess threads in separate terminals like that?
If that's not possible, is there another way I can run subprocess in multiple terminals in VSCode?
Currently in VS Code, it supports running python code in a single thread in the terminal by default.
If you want to run the python code in two or more VS Code terminals separately, and not run them sequentially, you could manually enter the run command in the two VS Code terminals, for example:
The command to run the python file'c.py': "..:/.../python.exe ..:/.../c.py".
And for multi-threaded synchronous operation, except for the manual input of execution commands in two or more newly created terminals to make the code run synchronously, VSCode currently does not have other local support that supports this function.
I have submitted an application for this feature in Github and we are looking forward to the realization of this feature:
Github link: Can VSCode automatically run python scripts in two or more terminals at the same time?
I am trying to write a script to open a third-party console application and run commands on Windows. The third-party console requires the two commands; 'connect' and 'run'. A typical input/output is shown below. This connects to the host server then runs a process indexed by three parameters (p1,p2,p3).
>connect server
Successfully connected to service.
>run p1 p2 p3
Successfully started.
FINISHED
The app does not allow me to execute both commands in one line using & as with cmd.
Despite reading the subprocess documentation I can't figure out how to pass my two commands into the executable.
I am using Python 3.5, so I believe subprocess.run should be suitable for this task. The snippet below simply opens the third-party console. I have tried other code, linked at the bottom of the post, but I am unsure how to implement it for my purpose.
import subprocess
exe = r'C:\...\third_party_app.exe'
subprocess.run(exe)
Below are some of the SO resources that could be helpful that I have tried, and failed, to interpret.
https://stackoverflow.com/tags/subprocess/info
Python - How do I pass a string into subprocess.Popen (using the stdin argument)?
I am currently writing a program to help me with some database administration. It's supposed to execute commands in MariaDB. I thought since this is a CLI application I can just do that with os.system, but I'm having problems doing so. So lets say I have the following code
import os
os.system('mysql --user=%s --password=%s' %(user, password)
os.system('USE database;')
This code logs me into MariaDB, but it does not execute the second command to choose the database. Is this possible with os.system, and if not, what are my alternatives? Thanks.
Edit: I tried using subprocess instead, which gives me another problem: It immediately exits MariaDB after logging in.
I recomand you to use Subprocess instead of os python module, using Popen
try to check this link out :
Calling an external command in Python
I am writing a Python script that creates and runs several VMs via virsh for the user. Some of the configuration has to be done by executing commands inside the VM which I would want to do automatically.
What would be the easiest way to get remote shell access in Python? I am considering the following approaches:
To use the virsh console command as a sub-process and do I/O to it.
To bring up an SSH session to the VM. I can configure the VM before it boots by editing its file system so I know its target IP address.
Any better API for doing this. RPC?
I need to get the return values for commands so I know if they executed correctly or not. For that matter I need to be able to detect when a program I invoke has finished. Options #1 and #2 rely on scraping the output and that gets complex.
Any suggestions much appreciated.
Suppose that I have an exe running and its name is myexe.exe. On Windows, I can see the process for myexe.exe in Process Explorer. Using Python, how can I obtain the process ID of myexe.exe and shut it down?
You can do it via wmi service, if you use the wmi (Windows Management Instrumentation module). Look here for the examples to get the process name and to terminate using the process id.
The best way to do it on windows is to use PyWin32 ( http://sourceforge.net/projects/pywin32 )
Look at the following code sample: http://coding.derkeiler.com/Archive/Python/comp.lang.python/2007-10/msg00717.html
It might be exactly what you need.