Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to write a python code to run the job of the following Shell script for me:
for run in {1..100}
do
/home/Example.R
done
The shell script is basically running an R script for 100 times. Since I am new to python can somebody help me to write this code in python?
You could use subprocess.call to make an external command:
from subprocess import call
for i in xrange(100):
call(["/home/Example.R"])
You can use python's commands module to execute external commands and capture their output.
The module has a function commands.getstatusoutput(cmd), where cmd is the command you are looking to run, as a string.
Something like this could do the trick:
import commands
for x in xrange(100):
commands.getstatusoutput("/home/Example.R")
Each iteration of the for loop, the commands.getstatusoutput() function will even return a tuple (status, output), where status is the status after executing the program, and output being anything that the command would have written to stdout.
Hope that helps.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Assuming a script fails and it is captured in a try catch how to run a python script again after a few days?
I can use sleep on this problem, but I think it will not work due to the fact that the server restarts every day. What is the best solution on this problem?
Typically you want to address this with a cron job.
I would probably do the following:
When the python file runs, save a log file with the status date/time.
Set up a cron job on the server to check, say once every 24 hours, that checks that log file and either do nothing or runs the python file again.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am coding a personal assitant in Python. At this moment, I am planning all the things I am going to do but I have come up with a problem that I can't solve.
I will be running a main script that will check if user says 'Hello' every 3 seconds. If he does so, then it should start running another script/function and stop the current one. After the task is performed it should start running again the main script (I will be using different scripts for each task to make it cleaner). I had thought about a while loop but I am not sure if this is the best option.
The select system call is the a very efficient way to wait until a file is ready to be read before doing something:
import select
import sys
while True:
reads, _, _ = select.select([sys.stdin], [], [], 3)
if reads:
line = reads[0].readline()
if line.strip().lower() == "hello":
# do a thing
print("hi")
Once hello is read, and your function or process is executed, your program will return to reading stdin.
Note that this works for POSIX systems but not for Windows (except for sockets).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
So its for a python script, no idea where to start for it lol. the script i'm using sometimes randomly stops and i need the batch file to automatically re run it whenever it stops.
I guess i need to make something with like fdos so like, i run the script, and lets say the batch file every 1 or 2 hours it restarts itself so i can always have it running without worrying about it stopping randomly.
Please help <3.
You could specify an atexit handler in your python script:
import atexit
import os
def exit_handler():
os.system("C:/Path/To/batch.bat")
atexit.register(exit_handler)
Where test.bat contains:
Python C:/Path/To/python.py
However, this will also cause the script to start up again if it exits legitimately.
Perhaps if you could identify what exit code is sent when it exits "randomly", you can check for that exitcode in atexit and start the batch file up if it matches your problematic exit code.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a python script script.py. I decide to execute this in the Terminal.
python script.py
It has been running for several hours. How do I check the status? How can I check whether this program is still responsive?
top doesn't work in this case, as the program is still running. I think there's a keyboard command---i't's something like 'Control' + 'something'
While your program is still running type C-z (while pressing 'Ctrl' press 'z')
You wille get a output like this:
[1] + 2267 suspended
python script.py
Then you can get the current runtime with:
ps -p "2267" -o etime
Replace "2267" with the pid shown in step 1.
When you want to resume your script type:
fg %1
You can think of %1 as the current count of "backgrounded" processes. If the output after C-z shows something e.g. [4] + XXXX suspended means that you must use fg %4 to resume the process.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a Perl script I want to embed into Python. Whenever I run Python the Perl code also should execute. Both codes are in same file. I am running from HPSA so there is no filename associated with code run.
Something like this?
from subprocess import call as sp_call
perlscript = """
use strict;
use warnings;
print "EHLO world\n";
"""
exitcode = sp_call(['perl', '-e', perlscript], shell=False)
If you need to capture the output from the Perl script etc, look at subprocess.Popen and friends.