0

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!

martifapa
  • 107
  • 3
  • 12
  • You should look at TaskScheduler for Windows or cron for Linux. But you want your code to run while the PC is turned off...? – dspencer Apr 17 '20 at 10:38
  • Wake on LAN could help to start the PC with scripts, in that case the scheduler should work on another PC that always works as well. Bad practice to `import Script1` that run something, just define function in it, then in Script3 `from Script1 import example1` and run it `example1()`. Scheduler apps are trying to run without much CPU loading. – frost-nzcr4 Apr 17 '20 at 10:40

3 Answers3

1

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.

1

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

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
0

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.

Devansh Soni
  • 771
  • 5
  • 16