Linux Crash Course - Scheduling Tasks with Cron

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] you know sometimes i wish human memory was as good as computer memory at least in terms of me i forget pretty much everything and i need constant reminders to complete a task it's just a personal thing and i think all of us are forgetful to some degree so we definitely don't want to be reminded to do a backup because we might forget to do that we want to use something that's going to automatically do a backup or automatically complete a task for us so that way we don't have to remember to do it because let's be honest sometimes life gets in the way and we just don't get around to it in today's episode of linux essentials we're going to go over cron crown allows us to schedule tasks in advance and it's very powerful and definitely recommended because you should absolutely learn cron it's very important so in this video i'm going to teach you all about kron so let's get started alright so let's get started with setting up cron jobs as i mentioned in the intro of the video cron jobs are basically scheduled tasks you want something to be done at a specific time and you probably don't want to set an alarm at three in the morning to run that backup job that you plan on running you want to be able to schedule that and have it run when you are not at your computer or server that's exactly what a cron job will allow you to do now the first thing to know is that every user on the system will have their own set of cron jobs and a list of crown jobs for a user is considered their cron tab so the first command that i'm going to give you is crontab dash l just like that and at this time i don't have a cron tab and what that means is that my user has no cron jobs at all i have no schedule tasks now to set up a crown job i can run a different variation of the crontab command crontab dash e e for edit i'll press enter if i have multiple text editors on the system it's going to ask me which one i would like to use for the editing of the cron tab i'll just choose one i'll press enter but you can choose whichever one you'd like and here we have a default cron tab the actual file that we're editing is listed right here at the top it's actually a temp file so with the cron tab dash e command we're not directly editing the cron tab it's first going to have us work on our changes in a temp directory and then assuming everything is good it's going to copy it to the actual cron tab and here at the top we have a bunch of comments and these comments in particular will help you understand a little bit better what each individual field is and how you set up a crown job but you don't really have to read all this right now because well you have this video and the whole point of this video is to teach you all about it so what i'm going to do is go to the very end of this file and then on the first blank line i can go ahead and add a new crown job and a cron job actually consists of six fields and i'll just add a bunch of white space here to center the screen a bit more and i'll show you how the fields break down so what i'll do right now is type out a brief summary of what a cron job will look like so i have five asterisks and then the word command now this isn't really a valid cron job because i don't have a command command but i'm just showing you the way that it's laid out we have a legend up here it's probably the only thing in this file i might point you to because the whole point of this video is to teach you everything you need to know but what we can glean from this is what each individual field represents one two three four five and six and we have the same number of fields right here so the first field refers to minute we can actually set them all to asterisks and that's okay i'll get into why that's okay in a moment or we can change one of these asterisks to a number if we want to be more specific the first asterisk actually refers to minute so for example you could put a five right here and that refers to a minute of five and the number that you set here can start at zero and be as high as 59. basically minutes zero through 59 so right here we have a minute of five so when is this particular command going to run well right now it's actually going to run never because this isn't a real command and this isn't really a very useful cron job because all it's doing is executing the echo command which doesn't really do much but this is a valid command so there's that so anyway we have five here for the minute field and we have an asterisk for the hour field for day of month the actual month itself and the day of the week all of those are said to be asterisks so in this case since these are all asterisks going forward hour in this case means every hour every day of the month every month and every day of the week so in this case what that translates to is every single hour of every day during any month any day of the week it's going to execute this command anytime the minute field of the current time equals five so 105 205 305 and so on as we go to the right we can actually restrict this a little bit more so what i can do is change the hour to nine now what's going to happen is that this particular command is going to run every day at 905. it doesn't matter if it's the 15th of the month the 24th it also doesn't matter if it's june july or august nor if it's friday saturday or sunday it's always going to run every day at 905. if i change the day of month which is this field right here i could change that to the 15th so now this particular command is going to run every month on the 15th at 905. and then the next field we have the actual month itself this could be anywhere from 1 to 12. since this field right here is for the day of the month it could be anywhere from 1 to 31 and in this case there's only 12 months in the year so this could be anywhere from 1 to 12. so if i was to set that to 8 for example so now that means this particular command will run actually once per year on the 15th of august at 905 and then here this field is again the day of the week and this number can be anywhere from zero to seven and specifically sunday can be written as zero or seven either one is valid so if i was to change this to five that refers to friday and now this particular command is very restricted because now it's only going to run on the 15th of august at 905 if the 15th of august falls on a friday which actually means most years this won't run at all but any one of these fields can be changed to an asterisk and again they can all be an asterisk that's perfectly valid and when it comes to a crown job asterisk means any so this particular command if we were to save this is going to run every minute of every day doesn't matter the day of the week it's going to run every minute and you could customize this any way you'd like so for the hour for example you can set it to 11 a.m an hour can be anywhere from 0 to 23 is military time so 11 in this case means 11 am so this is going to run at 11 am every day if i leave this at 11 as you see here i could skip all the way over here to day of week i could change it to thursday which is four so every thursday at 11 am this command will run i think you get the idea now we can actually simplify this even further and what i'm going to do is take out all of these right here and then what i'm going to do is type at and then hourly i'm not going to bother filling in any of the fields that i showed you earlier i'm just going to leave it like this at hourly and then the command this is an easy way to set up something for hourly daily and so on by just including at and then hourly daily whatever it happens to be however often you want it to run and then it'll run the command but that's not all we can do you can also change this to reboot which means every time the server reboots it's going to execute this command and that's really helpful because if we were to follow this format right here there's really no way to set that to reboot we can set the time the day all of that but this just doesn't give us the ability to set it up for reboot but we can actually type right here at reboot so every time the server reboots like i mentioned the command that you have here following is going to run now what i'm going to do is give you a more practical example i'm going to close out of this i'm not going to save my changes i just held ctrl i pressed x i'm going to press n for no and it says no modification made so if i was to run crontab dash l again there's no crontab but there's another more important thing that we want to take into consideration most of the time you'll probably not want to run a cron job as your user when you execute crontab dash e that's allowing you to set up a cron tab for your actual user or the user that you are currently logged in with if you don't specify a user it's just defaulting to your user i don't recommend running cron jobs as your user i mean it's technically correct you can do it it is valid it's better to segregate commands to a specific account an account that matches your purpose for example maybe you have a web server apache or nginx for example and when it comes to ubuntu and debian that gives you the www data user and on most distributions having a web server installed gives you a web server specific user so if you want to perform a task on your website you should probably use that user but how exactly do you tell the crontab command that you want to edit the cron tab for a different user that's actually simple we provide dash u and then the user name for the user we want to edit the cron tab on i'm going to edit root in this example i'll press enter and of course it tells me that i can't do that because in order to edit the cron tab for a user other than your user you will either need to be logged in as root or have access to sudo so i'll give it sudo i'll type in my super secret password and again it's going to ask me which text editor i would like to use i did set this up a moment ago but this time i'm using root to edit the cron tab because it's roots crown tab i want to edit in particular and i haven't done that before so i'm selecting the preferred text editor for that user and i'll just leave it at the default of one i'll press enter and then we have the same screen right here so what i'm going to do is i'm going to give you guys a sample command in this particular example i'm going to have it run every minute this isn't going to be the most practical example but it is something that's going to make a change to the computer and that's what i want you guys to be able to see and i'm just going to include an app install command here i'm going to have it install tmux so i'll hold ctrl and press o and then enter to save the file ctrl x to exit out it tells me that the cron tab has been installed and as you can see i don't have the tmux command available so what's going to happen is that when this runs it's going to install tmux for me and since i set it to run every minute as soon as the minute changes it's going to run and that's going to happen very soon isn't it it's already happened because when i ran the date command we were two seconds away from it being the next minute and before i go and see whether or not tmux was installed as an aside i can run sudo crontab dash u root and dash l for list and we can see the cron tab down here so that's a good way to see which crown jobs were installed so crontab dash l again is how you list which cron jobs a user has and if you execute crontab dash l without even specifying the user that you want to check it's going to check your own user or whatever user you are logged in with but in this example i designated that i want to check the crontab for root in particular and that's what it did so moment of truth we now have tmux i promise you i didn't install it i didn't pause the recording and then go behind your back and install this package the crown job for root ran and installed this package for me again this is the cron tab right here and it ran this command to be double sure of that i can run sudo and then cat i want to check the contents of a file i'm going to check the syslog and i'm going to grep for cron so we can see some output here that tells us that it did at least try to run the crown jobs now my user doesn't even have one so i wouldn't have found anything to run and to prove that i didn't install it what i'm going to do is i'm going to cap the contents of the syslog and if kron actually did something i should be able to grep for it and in the log it should show cron in all caps if it did run so i'll press enter and we can see right here the cron tab for root it did run and it's wrapped here but this is the command that it ran apt install dash y tmux now this isn't really a good example though because it's going to try to install that package like every minute and it's only going to need to do that once and then it's never going to be needed again so here's the command that we used to set up that crown job i'll press enter we should probably go ahead and delete it in all honesty it probably would have been a lot easier to just install tmux manually rather than just set up a crown job for installing one single package but you know what it was a quick example and it did work so what i'm going to do right here is have the crown job execute the date command and i'll have the output of that command redirected to a text file so i'll save the file exit out i'm going to switch to root and right now we don't have that file we do have that cron job though but according to this that cron job should actually create that file and it looks like it just changed here so i'll list the storage and we have the date and there's the file so what i'm going to do right now is just wait to the next minute i'll just go ahead and edit out the next chunk of time and then we'll check it out so it looks like enough time has passed and as you can see it added another line right here and it's going to keep doing this forever now i'm going to go ahead and remove this i'm going to execute crontab dash e because after all root is the cron tab that i want to edit and i'm logged in as root so i don't need to specify the user and i'll just go all the way to the bottom i'll get rid of that line i'll save the file and now root has no crown jobs at all let's see a more practical example what i'm going to do is run apt install i don't need to use sudo because i'm logged in as root i'm going to install apache 2. i'll just press enter to accept the defaults and now we have apache i should probably log out of root i don't want to leave that session open so i'll hold ctrl and press d to disconnect from root i'll go to a new workspace here i'll bring up a browser and then i will browse to localhost after all i do have apache installed now so i should get the default apache start page which i do so now that i have apache installed i should have a web server user as well and to prove that i will cut out the contents of etsy password i'm going to grep for www i'll press enter and sure enough we have the www data user that we see right here so if your server is serving a website then there might be a cron job that you might want to run maybe you're backing up the website every night or the website itself has some sort of php script that is running every day there's all kinds of reasons why you might want a cron job especially when you have a web server so what we're going to do is add a crown job to the www data user so let's go ahead and switch over to that user right now and right now it's telling me that this account is not currently available i did that on purpose i knew i wouldn't be able to log in and the reason why i'm not is because the shell for that user is set to this right here no login that's very common a system user by default can't log into the system and that makes sense we really don't want anyone to log in as www data if someone was able to break in and log in as that user they could actually delete our entire website they can insert some malware they could do all kinds of things that we would rather they not do but even though i can't log into this account i could still add a cron job for this account so i can run sudo and then cron tab dash u for user and then dash e because we want to edit the crown tab i'll scroll all the way down and we can go ahead and add a crown job for this particular user so for a hypothetical example i'm going to set the minute to zero the hour for three for three am i'll set the rest to asterisks and then i'm going to set up the command this is just a hypothetical example this particular script does not exist on the server but i wanted this to look more realistic and this is definitely something that you might see in the real world where someone has a backup script running in this case i'm running this particular script every day at 3 am i know that because the minute is zero and the hour is three doesn't matter what the day of the month is what month in particular it might be or the day of the week none of that matters so essentially we want this to run every morning at 3am now notice that i included the full path to the script right here and that's very very important anytime you are using cron you should always include the full path to the executable or script that you are running and that's an important distinction because if i actually did have a script right here websitebackup.sh if that did exist on the command line to execute this manually i don't have to actually type out the entire path i could just simply type the name of the script and that's good enough and the reason why that works is because your path includes user local bin by default and your shell already knows to look there for commands and that's why you're able to type commands without typing the entire path to that command but when it comes to cron the user that you are running your cron jobs with may not have a full path available maybe user local bin is not a part of that user's path maybe their shell isn't even checking the path at all in that case it wouldn't even matter but if you include the entire path to the command then it's going to work no matter what and some distributions do this better than others i've seen on red hat for example that they often don't include the path for virtually anything so in that case if you are using a red hat or red hat based system you especially want to include the full path dubbing and ubuntu they do a better job but still it's better to be explicit and type out the entire path that way there's no confusion and also the interpreter that's running the crown job won't get into a situation where it doesn't know where to find the script you're trying to run you're typing an entire path and knows exactly where to go now i'm going to exit out of here i'm not going to save it after all that particular script does not exist but i think you get the idea you now know the basics of setting up a cron job now at this point before i close out the video what i want to do is actually give you guys a website that you can use to generate a crown tab line so that way if you're having trouble remembering the fields and what they're for this will make it a little bit easier for you as you get started so here in my browser what i'm going to do is i'm going to type the url to the website in particular and it's crontabgenerator.org and there's actually other websites out there as well that'll allow you to do the exact same thing so you might even find a better one but this one seems like it works out okay you just go ahead and click through these fields here so in this example i'm going to set the minute to 10 the hour to 3 a.m i'm going to leave days as every day and for months i'll leave that alone weekday i'll leave that alone scroll down i'll type in the path to the script just like that and then i'll scroll down and right here i have a selection to mute the output or to save the output to a file i'm just going to mute the output i think that's fine and right here it actually gives us the crontab line that we would paste into our crown tab editor to basically accomplish exactly what we were looking for and this is going to run the script right here the website backup.sh at 310 every morning and right here we have something that i didn't go over yet and what that's going to cause it to do is mute all output and it's going to run this particular cron job silently output redirection is not something that i've gone over in this series just yet depending on when you're watching this i may already have a video on that just check the playlist but as of right now output redirection is beyond the scope of this video just keep your eye on the playlist for this particular series and you'll see it there as soon as that's ready so by now you should have a decent understanding of cron how to create your own cron jobs how to understand the syntax of a cron job things like that and even if you don't just give it a little bit more practice and you'll definitely get the hang of it kron is awesome i just love scheduling tasks ahead of time and also i love automation too and kron is just the beginning so let me know what you guys think of this video i look forward to hearing what you guys have to say and be sure to share this video with your friends and colleagues that help spread the word and i would really appreciate that anyway thanks for watching [Music] you
Info
Channel: Learn Linux TV
Views: 36,111
Rating: undefined out of 5
Keywords: cron, crontab, cron job, linux crontab, command line, linux tutorial for beginners, linux tutorial, linux essentials, linux schedule, cron jobs, linux commands, crontab tutorial, crontab command, linux cron job, cron tutorial, ubuntu cron, linux tutorial 2021, linux crontabs, linux, linux cron, tutorial, linux automate, cli, command-line, cron job linux, task scheduling, linux cron job tutorial, learn linux, cron job tutorial, crontab in linux, crontab file, cron utility
Id: 7cbP7fzn0D8
Channel Id: undefined
Length: 19min 25sec (1165 seconds)
Published: Thu Sep 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.