-1

I want a python file to run automatically at 8am every day going forward. I try to use the library schedule as pointed out in the second answer here, in Windows.

import schedule
import time

def query_fun(t):

  print('Do a bunch of things')
  print("I'm working...", t)
  df.to_csv('C:/Documents/Query_output.csv', encoding='utf-8')

schedule.every().day.at("08:00").do(query_fun, 'It is 08:00')


while True:
    schedule.run_pending()
    time.sleep(60) # wait one minute

But 8am has come and gone, and the csv file hasn't been updated, and it doesn't look like the script runs when I want it to.

Edit: Based on this, I used pythonw.exe to run the script in the command line: C:\Program Files\Python3.7>pythonw.exe daily_query.py but the script doesn't run when expected.

3 Answers 3

1

You took out the key part of the script by commenting it out. How is the script magically supposed to rise up at 8 AM to do something? The point is to always keep it running and trigger at the right time using the mechanism provided by schedule library (running any pending jobs at time T on day D that is). What you are doing right now is just declaring the method and exiting without doing anything.

The point is to keep the script running in background and trigger the function by matching the current time with the time specified, running any pending assigned jobs as per your logic. You run your script in background and forget about it until 8 AM:

nohup python MyScheduledProgram.py &

nohup will take care that your terminal doesn’t get any output printed on it from the program. You can view the output from nohup.out though.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but is there an equivalent answer for Windows?
1

On a windows machine you can use "Task Scheduler" to run a python script repeatedly:

  1. Search for "Task Scheduler" in the start menu and open it.
  2. Now in "Actions" menu click "Create task".
  3. In the "General" tab, give it a name and a description.
  4. In the "Actions" tab, select “Start a program” as the action type.
  5. In the “Program/script” field, enter the path to your Python executable file (e.g., C:\Python39\python.exe). You can find your python executable path by typing "Where.exe python" in CMD or PowerShell
  6. In the “Add arguments” field, enter your scripts name (e.g., myscript.py)
  7. Then in the "start in" field, enter the path to your script (e.g., C:\Users\username\Documents).
  8. click on "ok" to save your Action.
  9. Now you can easily go to the "Triggers" tab, click on the new button and schedule your task however you see fit: daily, hourly, and even "at startup" or at "log in".

With this method you can completely remove "schedule module" and its related code from your ".py" file.

Comments

0

Here you can easily see what the skript does:

schedule.every().day.at("08:00").do(query_fun, 'It is 08:00')

tells the scheduler to run the function if it is 8am. But the other part of the library is this one:

while True:
schedule.run_pending()
time.sleep(60) # wait one minute

this part checks if it should start a skript, then it waits for 60 seconds, and checks again.

EDIT:

The question was related to a Windows machine, therefore my answer has no point here.

If you are on a linux machine, you should consider using crontabs:

Open a terminal and type crontab -e

After you selected the editor you wanted (lets take nano) it opens a list, where you can add various entries

just add:

0 8 * * * /usr/bin/python3 /home/path/to/skript.py

Then save with STRG + O and exit nano with STRG + X

The skript will run everyday at 8am, just test the command

/usr/bin/python3 /home/path/to/skript.py 

to make sure the skript does not produce an error

1 Comment

Does the line df.to_csv('C:/Documents/Query_output.csv'...) tell you anything about whether the asker is using Linux, and hence whether they can use cron?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.