martifapa
martifapa

Reputation: 117

Python - Run script periodically

Right now I have some scripts I want to run automatically and periodically, say, every two days.

The way I've structured looks something like this.

Script1:

def example1():
    #Some Selenium code

example1()

Script2:

def example2():
    #Some more Selenium code

example2()

Script3:

import Script1
import Script2

As you'll notice, 'Script1' and 'Script2' already call their main function, so in 'Script3' I only need to import the scripts, and not call the functions (although this works for me, I'm not sure whether this is a safe approach/good practice).

My question: if I use schedule to run 'Script3' every x days, will this mean the script will run forever, or does it run once every x days and then goes into sleep mode until it has to run again? Also, for it to run it would requiere the PC to be always on, right?

If so, is there any way to make it run automatically and periodically even with the PC turned off?

Thanks in advance!

Upvotes: 0

Views: 1735

Answers (3)

Roland Smith
Roland Smith

Reputation: 43495

I'm not sure whether this is a safe approach/good practice

The Zen of Python says; "Explicit is better than implicit." So it is better to use:

 import script1
 import script2

 script1.example1()
 script2.example2()

It is generally not recommended for a module to run code on import. Importing a module should preferably not have side effects.

if I use schedule

There are probably more than one program named "schedule". Can you be more specific?

is there any way to make it run automatically and periodically even with the PC turned off

There are more than one way to run a script periodically. UNIX-like systems such as Linux have cron and atrun. Other systems have their own way.

But all of those only work when the machine is on. Turning a machine on at regular intervals is generally not possible on a PC without extra hardware. Unless you can get the Intel Management Engine to do your bidding.

Microcontrollers such as the ESP32 (which can run micropython) do have a "deep-sleep" mode for this purpose but I kind of doubt they can run selenium. :-)

Upvotes: 1

Jonathan Sánchez
Jonathan Sánchez

Reputation: 464

I suppose Windows scheduler works in the same way as cron jobs in linux. So in fact after creating the "task", windows checks every second to see if he has to do any task. But this is being done always, so this has no impact in performance if that is what you're worried about. Then just to clarify:

  1. Windows sees the batch file scheduled and execute it on time.
  2. Windows execute the python script inside the batch file, and the script finishes.
  3. Windows keeps checking the scheduler but the process of the script "died".

For the part of doing it even with the PC turned off, that's not possible.

Upvotes: 1

Devansh Soni
Devansh Soni

Reputation: 771

If you are on Linux or any other Unix based OS, then you can use Cron for job scheduling. But if you shutdown while the cron jobs are running, the system shuts down and the cron jobs stop (or don't run).

One alternative you can check into is anacron.

Upvotes: 0

Related Questions