1 00:00:00,150 --> 00:00:01,620 Hello, you beautiful people. 2 00:00:01,620 --> 00:00:06,300 And welcome to this video where you're going to take what you've learned about scripts in the last few 3 00:00:06,300 --> 00:00:11,490 videos and supercharge it by using Cron to make those scripts run on a schedule. 4 00:00:11,490 --> 00:00:16,590 It's going to be super awesome and over the next few videos you're going to make some batch scripts 5 00:00:16,590 --> 00:00:18,840 and schedule them to run automatically. 6 00:00:18,840 --> 00:00:24,180 And we're also going to top it off by showing you how you can schedule automatic backups of your files. 7 00:00:24,270 --> 00:00:25,350 So epic, right? 8 00:00:25,350 --> 00:00:30,480 So by the end of the next few videos, you're going to be able to automate your workflow by scheduling 9 00:00:30,480 --> 00:00:33,240 your scripts to run wherever you want. 10 00:00:33,240 --> 00:00:37,560 And this really does open the doors to untold awesomeness going forward. 11 00:00:37,560 --> 00:00:40,080 So let's go ahead and jump right into it. 12 00:00:40,860 --> 00:00:41,280 Okay. 13 00:00:41,280 --> 00:00:45,510 So Cron is a command line based program that is used to schedule tasks. 14 00:00:45,510 --> 00:00:50,850 Now Chrome gets its name from the Greek word Chronos, which means time, in case you were wondering. 15 00:00:50,850 --> 00:00:53,490 So Cron actually has quite a good name for a scheduling program. 16 00:00:53,490 --> 00:00:59,100 Right now each user has what is known as a cron tab, which basically is just a text file, and each 17 00:00:59,100 --> 00:01:03,330 cron tab lists which commands or scripts will be run automatically by that user. 18 00:01:03,330 --> 00:01:06,570 And it also lists when they will be scheduled to be run. 19 00:01:06,630 --> 00:01:11,660 Now to edit the cron tab for your user, you just open a command, open a terminal and type cron tab 20 00:01:11,670 --> 00:01:14,520 dash E and the E option is for edit. 21 00:01:14,520 --> 00:01:17,670 And when you open it you'll see that the cron tab begins to open. 22 00:01:17,670 --> 00:01:22,350 But if this is the first time that you're opening up a cron tab, it might ask you what editor you want 23 00:01:22,350 --> 00:01:22,890 to use. 24 00:01:22,890 --> 00:01:28,590 So we're just going to press one for nano and press enter and we will see that now we've opened up our 25 00:01:28,590 --> 00:01:30,270 cron tab in Nano. 26 00:01:30,270 --> 00:01:34,680 Now the first thing you'll notice is that there's a lot of text in here and this is a kind of introduction 27 00:01:34,680 --> 00:01:36,600 to how cron tabs work. 28 00:01:36,600 --> 00:01:41,670 But these are commented lines and what I mean by that is they are not interpreted by the CHRONOTYPE 29 00:01:41,670 --> 00:01:43,830 as actual valuable information. 30 00:01:43,830 --> 00:01:48,780 And you can tell that because they start each line with a hash and that allows, you know, people to 31 00:01:48,780 --> 00:01:54,870 write what they want in a file, like some notes, but it's not interpreted by the actual by the actual 32 00:01:54,930 --> 00:01:56,250 cron utility. 33 00:01:56,400 --> 00:02:02,760 But down the bottom, each cron tab is each cron tab is broken up into rows and one row is for each 34 00:02:02,760 --> 00:02:07,500 command or script that you want to schedule, and each row will have six columns. 35 00:02:07,500 --> 00:02:13,710 So the first five columns are scheduling information, and the final sixth column is the command or 36 00:02:13,710 --> 00:02:15,900 script to be run at that scheduled time. 37 00:02:15,900 --> 00:02:18,600 So you need to get in to the bottom and to enter your information. 38 00:02:18,600 --> 00:02:23,370 Okay, now this is really one of those things that's easier to be shown than explained. 39 00:02:23,370 --> 00:02:25,290 So let me show you how this works. 40 00:02:25,290 --> 00:02:28,860 So let's say that we want to run a command that just outputs the text. 41 00:02:28,860 --> 00:02:35,370 Hello world to a file called hello txt on our desktop and we want that command to run every minute of 42 00:02:35,370 --> 00:02:39,930 every day all year round because for some reason we just really like saying hello. 43 00:02:39,930 --> 00:02:44,700 Okay, so to make this happen, we need to enter a new row in our cron tab. 44 00:02:44,700 --> 00:02:50,940 And so here we are, we're on a new row and there are six columns in that row, each column being separated 45 00:02:50,940 --> 00:02:52,500 by some blank space. 46 00:02:52,860 --> 00:02:54,870 So let's take it step by step. 47 00:02:54,870 --> 00:03:01,830 Now, the first column of the row is for the it's for the minutes of the hour that you want the command 48 00:03:01,830 --> 00:03:02,640 to run on. 49 00:03:02,760 --> 00:03:04,590 So let's go to the first column. 50 00:03:04,590 --> 00:03:08,040 And we could put any number we want in here as long as it's a valid minute. 51 00:03:08,040 --> 00:03:09,930 So we could type 15, for example. 52 00:03:09,930 --> 00:03:13,200 Now, this would make the command run at 15 minutes past the hour. 53 00:03:13,200 --> 00:03:18,210 So things like 12, 15, two, 15, nine, 15, seven, 15 and so on. 54 00:03:18,210 --> 00:03:24,210 Okay, now the numbers for minutes can go from 0 to 59 because that's what numbers 4 minutes do on a 55 00:03:24,210 --> 00:03:24,810 normal clock. 56 00:03:24,810 --> 00:03:25,020 Right. 57 00:03:25,020 --> 00:03:29,670 So you can go from 0 to 59 now because we want the command to run every minute. 58 00:03:29,670 --> 00:03:34,350 We'll just put a star in there to say, run any minute now. 59 00:03:34,350 --> 00:03:36,810 No, this isn't exactly like a wild card. 60 00:03:36,990 --> 00:03:43,560 The star happens to mean any in this case, but crosstabs don't really support other wild cards, so 61 00:03:43,560 --> 00:03:49,140 don't put symbols such as question marks or the brackets in there because they won't work. 62 00:03:49,410 --> 00:03:52,860 The star is only what's accepted and it just means any value. 63 00:03:52,890 --> 00:03:53,850 We don't really care. 64 00:03:54,330 --> 00:03:57,660 So the first column is the minutes of the hour. 65 00:03:57,660 --> 00:03:59,970 The next column is the hour number. 66 00:03:59,970 --> 00:04:02,940 And we could put numbers like, for example, 11. 67 00:04:02,940 --> 00:04:05,400 So this would make it the command or whatever. 68 00:04:05,400 --> 00:04:09,120 We scheduled to only run the at 11 a.m. each day. 69 00:04:09,120 --> 00:04:12,630 We could put 14 so that it would only run at 2 p.m. each day. 70 00:04:12,660 --> 00:04:17,940 Now the numbers here are based on a 24 hour clock and can go from 0 to 23. 71 00:04:17,940 --> 00:04:21,630 And we but we don't mind particularly what our command runs at. 72 00:04:21,630 --> 00:04:23,520 So let's just put a star there as well. 73 00:04:23,640 --> 00:04:30,600 But if I put, for example, an 11 in the in the hour column and a 20 in the minute column, then our 74 00:04:30,600 --> 00:04:33,570 command would run at 11:20 a.m.. 75 00:04:33,570 --> 00:04:37,590 So the first column is for the minutes and the second column is for the hours. 76 00:04:37,590 --> 00:04:43,080 And by the way, it doesn't really matter where we place these columns as long as there's some space 77 00:04:43,080 --> 00:04:43,620 between them. 78 00:04:43,860 --> 00:04:48,090 So we could like, you know, separate them out like that with more than one space. 79 00:04:48,090 --> 00:04:52,350 But as long as there's some blank space in between each of the columns, Cron isn't too fussy about 80 00:04:52,350 --> 00:04:54,420 how the about the how the stuff is laid out. 81 00:04:54,420 --> 00:04:57,570 So let me just try and make it a bit clearer which column we're filling in here. 82 00:04:57,570 --> 00:04:59,820 So there's the first column is minutes, seconds column. 83 00:05:00,040 --> 00:05:01,210 Hours and so on. 84 00:05:01,210 --> 00:05:03,820 I'm just going to kind of spread out our stuff a little bit here. 85 00:05:05,050 --> 00:05:10,090 So we got a bit of a nicer layout, but we don't really care about the minutes, so we'll put a star 86 00:05:10,120 --> 00:05:10,510 there. 87 00:05:10,510 --> 00:05:13,810 We don't really care about the hours, so we'll put a star there too. 88 00:05:13,840 --> 00:05:17,110 Now the third column is what we see here. 89 00:05:17,110 --> 00:05:19,810 Dom is the day of the month. 90 00:05:19,810 --> 00:05:24,280 So if we put a one here, then it would only run on the first day of the month. 91 00:05:24,880 --> 00:05:30,670 And so, for example, if we had a 20 in the minutes column and we had in the Hours column, we had 92 00:05:30,670 --> 00:05:35,650 an 11 and here we had a let's say a ten in the third column. 93 00:05:35,650 --> 00:05:42,490 Then this, then whatever command that we gave it would only run on the 10th day of the month at 1120, 94 00:05:42,760 --> 00:05:47,920 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.. 95 00:05:47,920 --> 00:05:50,080 But again, we don't really mind what day of the month. 96 00:05:50,080 --> 00:05:54,010 So we'll put a star there and we'll put a star in the other columns as well. 97 00:05:56,560 --> 00:05:57,040 There we go. 98 00:05:57,040 --> 00:05:59,620 So let's just lay this out a bit nicer. 99 00:05:59,650 --> 00:06:00,460 Okay, cool. 100 00:06:00,490 --> 00:06:04,720 Now, the fourth column is actually the month itself. 101 00:06:04,750 --> 00:06:11,320 Now, in here, we can put numbers, so we could put one being January, 12 being December and all the 102 00:06:11,320 --> 00:06:13,030 other months in between. 103 00:06:13,300 --> 00:06:15,520 But you could also do it using text. 104 00:06:15,520 --> 00:06:21,010 So for example, in capital letters, I could put Jan for January, February, for February I could 105 00:06:21,010 --> 00:06:23,680 do May or Dec for December. 106 00:06:23,680 --> 00:06:27,970 So three letter months can be used as well for for the month. 107 00:06:28,240 --> 00:06:29,650 If you write them in capital letters. 108 00:06:29,650 --> 00:06:33,550 But here we don't really care about in the month, so we're just going to put a start as well. 109 00:06:33,970 --> 00:06:40,390 And so just to see how this is all coming together, if we add a 20 in the first column and we had in 110 00:06:40,390 --> 00:06:47,230 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 111 00:06:47,230 --> 00:06:48,040 for June. 112 00:06:48,130 --> 00:06:54,610 Then this command would run on the 10th day of the month, on the 10th sorry, on the 10th of June at 113 00:06:54,610 --> 00:06:55,870 11:20 a.m.. 114 00:06:56,680 --> 00:06:58,870 So that's that's how that works there. 115 00:06:58,870 --> 00:07:01,390 But we're going to replace them again with stars. 116 00:07:03,310 --> 00:07:08,890 So as I say, the the cron tab doesn't really care about the amount of space is just that each column 117 00:07:08,890 --> 00:07:11,410 is separated by at least one space. 118 00:07:11,410 --> 00:07:13,240 Okay, so, so there we go. 119 00:07:13,240 --> 00:07:15,850 The next column is the day of the week. 120 00:07:15,850 --> 00:07:22,750 Now the day of the week can have numbers from 0 to 6 with zero being Sunday and six being Saturday. 121 00:07:22,750 --> 00:07:27,070 But as well, you can do this doing text using three letters for each day of the week. 122 00:07:27,070 --> 00:07:33,370 So sun would be sun, mon would be Monday, THG would be Thursday, Saturday would be Saturday and so 123 00:07:33,370 --> 00:07:33,670 on. 124 00:07:34,690 --> 00:07:43,570 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 125 00:07:43,570 --> 00:07:47,890 the hour was 11, and then the minute was 20. 126 00:07:47,890 --> 00:07:55,510 Then this would run at 11:20 a.m. on the 10th of June, as long as the 10th of June. 127 00:07:55,510 --> 00:07:57,940 On on that year was a Sunday. 128 00:07:58,330 --> 00:08:03,610 So you can see how you can become incredibly specific with how you schedule your commands to run. 129 00:08:03,610 --> 00:08:06,620 But you can also be very general and very relaxed as well. 130 00:08:06,640 --> 00:08:09,820 The cron tab, the cron utility is amazing for that kind of stuff. 131 00:08:09,820 --> 00:08:12,490 You can be very expressive with your scheduling. 132 00:08:12,490 --> 00:08:13,780 So there we go. 133 00:08:13,780 --> 00:08:17,800 We're going to set those back to stars because we don't care what day of the week it is. 134 00:08:17,800 --> 00:08:23,770 We want this command to run all day, every minute, all year round, so we don't really care. 135 00:08:24,100 --> 00:08:29,080 So those are the first five columns and these first five are scheduling information. 136 00:08:29,080 --> 00:08:29,460 Okay. 137 00:08:29,590 --> 00:08:35,409 Now the final bit is actually where you tell it the command or script that you want it to run. 138 00:08:35,409 --> 00:08:35,909 Okay. 139 00:08:35,950 --> 00:08:39,700 So the sixth and final column is the command to actually run. 140 00:08:39,700 --> 00:08:46,150 Now we said that we want a command that will output hello world to a file called hello text let's say 141 00:08:46,150 --> 00:08:47,170 on our desktop. 142 00:08:47,410 --> 00:08:48,700 So how would we write that. 143 00:08:48,730 --> 00:08:52,000 Well we might write Echo then. 144 00:08:52,000 --> 00:08:52,960 Hello World. 145 00:08:53,590 --> 00:08:56,650 And then we'd append that to somewhere on our desktop. 146 00:08:56,650 --> 00:08:59,110 A file called Hello Text. 147 00:08:59,380 --> 00:09:01,930 So if I just expand the window here, you'll be able to see it a bit better. 148 00:09:01,930 --> 00:09:02,500 There we go. 149 00:09:02,500 --> 00:09:07,990 So you can see here that we're just saying this is just the command basically echo hello world and put 150 00:09:07,990 --> 00:09:10,480 that on a file on a desktop called Hello Text. 151 00:09:10,480 --> 00:09:16,150 But we're using the two arrows here to allow whenever this command runs it will append data to the file 152 00:09:16,150 --> 00:09:19,600 so we can see extra data being added each time the command runs. 153 00:09:19,600 --> 00:09:25,780 Okay, so when we save the cron tab, so cron will make sure that this command is now run at every minute 154 00:09:25,780 --> 00:09:30,850 of every hour of every day of the month for every month, no matter the day of the week. 155 00:09:31,060 --> 00:09:33,280 So this command is going to run every minute, all year round. 156 00:09:33,520 --> 00:09:39,550 So if I press control in O to save that file and I exit, you can see here that we get a message saying 157 00:09:39,550 --> 00:09:42,600 that it's installing a new cron tab and we can see here that hello. 158 00:09:42,760 --> 00:09:47,050 TXT has actually just been has just been executed. 159 00:09:47,050 --> 00:09:47,410 Okay. 160 00:09:47,440 --> 00:09:50,860 So if we take a look, you can see that it actually just ran, right? 161 00:09:50,860 --> 00:09:52,930 Then we can see that it's actually executed. 162 00:09:52,930 --> 00:09:53,380 Hello world. 163 00:09:53,380 --> 00:09:57,670 And put it inside inside the inside the file. 164 00:09:57,670 --> 00:10:03,730 Now if I run the date command you can see that we've got about 40 seconds until this command runs again. 165 00:10:03,730 --> 00:10:07,210 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 166 00:10:07,210 --> 00:10:07,900 runs. 167 00:10:08,560 --> 00:10:08,740 Okay. 168 00:10:08,740 --> 00:10:09,370 So here we are. 169 00:10:09,370 --> 00:10:10,780 We can see that it's just gone. 170 00:10:10,780 --> 00:10:12,880 And literally as I press date, that's awesome. 171 00:10:13,300 --> 00:10:14,290 It's just past the minute. 172 00:10:14,290 --> 00:10:19,210 So if we look in there now, we should see that we now have two rows of data. 173 00:10:19,210 --> 00:10:20,020 So fingers crossed. 174 00:10:20,020 --> 00:10:20,890 Yes, we do. 175 00:10:20,890 --> 00:10:28,900 We now have two rows of data in there because the the the the cron tab has executed again right at the 176 00:10:28,900 --> 00:10:36,700 top of the hour and we'll just continue to wait until the next minute passes and it will continue to 177 00:10:36,700 --> 00:10:37,870 run that. 178 00:10:37,870 --> 00:10:45,730 It'll continue to run that that command over and over and over again and fill up this hello text file. 179 00:10:45,760 --> 00:10:46,240 Okay. 180 00:10:46,330 --> 00:10:51,850 Now, when when that actually runs, we'll see that more data has been added. 181 00:10:52,150 --> 00:10:53,590 We've got about 18. 182 00:10:54,020 --> 00:10:56,300 Also see if you can wait with me for like 18 seconds. 183 00:10:56,300 --> 00:10:57,140 We'll see this happen. 184 00:10:57,140 --> 00:10:58,550 Okay, so let's. 185 00:10:58,550 --> 00:10:59,000 Here we go. 186 00:10:59,000 --> 00:11:01,220 We're seeing another 11 seconds or so. 187 00:11:01,640 --> 00:11:06,380 Um, and now if I, if I check here, now we can see that there's 5 seconds to go. 188 00:11:06,530 --> 00:11:07,820 5 seconds to go. 189 00:11:08,060 --> 00:11:09,350 2 seconds to go. 190 00:11:09,920 --> 00:11:11,120 It should have done it now. 191 00:11:11,120 --> 00:11:13,130 So if we look inside, yep. 192 00:11:13,130 --> 00:11:17,030 It's run and it's added another row to the file and I didn't touch anything. 193 00:11:17,030 --> 00:11:17,660 I promise. 194 00:11:18,020 --> 00:11:19,730 So the Akron tab is working. 195 00:11:19,730 --> 00:11:20,300 Hooray! 196 00:11:20,300 --> 00:11:23,600 Okay, we've just made it run a command over and over and again.