Hello, you beautiful people.
And welcome to this video where you're going to take what you've learned about scripts in the last few
videos and supercharge it by using Cron to make those scripts run on a schedule.
It's going to be super awesome and over the next few videos you're going to make some batch scripts
and schedule them to run automatically.
And we're also going to top it off by showing you how you can schedule automatic backups of your files.
So epic, right?
So by the end of the next few videos, you're going to be able to automate your workflow by scheduling
your scripts to run wherever you want.
And this really does open the doors to untold awesomeness going forward.
So let's go ahead and jump right into it.
Okay.
So Cron is a command line based program that is used to schedule tasks.
Now Chrome gets its name from the Greek word Chronos, which means time, in case you were wondering.
So Cron actually has quite a good name for a scheduling program.
Right now each user has what is known as a cron tab, which basically is just a text file, and each
cron tab lists which commands or scripts will be run automatically by that user.
And it also lists when they will be scheduled to be run.
Now to edit the cron tab for your user, you just open a command, open a terminal and type cron tab
dash E and the E option is for edit.
And when you open it you'll see that the cron tab begins to open.
But if this is the first time that you're opening up a cron tab, it might ask you what editor you want
to use.
So we're just going to press one for nano and press enter and we will see that now we've opened up our
cron tab in Nano.
Now the first thing you'll notice is that there's a lot of text in here and this is a kind of introduction
to how cron tabs work.
But these are commented lines and what I mean by that is they are not interpreted by the CHRONOTYPE
as actual valuable information.
And you can tell that because they start each line with a hash and that allows, you know, people to
write what they want in a file, like some notes, but it's not interpreted by the actual by the actual
cron utility.
But down the bottom, each cron tab is each cron tab is broken up into rows and one row is for each
command or script that you want to schedule, and each row will have six columns.
So the first five columns are scheduling information, and the final sixth column is the command or
script to be run at that scheduled time.
So you need to get in to the bottom and to enter your information.
Okay, now this is really one of those things that's easier to be shown than explained.
So let me show you how this works.
So let's say that we want to run a command that just outputs the text.
Hello world to a file called hello txt on our desktop and we want that command to run every minute of
every day all year round because for some reason we just really like saying hello.
Okay, so to make this happen, we need to enter a new row in our cron tab.
And so here we are, we're on a new row and there are six columns in that row, each column being separated
by some blank space.
So let's take it step by step.
Now, the first column of the row is for the it's for the minutes of the hour that you want the command
to run on.
So let's go to the first column.
And we could put any number we want in here as long as it's a valid minute.
So we could type 15, for example.
Now, this would make the command run at 15 minutes past the hour.
So things like 12, 15, two, 15, nine, 15, seven, 15 and so on.
Okay, now the numbers for minutes can go from 0 to 59 because that's what numbers 4 minutes do on a
normal clock.
Right.
So you can go from 0 to 59 now because we want the command to run every minute.
We'll just put a star in there to say, run any minute now.
No, this isn't exactly like a wild card.
The star happens to mean any in this case, but crosstabs don't really support other wild cards, so
don't put symbols such as question marks or the brackets in there because they won't work.
The star is only what's accepted and it just means any value.
We don't really care.
So the first column is the minutes of the hour.
The next column is the hour number.
And we could put numbers like, for example, 11.
So this would make it the command or whatever.
We scheduled to only run the at 11 a.m. each day.
We could put 14 so that it would only run at 2 p.m. each day.
Now the numbers here are based on a 24 hour clock and can go from 0 to 23.
And we but we don't mind particularly what our command runs at.
So let's just put a star there as well.
But if I put, for example, an 11 in the in the hour column and a 20 in the minute column, then our
command would run at 11:20 a.m..
So the first column is for the minutes and the second column is for the hours.
And by the way, it doesn't really matter where we place these columns as long as there's some space
between them.
So we could like, you know, separate them out like that with more than one space.
But as long as there's some blank space in between each of the columns, Cron isn't too fussy about
how the about the how the stuff is laid out.
So let me just try and make it a bit clearer which column we're filling in here.
So there's the first column is minutes, seconds column.
Hours and so on.
I'm just going to kind of spread out our stuff a little bit here.
So we got a bit of a nicer layout, but we don't really care about the minutes, so we'll put a star
there.
We don't really care about the hours, so we'll put a star there too.
Now the third column is what we see here.
Dom is the day of the month.
So if we put a one here, then it would only run on the first day of the month.
And so, for example, if we had a 20 in the minutes column and we had in the Hours column, we had
an 11 and here we had a let's say a ten in the third column.
Then this, then whatever command that we gave it would only run on the 10th day of the month at 1120,
but it would run all of the months of the year, but only on the 10th day of the month at 11:20 a.m..
But again, we don't really mind what day of the month.
So we'll put a star there and we'll put a star in the other columns as well.
There we go.
So let's just lay this out a bit nicer.
Okay, cool.
Now, the fourth column is actually the month itself.
Now, in here, we can put numbers, so we could put one being January, 12 being December and all the
other months in between.
But you could also do it using text.
So for example, in capital letters, I could put Jan for January, February, for February I could
do May or Dec for December.
So three letter months can be used as well for for the month.
If you write them in capital letters.
But here we don't really care about in the month, so we're just going to put a start as well.
And so just to see how this is all coming together, if we add a 20 in the first column and we had in
the our column we had in 11 and in the day of the month we had a ten and in the month we had a June
for June.
Then this command would run on the 10th day of the month, on the 10th sorry, on the 10th of June at
11:20 a.m..
So that's that's how that works there.
But we're going to replace them again with stars.
So as I say, the the cron tab doesn't really care about the amount of space is just that each column
is separated by at least one space.
Okay, so, so there we go.
The next column is the day of the week.
Now the day of the week can have numbers from 0 to 6 with zero being Sunday and six being Saturday.
But as well, you can do this doing text using three letters for each day of the week.
So sun would be sun, mon would be Monday, THG would be Thursday, Saturday would be Saturday and so
on.
So let's say if we had a sun in there and the month was June and the day of the month was ten and then
the hour was 11, and then the minute was 20.
Then this would run at 11:20 a.m. on the 10th of June, as long as the 10th of June.
On on that year was a Sunday.
So you can see how you can become incredibly specific with how you schedule your commands to run.
But you can also be very general and very relaxed as well.
The cron tab, the cron utility is amazing for that kind of stuff.
You can be very expressive with your scheduling.
So there we go.
We're going to set those back to stars because we don't care what day of the week it is.
We want this command to run all day, every minute, all year round, so we don't really care.
So those are the first five columns and these first five are scheduling information.
Okay.
Now the final bit is actually where you tell it the command or script that you want it to run.
Okay.
So the sixth and final column is the command to actually run.
Now we said that we want a command that will output hello world to a file called hello text let's say
on our desktop.
So how would we write that.
Well we might write Echo then.
Hello World.
And then we'd append that to somewhere on our desktop.
A file called Hello Text.
So if I just expand the window here, you'll be able to see it a bit better.
There we go.
So you can see here that we're just saying this is just the command basically echo hello world and put
that on a file on a desktop called Hello Text.
But we're using the two arrows here to allow whenever this command runs it will append data to the file
so we can see extra data being added each time the command runs.
Okay, so when we save the cron tab, so cron will make sure that this command is now run at every minute
of every hour of every day of the month for every month, no matter the day of the week.
So this command is going to run every minute, all year round.
So if I press control in O to save that file and I exit, you can see here that we get a message saying
that it's installing a new cron tab and we can see here that hello.
TXT has actually just been has just been executed.
Okay.
So if we take a look, you can see that it actually just ran, right?
Then we can see that it's actually executed.
Hello world.
And put it inside inside the inside the file.
Now if I run the date command you can see that we've got about 40 seconds until this command runs again.
So what I'm going to do is I'm going to cut the video here and come back closer to the time when that
runs.
Okay.
So here we are.
We can see that it's just gone.
And literally as I press date, that's awesome.
It's just past the minute.
So if we look in there now, we should see that we now have two rows of data.
So fingers crossed.
Yes, we do.
We now have two rows of data in there because the the the the cron tab has executed again right at the
top of the hour and we'll just continue to wait until the next minute passes and it will continue to
run that.
It'll continue to run that that command over and over and over again and fill up this hello text file.
Okay.
Now, when when that actually runs, we'll see that more data has been added.
We've got about 18.
Also see if you can wait with me for like 18 seconds.
We'll see this happen.
Okay, so let's.
Here we go.
We're seeing another 11 seconds or so.
Um, and now if I, if I check here, now we can see that there's 5 seconds to go.
5 seconds to go.
2 seconds to go.
It should have done it now.
So if we look inside, yep.
It's run and it's added another row to the file and I didn't touch anything.
I promise.
So the Akron tab is working.
Hooray!
Okay, we've just made it run a command over and over and again.