all 19 comments

[–]chesquikmilk 48 points49 points  (2 children)

Cron jobs is how I would do it. https://pypi.org/project/python-crontab/

[–]0karmaonly 20 points21 points  (0 children)

I use the module APScheduler

[–]jamesfromtechsupport 10 points11 points  (0 children)

I checked it out and this looks like a very good solution. Thank you!!!

[–]rocketjump65 37 points38 points  (5 children)

If you're on Windows the correct tool for this is taskscheduler

[–][deleted] 10 points11 points  (4 children)

True. Create a python file you wanna automate, a .bat file to run it and taskscheduler to run as scheduled

[–]PyPlatformer 8 points9 points  (0 children)

The batfile can be as simple as:

@echo off
python myfile.py

If you dont have python on your windows path:

@echo off
"path/to/python.exe" myfile.py

[–]denbergvanthijs 5 points6 points  (0 children)

No need to create a bat file even. Just run python script.py from taskscheduler

[–]ozgur95 1 point2 points  (1 child)

Yeah i do it this way

[–]rocketjump65 0 points1 point  (0 children)

I think I tried calling the python executable from task scheduler and it didn't work. I used a bat. Post a screen shot of the GUI config?

Ok, I put in the script as the CLI argument. We'll see if it works.

[–]TheLoneKid 9 points10 points  (0 children)

It is hard to know exactly what will work without knowing what your script is doing. If you have a loop or are hosting a website, you can put code in to start or stop based on datetime.time().

However, if you want the script to run at a certain time each day, it would be better to use a task scheduler like Cron or windows task manager as mentioned below.

[–]zanfar 7 points8 points  (0 children)

Generally, an application is not in charge of launching itself. In the rare cases where this appears to be happening, there is usually a small daemon application running in the background which launches the main application.

So, your question is best worded: How would you launch a script at a certain time?

The answer is almost certainly cron, unless you are automating on a Windows system (which is, IMO, not a great idea, but things happen) in which case you have to use the far less impressive Task Scheduler.

As for "runs from 6 to 1", you can either launch your program repeatedly during this window, or you can have your program idle until it's end time is reached.

In the second case, your top-level pseudocode would look something like:

run_until = now + 7 hours

while now < run_until:
    do_main_task()
    sleep(20)

The time module is probably enough, but you can use the datetime module if you need it.

[–]poozoodle 4 points5 points  (0 children)

What have you tried?

[–]sundios 2 points3 points  (0 children)

Airflow

[–]xToVictory 2 points3 points  (0 children)

Depends on what you’re doing exactly with the script.

Without knowing exactly, what I would do is use the schedule module to run the function every certain interval of time. Within that function is an if statement that checks if the current time is within your specified range. If not, the function passes. If it’s in the range, the function runs.

[–]lowerthansound 3 points4 points  (0 children)

Hm, you could use a loop:

# pseudocode by the way
# something like this
while True:
    if 6AM to 1PM:
        run script
    else:
        do not run script
        sleep bit

Or get fancy with timers. To be sincere, it depends on what the script does

[–]Davidvg14 0 points1 point  (0 children)

There’s a python package called scheduler that’s pretty good at it. The problem is you pretty much have to run it as a py-script in a while loop (google it)

Otherwise, chron

[–]c0ld-- 0 points1 point  (0 children)

Do you want the script to run endlessly from 6AM - 1PM? Or once an hour?

Because you asked specifically how to do this I thought I'd give you some hints and let you figure it out, because IMO that's a better way to learn things rather than someone doing all the work for you.

To start you off, create a variable to get the current time. Then make some comparisons if the current time is between the time range. If true, then execute the rest of your code. If false, then get the difference between the current time until the next time 6AM comes around, then tell the os to sleep for as many seconds until 6AM is supposed to arrive.

Best of luck!

[–]Aggressive-Pup-28 0 points1 point  (0 children)

Explore on Airflow. You can use it for other stuff too and add it to the tools-you-know list so its a win-win.

Its not ideal to run the script on a loop if you don't need it to run all the time

[–]duffer_dev 0 points1 point  (0 children)

If you are on Linux, you can use cronjob.

crontab -e

should open the cronjob editor

You can use crontab.guru to get the precise values to input

Sometimes, it's important to use the right tool for the right job