So let's take a look at our tab again.
Let's go Chrome tab with the E option and we've opened it up.
See, it's remembered that Nano is our editor.
And just so just in case you're interested, if you go on to your home directory and you look at your,
your all the files with LS Dash a there's a hidden file here called selected editor and it's that file
that stores your preference for for editor.
So in here, if we look at dot selected editor, you can see you can change that if you want by putting
the path to the editor that you want to have.
Or alternatively, you could just type the select editor command and it would give you the choice again.
But I'm going to keep it to Nano and leave it like that.
But that's how if you wanted to change the editor later on, maybe you get better with another command
line editor.
That's how you can change it.
So let's go on cron tab dash e and now what we're going to do is we're going to look at some more advanced
options and more advanced ways that you can do some scheduling.
We can add as many rows as we like in this cron type and they'll all work exactly the same.
So I'm going to make a new row and look at some more advanced scheduling opportunities.
So for example, let's say I want to run something exactly at the hour and 15, 30 and 45 minutes past.
Okay.
So I don't just have to have one option here.
I could have zero, comma, 15, comma 30, comma, 45.
That's going to be my first column.
Right?
So this is going to be the minute I can have a zero past 15 minutes past, 30 minutes past and 45 minutes
passed.
Notice how I've got commas but no spaces.
You're not allowed to have any spaces because spaces separate the columns.
So that would be like my first my first column.
I could then space this stuff over.
So I got a bit more space and now I could say, okay, run it at every hour, every day of the month,
every month, every day of the week.
And then whatever command I wanted to happen here.
So something like Hello Echo, hello, for example.
And this would run at exactly as the hour turned.
And 15 minutes past the hour.
30 minutes past the hour and 45 minutes past the hour or quarter to the hour or I could just say more
simply run every 15 minutes and the way I do that is star then a slash 15.
Now that says run every 15 minutes, I could say run every 5 minutes by star slash five or every 3 minutes
star slash three every 7 minutes star slash seven or star slash 15, like I said, for every 15 minutes.
But what about what if I wanted to run every 15 minutes, every three days?
Let's say?
So a lot of what I do is I'd keep the hours the same and I'd go to the days of the month option and
do star slash three now.
So every three days.
So let's say on the first of the month, the fourth of the month, the seventh of the month, and so
on, on those days it'll run every 15 minutes.
So you can see how this kind of works here.
What if I wanted the command to run once a week at 2359 on a Sunday, only in December and January 23,
59 on a Sunday, but only in December and January.
Well, how do we do that?
Let's make another roll.
We'd say let's to 23, 59.
So 59 is the minute.
The hour is going to be 23.
It doesn't matter which day of the month, it doesn't really actually the month does matter only January's
and December.
So January's and December's day of the week, only Sundays.
Okay.
We could say because we can.
Okay, so there we are.
Now what we've got 23, 59 every day of the month.
But so what this is going to do 2359 in January, in December, but only on Sundays.
So there we are.
And it's going to echo because we can have for example, now there's a great website called Cron Tab
Dot Guru that you can take a look at and play around with to see what different Cron tab expressions
mean and I'll put a link to it in the resources section.
It's awesome.
Okay.
But a great use of cron is to schedule automated backups of the file system.
So let's say we want to run weekly backups every Friday at 2359 for every month of the year.
So if I delete these, these examples here, delete these.
So 2359 on Fridays for every month of the year.
So we know that 59 is the minute we know the hour is 23.
It doesn't matter the day of the month like numerically, it doesn't matter whether it's the first or
the seventh, just every Friday of every month of the year, but only on Fridays.
And then we'd put the command, which is going to back up our stuff and, you know, that's how it would
work.
And but we know how to do this because we can make a script for it.
So let's save our cron tab so far just like that.
And you can see it's work 24 lines.
But if I actually look just by the way, while we've been editing that, if I look in the text, you
can see we've got so many more hello worlds being added to that as our cron.
Her command that we set before has been continuing to run.
So that's really cool, right?
It's automated and it's running.
In the background.
All right.
So let's set up the script that we want to run now.
You remember in the last few videos when I was telling you about bash scripts, we made a folder called
Bin.
And inside that bin folder is where we started putting our scripts.
And we had this one file called Backup.
So let's take a look inside that backup script again.
Well, in here, what it does is it compresses into into a archive on our desktop, every single one
of the contents of our documents, downloads, desktops, pictures and videos, folders.
And it also outputs some the error, like the standard error, like the, the log messages that come
from the command into the, the Bitbucket.
So they're just sent to, to the void.
They're no longer printed to the screen.
Okay.
So this is good.
This is this is going to do a backup for us.
Let's say that actually, instead of sending them to the desktop, let's say that in our in our home
directory, let's say let's say we made a directory called backups.
I spelled that wrong.
So if I rename backups with two S's into just backups with one s, there we go.
We've now got a folder called backups and let's say we wanted to save the backups in there.
So if we go to the bin folder and we edit our backup script now we can say, okay, go into instead
of our desktop, go to our backups folder and then save the backup.
GZ That's good and that's good for that.
But what we want as well is we want to also log the date and time that the backup was created into a
log file.
Now the way that we could do that is we could use the date command and we could append that data to
a file in our backups folder called, let's say, backup backups log.
Why don't we say that?
Okay.
So we're going to call it backups log.
And that's just going to keep in there the date and time that the last log happened.
So if we save that, actually, we could we could make it a bit more advanced.
We could actually now let's leave it like that.
Let's not make this any more complicated than it needs to be.
Okay?
So we're going to take that date and we're going to append it to our backups log file.
And you can see here that I'm using the double arrows to make sure that as this script runs, each time
it's going to it's going to add it.
So the file gets longer as time goes on.
Okay.
So we've now got this script that we want to run.
And if we've run it just we just run backup from anywhere.
If we run backup, you can see that.
Now, if I just put this back to a normal size, if we go into our backups folder, we've now got backup
star GZ and we've also got this backups log.
If I open with that with nano backups log, you can see that it's logged the date and time.
So if I just remove remove everything in here using RAM, you can see they've all disappeared.
So we're good.
We know that script works.
Let's try and add it to our cron tab.
So here we are.
We've said that we want every Friday at 2359.
We want that.
We want that script to run.
So how can we do that?
Well, the way we can run that is because it's a bash script.
We'll type the bash command.
So just bash like that and then we'll give it the path to the script that we want to run.
So in our home directory in the bin folder, we're going to run the backup script.
So that's all we've got to do, right?
We just type bash and then give it the path to the script that we want to run.
And then that script will now run at 2359 on a Friday.
But to save you waiting all the way till 2359 on next Friday, let's go ahead and make this run every
minute so we can watch it happen.
So look what I'm going to do.
Nice hack, hack, hack.
Okay, so I'm going to cut the line and uncut the line and then cut the line again.
So that's a nice copy and paste and I'm going to make this run every minute.
So if we go, we're going to change all of these to stars.
So there we go.
Now, that should run every minute now.
So if I write the file and close it, you can see it's installed the new cron tab.
Now in backups, there's nothing in here.
But if I look at the date, we can see that we've got about 40 seconds until this runs again.
Needs to be very excited, I'm sure.
So we've got about about 30 seconds.
So I'm going to cut the video here and come back closer to the time.
So here we are.
Let's take a look.
And it's just gone past the minute.
So let's take a look at our backups folder and hooray, we have indeed run our script correctly.
And if we look inside the log folder, if we look at backups log, we can see that it ran at just past
the third minute of today.
So the reason that it runs at just the minute past, it's just like a like, you know, there's just
1/2 error.
I think it's just a clock thing.
It's, you know, the computer can only process things at a certain time resolution.
And 1/2 is really not that much of a bad error.
Okay.
So it'll you'll tend to see things like this.
I think it actually does run at exactly the time the minute goes, but the date command takes just about
a second or so to run or something like that.
So there's a slight delay, but really 1/2 is not bad at all.
So now we have indeed ran a command.
And look, it might it might even happen again.
It might even happen again.
So if we're at four more seconds, one more second.
Yes.
Now we look inside our backups log.
Backups log.
We can see that another one is run again just at 1/2 past the fourth minute.
So now we are making backups every single minute.
But that might be a bit of a a bit of a a bit of a burden on our computer here.
So let's just go ahead control and K to delete the line and then we can save our cron tab and leave
it so that it just runs at 2359 on Fridays.
So there you are, guys.
How awesome and how epic is that?
So I'm sure you can see the power of combining together bash scripts and cron scheduling to control
whatever you want about your computer.
Now, these two skills together are incredibly powerful combination, and you now have the foundation
in how they work.
So let's have a quick recap of what we've learned in this video.
So in this video, you took a look at how to use cron to schedule when commands and scripts should be
run.
And you learn that every user has what is known as a cron tab and that you can edit cron tabs using
the cron tab command with the E option with the E standing for edit.
Now cron tabs are text files that are broken up into many rows and you can have as many rows as you
like.
And each command or script has its own row and each row has six columns.
The first five columns are scheduling information and the sixth and final column details which command
or script should be run at that scheduled time.
So the columns are for, for the scheduling columns.
You've got the, the, the minutes of the hour.
So for example, if you put 15 in that column, the command would only run at the 15th minute of each
hour.
So things like 11, 15 to 15, three, 15 and so on.
Then you've got the hour itself.
So that's a number that you can put in 24 hour clock time.
So from 0 to 23 you've got, then the third day of the column is the day of the month.
So again, from one to the end of the month, whatever that might be, the fourth column is the month.
So that could be numbers with one being January and 12 being December.
Or you can just type in capital letters the three letter version of the month.
So Jan for January, DC, for December and so on, and the fifth column is the day of the week, so
that could be 0 to 6 with zero being Sunday and six being Saturday.
You could also enter seven as Sunday again if you wanted, but a good way to do it is just to enter
the day names.
So Mon being Monday, soon being Sunday.
So three letter day names.
And you also saw some advanced ways to to schedule this stuff as well.
And a great place to look at these these scheduling things is to look at a website called Cron Tab dot
guru.
I'll put a link to that in the resources section.
Um, because it's a great website for you to practice setting up these scheduling conditions, and it
also will be useful for you at some point in the future.
And as I should mention as well, columns can be separated by as many spaces as you like.
All cron wants is that you separate each column by at least one space, but feel free to format each
rows however you like.
And you also saw in this video how to use cron tabs and batch scripts to schedule automated backups
of your home directory, which is an incredibly useful thing to make sure that all your files stay safe
and it built upon what you learned in the last section about file compression and archiving.
So that's super awesome as well.
Now the key thing that I want you to remember from the last two videos is that in Linux, practically
anything can be edited using the command line.
Now, since you can write bash scripts to execute these commands in a controlled sequence and then use
Cron to schedule when these scripts should be run, you have the necessary toolkit to do whatever you
want on your system whenever you want to do it, which is a crazily powerful combination.
All you need now is a bit of imagination to decide what you might like to do now.
Batch scripts are a whole massive topic in and of themselves, so we only really had time to scratch
the surface and cover just a small bit of what they can do in this course.
But at least now you have an understanding of what batch scripts are, how they work, and where you
might use them.
And if you'd like me to create a specialized course on just bash scripts, then send me a message.
And if I get enough of you saying that you want that, then I can I can potentially put that together
for you.
But for what we've learned in this course so far, I want to make sure you get some practice in using
it.
So in the next video you're going to be getting the brief for your next project assignment.
Now, it will be a rather simple and fun one this time, but it allow you to go off and try writing
batch scripts and crosstabs for yourself so you can get a hands on understanding of how these incredibly
powerful skills work.
So for the brief of your next assignment, I'll see you in the next video.