How to run python script with depentdent shell command in the background - python

Is it possible to run shell command at background in python script? Let say I have run.py and in this script I need to do,
#run shell command(in python script until script is finished)
#continue with script
But the problem is that they are dependent. To run my script I need to run shell command in the same script.

Yes, you can use the subprocess library in Python and check the status of the output.
Ex: subprocess.check_call("exit 1", shell=True)

Related

Open terminal, run python script and keep it open for results?

How to get an sh script for starting a new terminal, execute a python script and keep it running? The python script is supposed to run continuously in a perpetual loop, spitting out results as they pop in. Whenever trying with sh-script for gnome-terminal just getting: child process exited normally with status 2
Manually it would just be: python home/ubuntu/pyscript.py
Could someone give an idea how to do this?
I have a list of scripts to run, so resorting to the manual solution is tedious.
You can use gnome-terminal with the -x flag.
Suppose you have a spam.py script; then the following command will spawn a new terminal, run spam.py in it, and close the terminal once the script has ended.
gnome-terminal -x python spam.py
Try with this script:
# spam.py
import time
for _ in range(5):
print("eggs")
time.sleep(1)
Then the previous command will spawn a terminal, that will be printed eggs five times, and then will be closed.
If you want to leave the terminal open with the Python interpret still running after the script ended, then Python's -i flag (doc then CTRL+F -> -i) is what you want:
gnome-terminal -x python -i spam.py
To run the Python script in a new instance of your favourite terminal, write:
x-terminal-emulator -e python -i home/ubuntu/pyscript.py
This will start the Python script and run it until it ends, then display a Python prompt to stop the terminal emulator from closing.
This will work with x-terminal-emulator substituted with any of the many, many terminals installed on my computer, so will work with little modification across all POSIX-compatible systems with the standard terminals installed. This won't work on a Mac, however. For a properly cross-platform Python implementation of something slightly different, see here. Most of the techniques should be transferable.
To run the Python script in the same terminal whilst carrying on with the rest of the shell script, write:
python home/ubuntu/pyscript.py &
Note the &, which runs the program as a new process (but still connects the output to the virtual terminal).

Launch Python script in new terminal

I want to launch a python script in a new macOS terminal window from another script. I'm currently using this code:
subprocess.call(['open', '-a', 'Terminal.app', '/usr/bin/python'])
which launches a python prompt in a new terminal window.
But when I try to run a python script with this code:
subprocess.call(['open', '-a', 'Terminal.app', '/usr/bin/python', 'test.py'])
it completely ignores the 'test.py' on the end and starts a python prompt, just like it does without the test.py on the end.
How can I make this work?
Don't use the open -a terminal.app, just use the python executable
subprocess.call(['/usr/bin/python', 'test.py'])
This Python code will open a new terminal window, and then have python3 run test.py:
import os
os.system("""osascript -e 'tell application "Terminal" to do script "python3 test.py"'""")
I'm unable to come up with a way to get this into a subprocess.call() call.

How to spawn detached background process on Linux in either bash or python

I have a long running python script on Linux, and in some situations it needs to execute a command to stop and restart itself. So, I would like to have an external script (either in bash or python) that executes command to restart the original script. Let me elaborate.
Suppose I have original_script.py. In original_script.py I have this in an infinite loop:
if some_error_condition:
somehow call external script external.sh or external.py
Let's suppose I can call external.sh and it contains this:
#!/bin/bash
command_to_restart_original_script
Finally, I know the command "command_to_restart_original_script". That isn't the problem. What need is the python command to "somehow call external script external.sh". I need the external script (which is a child process) to keep running as the parent process original_script.py is restarting, ie I need the child process to be detached/daemonized. How do I do this?
I found lots of suggestions in various places, but the only answer that worked for me was this:
How to launch and run external script in background?
import subprocess
subprocess.Popen(["nohup", "python", "test.py"])
In my case I ran a script called longrun.sh so the actual command is:
import subprocess
subprocess.Popen(["nohup", "/bin/bash", "longrun.sh"])
I tested this using this run.py:
import subprocess
subprocess.Popen(["nohup", "/bin/bash", "longrun.sh"])
print "Done!"
and I verified (using ps -ax | grep longrun) that longrun.sh does indeed run in the background long after run.py exits.

Python: Executing the windows command in windows command prompt from python script

I have to execute python script in windows command prompt
I am using the following command to run the command, so that the script opens the command prompt execute it
os.system("start /wait cmd /c {c:\\python27\\python.exe C:\\examples\\xml2html.py --dir c:\\Temp\\abcd c:\\tmp\\results.xml}")
I will be expecting a new directory called "abcd" created at that location and some output files created inside that.
When I run this command normally in the windows command prompt it works. I am not able to execute this in the script. Windows command prompt opens and terminates quickly.
Could any one let me know where exactly is it going wrong with the command please?
Unless you want to open a new console window you don't need to run cmd.exe (%COMSPEC%) in order to run another Python script as a subprocess:
import sys
from subprocess import check_call
check_call([sys.executable, "C:\\examples\\xml2html.py",
"--dir", "c:\\Temp\\abcd", "c:\\tmp\\results.xml"])

How to run a python script inside Applescript?

I had to do an Applescript and by the end of it i just want to run a python script.
I wrote
do shell script "/Users/Tom/Desktop/ayscript.py"
But it's said "permission denied"
Any idea ?
You are trying to run the script directly instead of using Python to run it. You need to do shell script python, passing the path to your script as an argument.
do shell script "python /Users/Tom/Desktop/ayscript.py"

Categories