How to Schedule and Execute Python Scripts as Cron Jobs & Execute Them Every After 5 Seconds
2K views
Jun 3, 2025
Get the full source code of application here: https://codingshiksha.com/python/how-to-schedule-and-execute-python-scripts-as-cron-jobs-execute-them-every-after-5-seconds/
View Video Transcript
0:00
uh hello guys welcome to this video so
0:02
in this video i will show you how to
0:04
schedule and execute python scripts as
0:07
chron jobs or exeu execute them every
0:10
after uh x amount of seconds let's
0:13
suppose 5 seconds 10 seconds so there is
0:16
some situations out there because uh you
0:19
need to schedule your python scripts to
0:21
actually execute at a specific amount of
0:23
time in the whole day so let's suppose
0:26
if you want to delete something using a
0:28
python script so you only want to delete
0:31
at a specific amount of time so i will
0:33
show you in this video how to schedule
0:35
it to run them at a specific amount of
0:38
time
0:39
so there are quite a different amount of
0:42
ways you can do this the very first
0:45
basic way by which you can do this is
0:47
using the time module which is a
0:49
built-in inside your python
0:52
uh installation so you need not have to
0:54
install anything just import the time
0:56
module and right
0:58
here you run a simple while
1:04
loop and inside this while loop you
1:08
actually put true here so it will be
1:11
running infinite number of times so here
1:13
you basically write any code that you
1:15
want to execute it can be anything and
1:18
after that you sleep for some time using
1:20
the sleep method so here this value is
1:23
in second so this means that it will
1:26
print out hello world it will execute
1:28
this code and then it will pause for 1
1:30
second and then it will repeat because
1:33
it's a while
1:35
loop so now the output of the program is
1:38
very simple you simply execute the
1:40
program and then it will print out hello
1:43
world and then it will pause for 1
1:45
second again execute and this will
1:48
execute infinite number of time so you
1:51
can even put a restriction that after
1:53
five attempts this will get
1:55
over so this is the very basic way by
1:59
which you can schedule python scripts to
2:03
repeat after every
2:05
second so the next way is slightly
2:09
complicated for this we actually use a
2:13
third party package for this so i
2:17
personally like this package which is
2:19
called as schedule should schedule the
2:20
name of the package is schedule it's
2:23
simply install this by executing this
2:25
command it's similarly scheduuling the
2:28
scripts you first of all import this
2:31
module
2:34
uh after importing it you specify it by
2:38
invoking this and it contains a every
2:42
function and uh right here you specify
2:45
the amount of time so every 5 seconds
2:48
after every 5 seconds i want to execute
2:51
this so here it will seconds you can
2:54
even say minutes as well there is also
2:57
minute as well but i will say every
3:00
after 5 seconds we need to execute this
3:03
do so here you can define a
3:07
function so it can be anything this
3:10
function can be anything so hello world
3:13
so now we define this function right
3:15
here which will be hello
3:18
world so here we print out hello world
3:21
simply so this can be anything it can be
3:24
any cron job that you want to execute
3:27
and then we just need to call this
3:29
schedule function in the while loop to
3:31
repeat this infinite number of times so
3:34
schedule dot run pending that's all this
3:39
code is used to initialize this package
3:42
so this will call this function after
3:46
every 5 seconds you want to execute this
3:48
hello world function which will print
3:50
out hello world on the terminal so if
3:52
you just execute this
3:55
now after 5 seconds it will take a pause
3:58
of 5 seconds and then again it will
4:00
print out hello
4:02
world again it will take a pause of 5
4:05
seconds and then again it will print out
4:07
hello world so this hello world can be
4:10
anything it can a code for deleting
4:13
files so here you can put a restriction
4:16
let's suppose every 60 seconds so here
4:19
you can change this so now this will
4:22
instead of waiting 5 seconds it will
4:24
wait an entire minute and then after
4:27
that it will execute this hello world so
4:29
in this easy way using this third party
4:31
package you can actually schedule your
4:34
python scripts to run at a particular
4:36
time interval as a chrome job you can
4:39
run these python scripts so i just loved
4:44
this module uh to
4:46
actually uh run python scripts uh after
4:50
x amount of seconds or minutes or hours
4:53
as well i
4:54
think there is also this minutes
4:58
available or you can even schedule it to
5:01
run on a specific time as well so here
5:04
add time as well so here you can
5:06
actually provide a specific time
5:08
interval so you can see in 24hour
5:13
format so 20 you can provide the full
5:17
time interval so here you can actually
5:21
add a specific time interval you can put
5:23
the time interval and then at that
5:25
specific time the python script will
5:27
execute so you can build out some
5:29
advanced kinds of application just
5:33
install this module pip install should
5:35
schedule and check out their
5:36
documentation
5:40
uh i think this
5:42
this is a complete example that they
5:45
have given on their documentation
5:47
website let me execute this so right
5:51
here every 10 seconds every 10 minutes
5:54
every 1
5:55
hour at a specific day at a specific
5:58
time you can see all these permutation
6:02
combinations are there you can just
6:04
execute this and integrate inside your
6:11
application so you can actually schedule
6:14
multiple amount of combinations and
6:18
whenever that
6:19
combination comes out to be true your
6:22
your script your code will get executed
6:25
so in this easy way guys you can
6:28
schedule your python scripts in as a
6:31
chron job thank you very much for
6:33
watching this video and please hit that
6:35
like button subscribe the channel and
6:38
also check out my website
6:40
freemediattools.com
6:42
uh which contains thousands of tools
#Programming
#Scripting Languages