How to start external program from Python script in the foreground? - python

import os
os.system("notepad macros.txt")
or
from subprocess import Popen
Popen(["notepad", "macros.txt"])
both start notepad in the background. How to start it in the foreground?

You can try to use the start command, maybe the /MAX option will force notepad to be in foreground, otherwise if you can wait untill notepad shutdown, use the /WAIT option/
Popen(["start", "/MAX", "notepad", "macros.txt"], shell=True)

Related

Python freezes running exe file

I'm doing a simple python gui and on button click it will run a simple command:
os.system("C:/cygwin64/bin/bash.exe")
When I look in the console it ran correctly and but my guy freezes and is not responding.
If I run the the command in the console without python it works perfectly and I start cygwin terminal.
If you know what is cygwin is there a better way to start it in the same terminal?
os.system blocks the current thread, you can use os.popen in order to do that in another thread, and it also gives you few methods to detach/read/write etc' that process.
for example,
import os
a = os.popen("python -c 'while True: print(1)'")
will create a new process that will be terminated as soon as you terminate your script.
you can do
for i in a:
print(i)
for example, and it will block the thread as os.system does.
you can a.detach() it whenever you want to terminate the process.
However, os.system
import os
os.system("python -c 'while True: print(1)'")
it will output the 1s forever until you terminate the script.
You can use function Popen in package subprocess. It has many possible arguments that allow you to pipe input to and/or pipe output from the program you are running. But if you just want to execute bash.exe while allowing your original Python program to continue running and eventually wait for the completion of bash.exe, then:
import subprocess
# pass a list of command-line arguments:
p = subprocess.Popen(["C:/cygwin64/bin/bash.exe"])
... # continue executing
# wait for the subprocess (bash.exe) to end:
exit_code = p.wait()

Close the terminal window after doing something

I want to just print some information and call an application e.g. notepad.
from subprocess import call
print("Opening Notepad++")
call([r"C:\Program Files (x86)\Notepad++\notepad++.exe"])
exit()
Problem now is that the terminal window doesn't automatically close. It stays open until I close the notepad window. How can I make the terminal window disappear automatically.
use Popen like so
import subprocess
subprocess.Popen(r'C:\Program Files (x86)\Notepad++\notepad++.exe', \
stdout=subprocess.PIPE, shell=False, creationflags = 0x08000000)
You need to call the notepad command with start COMMAND, like in Linux we use COMMAND & to fork the process into the background. in windows we use the start COMMAND
So you code refactored:
from subprocess import call
print("Opening Notepad++")
call([r"start C:\Program Files (x86)\Notepad++\notepad++.exe"])
exit()
Although note I don't have a windows machine to test on.
You could use pythonw.exe:
pythonw script.py
Or change its extension to pyw e.g. script.pyw and double click on it.
If you do that you should print "Opening Notepad++" to a popup window. See: Python Notification Popup that disappears

How to stop airodump-ng with the Python subprocess module

I wrote a program (I ran it in the terminal) that goes through a list of terminal commands (Kali).
import subprocess as sub
import time
sub.call(['airmon-ng', 'start', 'wlan0'])
p = sub.call(['airodump-ng','wlan0mon'])
time.sleep(10)
p.kill()
The last commmand is airodump-ng wlan0mon. Everything works fine (everything is displayed in the terminal (beacons, ESSID, etc.)).
After a specified time I wish to kill the process (airodump-ng wlan0mon).
I dont want to press ctrl-c by hand!
p.kill() does not work (maybe improper use)
How can I do this? What command to send through the subprocess module?
subprocess.call() waits for the subprocess to finish before your Python program executes the next statement. You'll want to use subprocess.Popen() to initialize a separate ("background") process and then continue your Python program.
sub.call(['airmon-ng', 'start', 'wlan0'])
p = sub.Popen(['airodump-ng','wlan0mon'])
time.sleep(10)
p.kill()
You could use subprocess to run "killall airodump-ng".
However, if you do this, you may as well convert the whole thing to a bash script.

subprocess window disappear quickly

In python, I use subprocess.Popen() to launch several processes, I want to debug those processes, but the windows of those processes disappeared quickly and I got no chance to see the error message. I would like to know whether there is any way I can stop the window from disappearing or write the contents in the windows to a file so that I can see the error message later.
Thanks in advance!
you can use the stdout and stderr arguments to write the outputs in a file.
example:
with open("log.txt", 'a') as log:
proc = subprocess.Popen(['cmd', 'args'], stdout=log, stderr=log)
In windows, the common way of keeping cmd windows opened after the end of a console process is to use cmd /k
Example : in a cmd window, typing start cmd /k echo foo
opens a new window (per start)
displays the output foo
leave the command window opened

how to create and destroy non blocking sub process that are OS independent in python?

In a python script i want to spawn a process that runs a file in the same directory
I dont want the python script to be blocked by the new process
Then want to be able to close the spawned process from the script.
Ontop of it all i need it to be OS independant.
What is the best of doing this?
As #Keith suggested use subprocess module, but more specifically use Popen. For example, on Windows, this opens myfile.txt with notepad and then terminates it after a 20 seconds:
import subprocess
import time
command = "notepad myfile.txt"
pipe = subprocess.Popen(command, shell=False)
time.sleep(5)
pipe.poll()
print("%s" % pipe.returncode) #"None" when working fine
time.sleep(5)
pipe.terminate()
pipe.wait()
print("%s" % pipe.returncode) # 1 after termination

Categories