Need to make an auto exec batch file [closed] - python

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.

Related

Re-run a python script after a few days if it fails [closed]

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.

Dashboard for monitoring the results of an iterative program [closed]

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 2 years ago.
Improve this question
I run a Python code in which an iterative process is done. Every few minutes an iteration is performed and the results are stored in a file. Currently, after each iteration I have to run another Python script to plot the recent results to monitor the progress. I want to have a dashboard which plots the recent results whenever the results file is updated.
It's not entirely clear what your question is, but it sounds like you want to monitor the output file for changes and plot them when the file is changed.
If you're using Linux (as the tag suggests), then I'd suggest using inotify, which is a Linux API allows you to monitor filesystem events (like file writes!).
There is a Python wrapper around this, also named inotify: https://pypi.org/project/inotify/. You should be able to add a watch on your log file and run your plotting function when it's modified (perhaps by watching for the IN_CLOSE_WRITE event).

Most efficient way to stop running a part of code [closed]

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).

Schedule Speed Tests [closed]

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 a way to test our network speed every 10 mins automatically and record the output, I found this python https://pypi.python.org/pypi/speedtest-cli and wanted to know if there is a way I could make a program that will run the command sleep for a few mins and then run it again capturing the output every time and logging it into a file.
The easiest way is to run a cronjob with also the date as name of the log file so after that you can analyse the logs.
Python logging:
python logging
You can also use greylog for logging in python, its simple and very useful:
greylog
Cronjob with date:
Cronjob

Translation of Shell Script in to Python [closed]

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.

Categories

Resources