Linux - Cron Jobs for Scheduled Tasks (crontab)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
please go to Eli the computer guy calm in order to view schematics code and more for the projects that you are learning about welcome back so in today's video I'm going to show you how to use a tool called crontab to be able to schedule events within your Linux environment so when I talk about scheduling the events basically what this means is you are going to be able to put in a full-fledged command something let's say possibly deleting cached files on your server possibly optimizing a database maybe even running in a Linux from the command line so what you're going to be able to do is you're going to be Avice scheduled when an event like that may occur what you do is basically you put the time or how often you want the event to a curb you put the full command in and then basically you save the file and then that will run so if you say every minute or every hour or every Wednesday that type of thing basically that command will run on that schedule and so this can be very important in the Linux world again when you're using a server a lot of times you do things such as you cache files or possibly you need a database optimized or even again you need an anti-virus software to run every once in a while and so using the cron tab will allow that to happen this can also be very important if you're using off-the-shelf packages on your linux server so let's say you're using some kind of let's say content management system is something like that they may require that you use the cron tab in order to schedule certain events to happen so that the content management system will be able to work effectively now when you start dealing with crontab the thing that I want to tell you the thing that I want to tell you is crontab is incredibly simple unfortunately it was designed by Linux people and so what should be an incredibly simple thing what is incredibly simple turns out to be a bit of a mess when you actually go and you try to actually use it in the real world so one of the things that I do want to warn you whenever you're using the crontab is that when you create your cron tab tasks that they will be created for each user that you have on your linux system so if you have multiple users on your Linux system you can actually have multiple users with multiple different crontab files so whenever I look at my system so right now I have a user named Bob I have the user named Tim and then also you have su do basically you have the root user so whenever you go and you create these crontab tasks they will be under each individual user account so this is one thing that you may be thinking about in the administrative world is possibly only using one user account for the cron tabs that you create the other thing to remember is that when you're dealing with crontab events is that they will they will operate under the privileges of the user that you have created them for so if you use sudo cron tab and then you put in the command then that then the event that you have happened will have root level permissions if I use BOM or if I use Tim for the examples that I'll show you then they will only have either Bob's or Tim's permission so this is an important thing to be thinking about especially when you go in there let's say you're trying to clean out a database you're trying to have events happen on your system if you simply do the cron tab for your user account you may not have the elevated permissions that are required in order to do what you want done so what I'm going to show you today is how to edit a cron tab file so if you want to have the cron jobs run as you you do cron tab space hyphen e for edit and so then whatever you add it whenever you add that will be based off of your account if you want the root user to be able to have permission to basically do the tasks or basically you want those tasks to be run under the root level permission you would actually do sudo crime that crontab space hyphen e and then that will put you into the root level and then whatever you edit in the crontab basically that will allow that to happen as the root user account so those are a couple of things that you need to be thinking about whenever you use cron to have again crontab itself is is simple but then you get anna's mess of how it was designed by the Linux folks so with that let's go over the computer so I can do a demonstration and basically show you how crontab works in the real world so here we are my about to server again I'm using eighteen point oh four point three LTS so what I'm showing you today should work fine on Ubuntu I do have a say when you're using a cron tab there may be differences with the different distributions so if you're having some problems you're using sent OS or something else you might just have to do a little bit of a google search just to verify what's going on as always welcome to the Linux world so we're gonna log in Bob I gotta do one two three four five six we're going to hit enter and now we're logged in and we're gonna hit clear so from here the first thing I want to show you is just how bad how horrible Linux coders are the people who that develop these products for Linux I want to show you how nasty they can be so the command that you're gonna use to actually edit the chrome type file is crontab space hyphen e but what if you're tired or if you forget and you just put in crontab all you do is put in crontab right how bad can that be for you well if you hit Enter what you notice is you're gonna get into this what is this this is this you're like okay no big deal I'll hit Q no I shift Q no I escape no and you're kind of just stuck in whatever the hell this is so this is one of those little booby traps the people that coded Linux will lead for the dumb people basically this is this is what tries to get people to give up on Linux and go home because you do a stupid little problem and then you're like I don't know how to get out of this so in order to get out of this what you do is you press down control you press down X then you press down C and then as you can see and that gets you back to the command prompt so control X C if you accidentally just do crontab and the cat I can't uh I can't say enough oh those linux people are just evil they're just evil in a way so anyways now let's go and actually taking a look at a real crontab file so again all the chrome tab files there are individual tab files for each user so I'm logged in as Bob so I'm going to be looking at Bob's crontab file I'm going to do - e for edit so crontab the space - e for edit I hit enter and the first thing that's going to do is going to ask me what editor I want to use so whenever you set this you actually have to reset it so basically this is going to become the default from now on there's a command that allows you to reset it so you should think about this so you can use Nano if you just press ENTER it'll go to Nano you can use vim basic vim tiny or edie I've shown you folks how to use vim so I would argue you should use them again that's what you know use what you know so I'll press - but you can use one of these different editors then I'm going to hit enter and now I am editing the cron file getting all the basic information up here and then the command is down here so what you're going to do is you're actually just going to type out a whole list of these commands with whatever time you want them to run so the first part of the cron let me take them let's take a look at and this is the part over here that's in yellow so all of this in yellow this is essentially what command you would type into the command line for what you want to have a curve so what I have here is I using this command called touch so it touches a weird little command what it does is it can either create a file or it when it touches a file basically all it does is it modifies when that file was edited so is this a simple way to make sure you can connect to a file or touch a file or make sure an events happening so all I'm doing here is I'm saying touch this file called cron test in the Bob folder in the home directory so all its gonna do is basically it's gotta go in there and it's simply going to modify the last time the file was modified so every what I have set up here basically every minute what's going to happen is it simply going to tuck in that particular file and it's going to change the modified time just basically a simple little thing so what you're gonna do is you're gonna type in the full command here so if it's a if you're gonna run antivirus software so you would type in you know the command for the antivirus software all the options all the arguments if you're going to clean out a directory you would type that in here so basically what yellow is is whatever command that you would type into the command line that's what you put here then in front of that what you have is this is the actual timing so when the willness event take place so these are all stars so since they are all stars what this means is every single minute it's going to touch this cron test file so the first star here so basically star is a wild card it means every so the first star here represents the minute so minute so you put in you can put anywhere between 0 0 to a 0 59 so what minute do you want things to happen so you if you want everything to happen on like the tenth minute you would put 10 here if you want everything to I guess Oh 9 there if you want everything to happen on the 19 or the 20th minute you would put 19 there so this is you put what minute you want something happen then you put the hour so one hour 24 hours in a day so 0 through 23 you will put what hour here so if you want something to happen at let's say 1 o'clock in the morning or something like that you would put that number here then pass that you have the day of the month so however many days on a particular month you'd have the day there then you would have the month here so whatever month it is and then the final one is day of the week so if you want something to run let's say every Sunday you could put a number here so these five numbers these five numbers remember represent the timing for when events should occur so there's a nice little website called cron cab guru and this site makes it very easy to be able to figure out what the timing should look like so currently here they give an examples of five for star star star and it says basically at 4:05 every morning so this is the hour so four and then five is the minute so basically at 405 and then it's every day of every month and every day of the week so every day at 4:00 or 5:00 in the morning this event will be triggered we click on random here so this gives you you know different things to show you so basically what this says is at the fifth minute so basically right after men so at 12:05 in the morning so 0 would be midnight so 1205 in the morning basically every day in August so this is a month in August this event will occur click random again and so we can see at 2:00 o'clock our 2:15 in the afternoon on the first day of every month this event will occur and so it's a cool part about this is you can go in and you can tweak with the different numbers you can play around and you can figure out what that timecode should look like and this also gives you some information below telling you different things so star equals any value you can value list separators so you can have something happen at the 10th minute at the 20th minute the 30th minute a range of values new step values and it also shows you allowed values oops and one of the things here with the allowed values is due notice it's 0 to 59 so for this counting 0 equals 1 and 59 basically equals 60 so basically just remember that when you're dealing with it again if you look at the hour it gives you the same types of things if you look at the day again allow values is 1 through 31 for their months you know 1 through 12 or January through December and day of the week you know some through sat or the different days for the days of the week so you can go through here and you can play with it to see how well it works and then well all you can do is then you go back and basically whatever time code you figure it out you plug that in here and then after that you put a space and then you put the the command line command that you want to use and then you go away you go now if you're not sure of what you're doing your students still a little bit new with this I would I would recommend you use a command such as touch so literally all touch does it simply touches this particular file just to change the modification timestamp because if you screw up the timing or whatever and you're doing it like a real command so we're getting cleaning out a database something like that and you do something stupid you might actually cause a real problem for the server so that's basically how this works now again if I'm sitting here and I'm editing this so if I so I I can go into insert I can come down here oh I don't know let me just edit this so I'll just put that to one so cron test one then I'll do escape and then I'll do : and I'll do W for ready to say if Q for quit and then I get out of this I can then clear the screen we do LS space - L and we can see you know I have cron test here and the last time it was touched it was at 3:25 and so in a little bit we shall get the cron test 1 now to be clear so that cron tab file that I showed you is only for Bob again so not so basically all users can have their own crontab file but again when the crontab events occur they will be used they will have the permission of whatever user account you're using so if you want to have things with let's say a root level permission you would use sudo and then you would do cron tab space hyphen e and then if we open this up on two three four five six we can see that this particular file is empty so the sudo crontab file is empty and so you could come in here and you could put in events that you want to occur under that root level permission now to get out of this I simply do : the queue because I'm using them and now I am out clear now I should be able to do LS - hello and there we go so basically we have that touch command so after a minute it ran and so I now set it to do cron test1 so it touched a quote-unquote cron test-1 file and it created it at 1526 so that's again basically when we're talking about crontab that's what we're dealing with with with this particular tool now there's a couple of other things that you can do with this so if you want to see like the crontab files for either your user account or another user account you can do cron tab space - L and flips so this will show you the crontab file for your account that your yes and we can come down here and we can see so I can't have not edited in it I'm not modified I don't have to worry about that so I can see okay this is the time doing touch Bob a home Krong Krong test one so I can see that so that's useful and then you can also look at the crontab files for other users so let me just do clear again so I have the other user named Tim that I've used before so let me just make sure I have the rights to actually look at the the user contact file so you do sudo then I'll do cron tab a new space then - you for user space and so we have this Tim user account space L so this will show us the crontab files for the Tim user account I hit enter and then I can see here so I have a ton at every minute of the day going to the home Tim directory cron test Tim so basically every minute this cron tests Tim file is being touched and so that's a way that you can go through again you can try to see what other crontab files there are and what other cron of ents may be occurring this can be one of the reasons why trying to deal with with chrono vents can become a little bit complicated in the linux world if you have multiple user accounts and you create multiple crontab files amongst all those user accounts it can become very difficult to keep track of everything so again just one of those things to be thinking about now as I talked about before once you set your crontab editor it will remain whatever it is you set it to so if I new crontab space - e to edit you notice we now drop into them so we set it to them and now it's vim but the question has to be as well what if I don't want to use vim or what if I want to use them but I accidentally selected Nano instead so in order to to reset the editor all you have to do is use a basic command to be able to reset the default editor so you're going to do RM space then what you're gonna do is you're gonna do that little squiggly mark then you're gonna do forward slash dot and then selected underscore editor so basically this command right here will remove the selected editor that you're using for for crontab you hit enter it doesn't really tell you anything but now if I want to do crontab space hyphen e what you'll notice is you now are able to select your editor again so again if you come in here you're not thinking about it and you hit the enter key so you hit the enter key and now you're using Nano like wait but but Eli taught me how to use vim I don't know how to use this thing by using that that remove command that allows you to reset the default editor and so that's the basics of crontab and it's really one of those things you just got play around with a little bit the only really difficult thing is just making sure you get the timing correct you know how often you want an event to occur and then also making sure whatever user account you're using to do the crontab events that it actually has permission to do whatever the hell it is you're trying to get done but with that that's really all there is to it so there you go that's how to use cron tab again in the Linux world this is one of those things that's relatively easy to use so long as you experiment and you play a little bit so you get an idea of what is happening and as long as you understand that the people that code for Linux are evil they are evil evil people and they really do some things that are just where you sit there go really really you can't just fail out you can't just have this you know I typed in something wrong just can't fail that out in a reasonable fashion you have to leave me at whatever it is you get when you type in cron tab and nothing else and so this is one of those perfect examples of where I want to show you where a Linux itself is not generally that difficult what it is is how things have been developed how things have been coded they are so it is such one of those things where you have to be precise and if you don't do everything absolutely correctly then you can get lost really quickly and everything can turn into a mess and that's why frankly a lot of people give up now again when you're doing the crontab something you think about the cron tab again there are different user accounts for for the crontab files so again you can have a Tim user account or ten Tim crontab account and a Bob crontab account and a sudo crontab account so something to be thinking about is when you're building out your server really think about how many users you want to be able to set things like crontab events to happen do you really want that many people to be able to do cron jobs or do you want that for only a couple of users based off of permissions that's one of those things to think about again with a lot of this stuff in the Linux world there are far more commands out there there are some interesting things that you can do with cron jobs and be able to look at different types of things but I didn't show you them today because I don't want to overload you with too much information I want to keep this pretty simple for you and so the main thing is just figuring out how to deal with at I mean how to make sure the command actually does what you think the command is going to do and then figure out the different user accounts that will have different crontab files again it is very important in the real world if you're gonna be doing this in a production environment to get to to make sure whatever command you plug in is the specific command that you need to plug in if you type a file name wrong if you don't put a space in there right every there's a time and then it's gonna run that command so if you screw up that command that can cause you a lot of major issues if you remove a directory or you remove files from the wrong directory you know that might end up crashing your server so just check and recheck a few times make sure the command that you're plugging in is what you want to have occur and so with that that's really all there is to it to cron jobs in the cron tab file as always I enjoyed doing this video and look forward to seeing on the next one
Info
Channel: Eli the Computer Guy
Views: 20,253
Rating: 4.7570095 out of 5
Keywords: Eli, the, Computer, Guy, Repair, Networking, Tech, IT, Startup, Arduino, iot
Id: Cb-L3LKHET4
Channel Id: undefined
Length: 21min 21sec (1281 seconds)
Published: Tue Sep 24 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.