Automatically Schedule Python Scripts with Cron Jobs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys welcome back in this video today we're going to learn about cron jobs and we're going to learn how to schedule Python scripts for example backup scripts so let us get right into [Music] it all right so cron is a command line tool that we can use on Unix like systems like Mac OS and Linux distributions to schedule jobs and in this video today we're going to use cron in combination with python to automate a simple backup procedure so we're going to have a folder on the desktop let's call it backup files for example and in this directory we're going to have different files documents images whatever and we want to have a python script that just takes the contents of this directory and uploads them to Dropbox so it backs up the files to Dropbox and now instead of running this script every time we want to do a backup manually we want to schedule a job that runs um runs on a regular basis so we can say once a minute once an hour every day at this time whatever we want to do that with KRON so we're going to schedule the job of running this backup script instead of having to run this automatically all the time um and the first thing of course we need to do here is we need to make sure that cron is installed on our system so just open up the command line depending on your operating system you're going to have to use different commands obviously so depending on your Linux distribution if you're using Linux you're going to have to use a different package manager on uh Mac I think you also have to use some package manager on Windows you don't usually use Chrome you use the task scheduler so that's a different Tool uh but what you can do here now on Debian based distributions is you just say pseudo app install and KRON and in this case it's going to install KRON if you don't have it yet and it's going to not do anything if you already have it so once you have KRON installed we're going to start with a simple example first so let's say we want to have some command that we want to automate we want to run this automatically every minute for example and just to keep it simple here here we're going to use a very simple command we're going to Echo uh the random variable like this so Echo random basically just gives you a random number as you can see I can run this I get random numbers here now instead of just putting it out to the command line I want to save it into a file so I can say here store this in uh let's say home neural 9 desktop let's call the file my file.txt so when I do this you can see I have this my file.txt and I have this value in here and the next time I run this you will see that I get uh let me run this again you will see I get a different value here um so we want to automate this just to see as a proof of concept here how KRON works so what we need to do to automate this and this is actually a little bit tricky in this case because the random variable and this is the first thing we can learn about KRON the random Val variable here um I need to Echo of course um only exists in this particular shell that I'm using right now the bash shell uh it does not exist in the Shell which is under /bin sh so this is now a different shell if I say Echo random here it cannot be found so it doesn't return any number here uh why is this important it's important because Chron by default uses this shell so we need to actually specifically say that we want to use bash so the actual command that we want to automate in this case is bin bash and then- c and then the command that we have so Echo random which is the exact same behavior as before or actually of course with slome noral 9 desktop and then my file.txt it's the exact same behavior as before but it's now the complete Command that we need to automate so you can choose whatever command you want it's just something that is going to run automated uh automatically every minute in this case now and in order to schedule this what we need to do is we need to run the command cron tab d e let me just zoom in a little bit so you can see the command line a little bit better Chron tab d e is going to open this file here where we can schedule our tasks now you can skip all these comments you can go to the bottom of the file and here you can see uh how this basically works you have minute hour day of the month month and day of the week and then you have the command so if you don't want to specify for one of these things a specific value what you can do is you can just provide an asterisk so a star symbol here so if you provide five asterisks this basically means every minute every hour every day of the month every month every week every day of the week so it basically means every minute run this command with no exceptions if I do something like 014 star star star or asteris risks uh what this basically means is every day of the month every month every day of the week run this at 14:00 so 2: p.m. that's the idea now to see results faster we're going to use the every minute combination here so we're going to use four asterisks to say run this thing every single minute and the command again is bin actually slash bin bash d c and then we want to use here the command again uh Echo dollar random and then we're going to say /home noral 9 desktop and then uh myfile.txt and that is now our Command that we want to schedule every single minute so I can just write this I can exit and now you can see installing new Chron tab so now every minute this command is going to run you can see now it's 6:42 p.m. if I now say watch desktop myfile.txt you can see now actually why is this denied watch cat sorry watch cat my file txt we can now see the content of this file in somewhat real time so every two seconds it basically updates the command so I can see the content here 15160 and now when it gets 643 we should see an update here we should see a different number because the command is going to be triggered so we just have to wait in this case now but you can see it doesn't take too much longer 4 seconds and then we should see an update for this file and there you go the content changed because we automatically scheduled this command uh of course if you want to change this again you just say cron t- e you go to the bottom you delete the line that's it so this is how you schedule crw jobs in general what we're going to do now is we're going to create a python script which backs up these uh backups these these files here in backup files so I'm going to say here backup uh program. py and what I'm going to do here is I'm going to say import OS import sh util and I'm going to say files path is going to be equal to slome neural 9 desktop and then um what was it backup files like this so that's the directory and then we're going to say Dropbox path in my case Dropbox is at slome noral 9 Dropbox like this so we have the files we have the Dropbox path and basically all I'm doing here is copying the file so for a file in OS list directory and then uh basically just false path what I'm going to do is I'm going to say shutil do copy and I'm going to join OS path join I'm going to join here the files path and the file name and I'm going to copy this to OS path join to Dropbox path and file that's basically it and what this is going to do now for us is um let me just open this up here we're going to go to Dropbox uh the backup actually we need the backup directory for this so we're going to say backup directory here I actually already created it in my Dropbox uh so this is my Dropbox backup directory it's empty now and this is going to copy the backup files into the backup directory uh directory so what we're going to do now is we're going to first create some files in here so I'm going to open up the terminal and I'm going to say test.txt hell world and then maybe we can also yeah let's just use text files here and I want to create some images now um or actually let's go ahead and create an image I'm going to use for this I'm going to open up I'm going to create a new image like this here I'm going to just draw something hello something like this just so we have different file types export I'm going to export this to desktop backup files Untitled PNG it's fine export there you go this card changes um and now we have these two files if I now run my python script here Python 3 backup program we have a problem because I never closed this bracket here so let's do that now run this again there you go let's go back to the backup directory and you can see now these two are backed up these two files are backed up to the Dropbox now let's delete them again from the Dropbox every time we run the script now we're going to take the files of this directory and we're going to upload them to Dropbox so what we're going to do now is we're going to say CR tab d e we're going to go down here or actually one thing that I forgot what we need to do first is uh we need to realize that or we need to locate the python version that we're using so we need to know okay when I'm running Python 3 here what am I actually running because that's important because remember we're running this from a different shell with different environments so what we're going to do here now is we're going to say which and then Python 3 and in my case this is a pyin Python 3 version in your case it might be something like user bin Python 3 whatever you get here this is the python that you want to run so I'm going to copy this here here and I'm going to go to crr tab d e at the bottom we're going to say here just for demonstration purposes every minute again and the command is going to be the Python 3 command so the path to Python 3 and then we're going to say home neural 9 uh desktop and then uh what was it again it was actually on a desktop Backup Pro do py so this file is now on the desktop it's going to run with Python 3 all the time so I can just save this here and what we need to do now is we need to wait for our uh clock to so that 1 minute is passed so at 6:49 p.m. we should get an update here and they should hopefully back up the file so I'm going to keep this open up here in the background maybe we can see what the current time is exactly okay we're going to have to wait 40 seconds seconds here all right so now we should see the script being triggered in a second now there you go backup happened so this is now going to be run every single minute it's going to do a backup of course this is not very useful what I would suggest uh you do is you do a daily or weekly backup so for example as I said every day 2 p.m would be star or actually not sorry zero 14 asterisk asterisk asterisk and then the command would mean every day at 14:00 which is 2: p.m. run this script in my case I'm going to remove this now again but you can use KRON in many different ways whenever you want to automate something whenever you want to have something being run every month every week every year or every minute hour whatever uh you can just schedule a Chron job and you can automate the process which can be very useful so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course as always don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you next video and bye
Info
Channel: NeuralNine
Views: 9,750
Rating: undefined out of 5
Keywords: cron, automatically, schedule, job scheduling, job schedule, scheduling, cron job, python, python cron, cron jobs python
Id: 2sehQ5oABqI
Channel Id: undefined
Length: 13min 25sec (805 seconds)
Published: Wed Nov 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.