ritos
ritos

Reputation: 671

How do I run python script everyday at the same time with scheduler?

This would be quite a general question though, what I want to know is: when scheduling a python script(ex. everyday 1:00 PM), I wonder if we have to let the script(or editor such as spyder) always 'open'. This means, do I have to let python always running? I have avoided to use scheduler library because people say that the python script is not killed, pending and waiting for the next task. What I have been doing as far was just using Windows Scheduler to run my scripts(crawlers) automatically everyday(people say this is called the 'batch process'..). But now I have to do these jobs on the server side, not in my local any more.

Therefore, how can I run my python scripts just the same as the Windows Scheduler, with using the python scheduler library?

Upvotes: 0

Views: 1635

Answers (2)

yadayada
yadayada

Reputation: 357

I would recommend systemd timers. Many people use cron and crontabs but it requires your system to be running 24 hours a day and not easily "debuggable".

The one major thing I like about systemd that:

  • all systemd timer events are carefully logged in systemd journal. Like in case with cron job it's hard to check what your cron job doing and it can become a mess.
  • systemd timers are systemd services with all their capabilities for resource management, IO CPU scheduling.
  • it's easy to use it to enable/disable/kill the job/etc. By just doing:

    systemctl enable/disable/start/stop

  • another thing I like if you on different timeszones you schedule systemd with calendars and monotonic times. And many more useful debugging features.

There are more good alternatives :

Jobber is a GO task scheduler is a more powerful tool than cron too. It features job execution history with status, controlling whether and when a job is run again after it fails and getting notified on each failed to run or only about jobs that were disabled due to repeated failures.

Hcron is good one and has some nice caveats like, storing events individually, each in their own file, rather than all in a single file and more here: https://expl.info/display/HCRON/Home and documentation is here https://expl.info/display/HCRON/hcron+Guide

Hope you will find this helpful.

Upvotes: 1

suryadina
suryadina

Reputation: 96

You can use cron in linux. I also use cron to run my python script on my shared hosting server. And if you need to install python modules on your server maybe you also need to create a virtual environment using virtualenv. From my experience, if your script has clean exit than your python script will be killed or terminated properly, so you dont have to worry about python script not being killed and consuming your server resources :D

Upvotes: 2

Related Questions