This is my first time to schedule a python script.
My goal is to schedule my python script to run every 10 min.
MAIN.PY
from scrapy import cmdline
cmdline.execute("scrapy crawl news".split())
I created a task in windows task scheduler, but my script is not running somehow( I know the script is working since i can run it manually ). I been trying to solve this issue for hours without any luck.
First I select my python path, then I select the filepath for the project and choose the .py I want to run.
example here
Then I choose to trigger every 10 min.
After doing the steps described above, nothing happens.
Hope you can help me with a solution to my problem.
Not sure that it will fit in your case, but I think it might. Please, check out this topic.
How do you run a Python script as a service in Windows?
Maybe you can run your script as a service and add a simple timer inside the script that launches your routine every 10 minutes from the inside.
Alternative solution that a colleague of mine is using is packing the python script inside an executable by using fbs or pyinstaller and adding it into the task scheduler.
it might be the environment problem.The windows task scheduler is running in C:\Windows\System32 by default,so if your some argument is not absolute path or will changed with the environment,it will get wrong result.i suggest u use some logging way to record your program's running status and result so u can debug where went wrong
Related
I have an Ubuntu 16.04 EC2 Instance and I need to run a python script every time the instance is Started.
I tried everything suggested on every question in the forum and haven't had any luck yet.
Specifically I've tested:
Adding #reboot python3 /home/project/script.py to crontab
Adding #reboot /bin/startup.sh and having the bash file configured to run the /home/project/script.py
Using etc/rc.local, etc/init/mystartup.conf, etc/systemd/mystartup.conf
Passing User Data
Probably missing a few others and literally nothing worked even though running the script manually works wonders.
Thanks a lot in advance for the help!
Put the script into: /var/lib/cloud/scripts/per-boot/
Cloud-Init, which runs User Data, will also check this directory.
From Modules — cloud-init documentation:
Any scripts in the scripts/per-boot directory on the datasource will be run every time the system boots. Scripts will be run in alphabetical order.
I wrote a python script that run an infinite loop and every half second checks if there are new files in a directory:
while True:
files = os.listdir(path_to_dir)
# do something
time.sleep(0.5)
The code runs on windows 10 in a cmd window and I need to make sure it will never stop.
A) I need to find a mechanism (or few mechanisms) that will restart the script in all possible scenarios that it might turn off (it is ok if the restart will happen only 2 minutes later...):
if the computer is restarting
if someone close the cmd windows
if the script end unexpectedly because of unhandle exception or memory leak (it is not suppose to happen...)
B) I want that once in a week, proactively,the script will be turn off and restart.
Any ideas?
Thanks!!
p.s. my first idea was that the python script will only check for new file, and the task scheduled will run it every second, but the minimum interval for task scheduler is 1 minute.
I think these answer all your questions:
Since your script runs on windows, you can use the Microsoft Task Scheduler (which is installed with Windows) to start your Python script when your computer starts up.
If you do not use the cmd window, you can change your Python script extention from .py to .pyw to run the script without a terminal window. A bit more on that here: Executing scripts.
For opening the script after an exception has happend, have a look at this blog post: How to Restart Python Script after Exception and Run it Forever.
To restart your script once a week, you can also use the Task Scheduler mentioned in answer 1. I think this post could help you with restarting your script: Start and stop a python task
I have written a python (python version is 3) script that runs 24/7. The way I run my script in my Windows machine is the following. I right click on the .py file, then click on "Edit with IDLE" and then "Run". The script has no issue but, due to the many instructions printed in the python shell (I use a logger), after a couple of days this python shell gets very heavy. My newbie question is the following. Is there to limit the number of rows temporarily saved in the python shell to a specific number? Or perhaps somebody has a better suggestion to run this constantly running script that prints a lot of the script steps in the shell? Please, notice how I'm not asking how to run a script 24/7, it's my understanding the best way to do it is though a VPS. My problem is that the data saved in the displayed python shell gets bigger and bigger every day, so I only wonder how to limit the data temporarily displayed/saved in it. Thanks
I have a very simple python script (to send an email) which works perfectly when I run it in the cmd window, or in python, or if I directly start a a .bat file pointing to it.
However, when I try to get the task scheduler to run it, nothing happens. The task scheduler says that it runs and completes successfully, the log file is blank, but no email is sent.
I'm aware there are a lot of other questions relating to this problem, and I've read through them and tried the solutions, but nothing seems to work. I am new to python (and to scheduling tasks!), so I may be implementing the solutions incorrectly.
Here is what I've tried...
Creating a batch file with the script in it in various ways:
python C:\Users\me\Documents\etc\script.py >C:\Users\me\Documents\etc\log.txt
python "C:\Users\me\Documents\etc\script.py >C:\Users\me\Documents\etc\log.txt"
C:\Users\me\AppData\Local\Programs\Python\Python35-32\python.exe C:\Users\me\Documents\etc\script.py
"C:\Users\me\AppData\Local\Programs\Python\Python35-32\python.exe" "C:\Users\me\Documents\etc\script.py"
C:\Users\me\Documents\etc\script.py
All of these work fine when double clicking on the file - but none run in task manager. (Although they say they have completed successfully). In task manager I just put the link to the .bat file in the "Program/script" box.
Doing the above but with the full path to the cmd.exe in the program/script, and the .bat file as an argument. I've also tried putting the location of the bat file in the "Start in (optional):"
Trying to run the .py file directly via the task scheduler, putting it in the "Program/script:" box
Trying to run the .py file directly via the task scheduler, putting the full path to the python.exe (see above) in the "Program/script", and the sript.py in the "Add arguments". I've also tried this with path to the location of the script in "Start in".
Trying to run the .py file via the cmd - so putting the full path to the cmd.exe in "Program/script" and the script.py file (full path) in the "Arguments".
Some of the ones where I try to run the script.py directly just say "running" in the Task Scheduler forever, but I didn't note down which these were.
I'm running Windows 7 (64bit), and have got Python 3.5.1 (32bit). I've got local admin rights.
Other scheduled tasks I have created (not involving a python script) work fine, but this has stumped me. Please help!
Just adding an answer in case this affects any other newbie :). I needed to check "Run only when user is logged on", and also uncheck "Run with highest privileges".
I'm guess this is because as eryksun says Outlook has a GUI.
Something else to try:
Make sure in your batch file you're adding a command to change directories to where your executable lives.
#echo off
echo.------------------------------------------------
echo.Windows Task invoked on %date%, %time% (local time)
echo.------------------------------------------------
SET My_exe_dir="C:\Program Files (x86)\MyProgram\FolderWhereExeLives"
SET Input_dir="C:\Program Files (x86)\MyInputFolder"
cd %My_exe_dir% <-- This was the key for me.
%My_exe_dir%\myprogram.exe %Input_dir%\MyInputFile.xml -1
For me, specifying the "start in" directory worked:
Define Start in directory in properties of scheduled task.
Start Windows Task Scheduler.
Navigate to the task and double click on it to open the Properties of the task.
Select tab Action and click on button Edit.
There is Start in (optional). Enter here the path of the executed batch file.
Click two times on button OK to save this important modification in properties.
Through an application I have made with Tkinter, I'm trying to add a command to run a script every week. When the program is closed the command should be in forever place.
I've sifted through the documentation on cron, but there doesn't seem to be a way to edit the crontab without using the shell. Also I've looked through the 'at' command, but that only seems to run once.
My question is - How can one create a weekly recurring task by issuing a single command in Python on Unix?
If not with only 1 command, can I use multiple?
In most modern Linux distros like Debian or Ubuntu, you can add an executable file (like a shell script or a symlink to one) into /etc/cron.weekly and it will be automatically run once a week for you. This is using the anacron command, which is fairly common these days.