How to Schedule a Python Script with a Cron Job

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone this is tony teaches tech i'm tony and in this video i'm going to show you how to schedule a python script to execute at a certain time or maybe an interval and the way we're going to do that is with a cron job now if you're not familiar with cron jobs or crontab i will definitely explain that in this video so if that's something you want to learn how to do let's go ahead and get on into the tutorial here okay so just to make sure we're all on the same page with what cron jobs are cron tabs is then we're going to do a simple example and then we'll incorporate python into that so we all know that there is a date command which just simply spits out the date on the command line here let's add a cron job which is again a way to schedule the execution of a command and we're gonna execute the date command when we do that so um basically what that's gonna be is let's take the output from date the date command and redirect it to a file in the temp directory called test.text okay so if we do that on the command line hit enter we won't see anything but if we look at temp test.txt you'll see that the date has been added to that so 706 58 pm utc if we execute this again our temp dot test test.txt file will update with the current date so that's just a way for us to track that the cron tab is actually executing okay so let's go ahead and apply this to a cron tab so this is precisely the command that we want to execute so let's copy that and we'll type in cron tab dash e for edit and i'm going to use bim to edit my cron tab this is just a once and done selection here the first time you run kron so i'll type two here and down here this is just all comments explaining how cron jobs work but what we want is to basically every minute of every hour of every day of the month of every month of the year of every day of the week we want to execute that command and if you're not familiar um well let's let's let's go ahead and save these changes uh just so we can go ahead and schedule that cron job to run and what we're going to do in the background is just watch that file to see it change in in real time or actually what what the watch command does is it executes the command the cat command every two seconds so right now this is what we see in our temp dot text file temp slash text test.text whatever what we see in the temporary file that's the time but when the cron job runs we'll see that update to the current time at that moment okay but back to what i was saying sorry from all over the place if you're not familiar with cron this is kind of how this works so this there's five different um slots here the first slot is that minute so if you want your cron to run maybe at 12 30 you would type in 30 space 0 for midnight or 12 4 noon and then this third slot here is the day of the month so it could be the first of the month the 15th of the month or you can do like the first comma second of the month and then the month one through 12 so january february march and then the day of the week so zero is sunday six is saturday and it also says that some systems um seven is sunday so that's that's a very quick overview of cron in this example in this case we're just going to keep it simple just execute it every minute on an interval but by combining these five different slots you can do pretty much any type of interval that you could possibly imagine so that's cron syntax and you can see here if you remember the time frame before it was updated from um whatever it was to 709 01 pm utc and very good it just updated to 7 1001 pm utc so at the top of every minute basically one second after it's executing our date command and piping that or redirecting that into temp slash test.txt okay guys now let's combine what we know about cron jobs and crontab with python so let's create first a python script a simple python script that does something similar to what we were kind of doing but this could be any type of python script whatsoever that you want to execute at an interval so let's make in again we're in our the root directory here let's make a python script called rand.pi and i'm just going to use my cheat sheet over here to paste in some python code and we'll execute this in a second but basically we're going to get the current date and we're going to pick a random number between 1 and 100 and inside of the temp directory we're going to create a file called rand.text and spit out the time dash your random number is something all right so let's save that script and let's execute it once just so we know what to expect so we'll type python 3 rand.txt and rand oh right python3 rand.pi that's that's the name of our python script so let's go ahead and look in the temp directory we should have the rand.text file that we generated from executing that python program and you'll see that it says the current date and time and your random number is 68. so if we go ahead and execute that again what it's going to do is add another line to that file so your random number is 55 and that was just oops oh boy uh sorry about that let me make this to the appropriate size okay so that was just a couple seconds later one second later your random number um is 55. all right so um let's combine the two like i was saying so what we need to make sure of when we execute cron jobs cron tabs is make sure we're using the absolute path to everything that includes python and the file that you want to execute so let's make sure that we know where everything exists so our rand.pi file is at slash root slash rand.pi and if we do which python 3 we'll see that our python 3 program exists at user bin python3 so when we go back into cron tab dash e we want to use those full paths so again this will be every minute every hour every day of the month every month of the year and every day of the week we want to execute and i'm going to say user bin python 3 and then at root we root at in the root directory there's a python script called rand.pi so and a good practice would also be to test out the absolute path so let me copy that and we'll go ahead and save this too but i just want to make sure that everything matches up so let's execute that here because that's what cron jobs going to run and it works so and we can double check that with um cat cat ran text so there we go so we got another line in there and let's check the time right now so let's see if it made it fourth entry and it did so that just executed at 9 16.01 and we'll check the time so in approximately 45 seconds this will execute again so let's just fast forward through that okay and we're back and we're looking at eight seconds to go three seconds to go five seconds ago let's look at the file right now uh we have four entries in here our last random number is 87 and now it is after the top of the minute so let's look at the file again we have our next entry in here at 17.01 and our new random number is 58. so hopefully that gives you a good idea about how to combine um cron tabs with python scripts if you have any questions about this let me know in the comments below i'll do my best to help you out the the one important thing i just want you guys to remember is to use those absolute paths you just don't want to say python in the name of your script because uh cron might be using a different user and they might not have access to those same directories so um basically that's just a long weird way of saying just use absolute paths just to be safe guys please give this video a thumbs up if you liked it it really helps with my channel if you want to see more videos from me in the future definitely consider subscribing and if you do i'll see you in the next one [Music] you
Info
Channel: Tony Teaches Tech
Views: 67,828
Rating: undefined out of 5
Keywords: cron job python script, run python script cron, python cron job example, cron to run python script, python scheduler cron, cron job to run python script, schedule python script
Id: EgrpfvBc7ks
Channel Id: undefined
Length: 9min 26sec (566 seconds)
Published: Fri Apr 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.