1 00:00:00,240 --> 00:00:02,190 So let's take a look at our tab again. 2 00:00:02,190 --> 00:00:05,370 Let's go Chrome tab with the E option and we've opened it up. 3 00:00:05,370 --> 00:00:07,800 See, it's remembered that Nano is our editor. 4 00:00:07,800 --> 00:00:13,680 And just so just in case you're interested, if you go on to your home directory and you look at your, 5 00:00:13,680 --> 00:00:20,160 your all the files with LS Dash a there's a hidden file here called selected editor and it's that file 6 00:00:20,160 --> 00:00:24,420 that stores your preference for for editor. 7 00:00:24,420 --> 00:00:30,330 So in here, if we look at dot selected editor, you can see you can change that if you want by putting 8 00:00:30,330 --> 00:00:33,390 the path to the editor that you want to have. 9 00:00:33,390 --> 00:00:38,970 Or alternatively, you could just type the select editor command and it would give you the choice again. 10 00:00:39,150 --> 00:00:41,310 But I'm going to keep it to Nano and leave it like that. 11 00:00:41,310 --> 00:00:44,640 But that's how if you wanted to change the editor later on, maybe you get better with another command 12 00:00:44,640 --> 00:00:45,330 line editor. 13 00:00:45,360 --> 00:00:46,740 That's how you can change it. 14 00:00:47,100 --> 00:00:54,870 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 15 00:00:54,870 --> 00:00:57,180 options and more advanced ways that you can do some scheduling. 16 00:00:57,630 --> 00:01:01,770 We can add as many rows as we like in this cron type and they'll all work exactly the same. 17 00:01:01,770 --> 00:01:06,300 So I'm going to make a new row and look at some more advanced scheduling opportunities. 18 00:01:06,300 --> 00:01:14,130 So for example, let's say I want to run something exactly at the hour and 15, 30 and 45 minutes past. 19 00:01:14,130 --> 00:01:14,360 Okay. 20 00:01:14,460 --> 00:01:16,410 So I don't just have to have one option here. 21 00:01:16,410 --> 00:01:20,160 I could have zero, comma, 15, comma 30, comma, 45. 22 00:01:20,160 --> 00:01:21,480 That's going to be my first column. 23 00:01:21,480 --> 00:01:21,840 Right? 24 00:01:21,840 --> 00:01:27,450 So this is going to be the minute I can have a zero past 15 minutes past, 30 minutes past and 45 minutes 25 00:01:27,450 --> 00:01:27,780 passed. 26 00:01:27,780 --> 00:01:30,600 Notice how I've got commas but no spaces. 27 00:01:30,600 --> 00:01:33,630 You're not allowed to have any spaces because spaces separate the columns. 28 00:01:34,110 --> 00:01:36,780 So that would be like my first my first column. 29 00:01:37,830 --> 00:01:40,290 I could then space this stuff over. 30 00:01:40,290 --> 00:01:47,610 So I got a bit more space and now I could say, okay, run it at every hour, every day of the month, 31 00:01:48,060 --> 00:01:50,160 every month, every day of the week. 32 00:01:50,160 --> 00:01:52,320 And then whatever command I wanted to happen here. 33 00:01:52,320 --> 00:01:55,560 So something like Hello Echo, hello, for example. 34 00:01:55,560 --> 00:01:59,520 And this would run at exactly as the hour turned. 35 00:01:59,520 --> 00:02:02,040 And 15 minutes past the hour. 36 00:02:02,040 --> 00:02:07,980 30 minutes past the hour and 45 minutes past the hour or quarter to the hour or I could just say more 37 00:02:07,980 --> 00:02:13,020 simply run every 15 minutes and the way I do that is star then a slash 15. 38 00:02:13,500 --> 00:02:19,920 Now that says run every 15 minutes, I could say run every 5 minutes by star slash five or every 3 minutes 39 00:02:19,920 --> 00:02:26,820 star slash three every 7 minutes star slash seven or star slash 15, like I said, for every 15 minutes. 40 00:02:26,820 --> 00:02:31,350 But what about what if I wanted to run every 15 minutes, every three days? 41 00:02:31,350 --> 00:02:32,010 Let's say? 42 00:02:32,010 --> 00:02:37,380 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 43 00:02:37,380 --> 00:02:39,900 do star slash three now. 44 00:02:39,900 --> 00:02:41,100 So every three days. 45 00:02:41,100 --> 00:02:45,870 So let's say on the first of the month, the fourth of the month, the seventh of the month, and so 46 00:02:45,870 --> 00:02:48,990 on, on those days it'll run every 15 minutes. 47 00:02:49,530 --> 00:02:51,330 So you can see how this kind of works here. 48 00:02:51,330 --> 00:03:01,050 What if I wanted the command to run once a week at 2359 on a Sunday, only in December and January 23, 49 00:03:01,050 --> 00:03:03,990 59 on a Sunday, but only in December and January. 50 00:03:04,050 --> 00:03:04,950 Well, how do we do that? 51 00:03:04,950 --> 00:03:05,900 Let's make another roll. 52 00:03:05,910 --> 00:03:08,460 We'd say let's to 23, 59. 53 00:03:08,460 --> 00:03:09,630 So 59 is the minute. 54 00:03:09,870 --> 00:03:11,610 The hour is going to be 23. 55 00:03:12,120 --> 00:03:17,820 It doesn't matter which day of the month, it doesn't really actually the month does matter only January's 56 00:03:17,820 --> 00:03:18,510 and December. 57 00:03:18,510 --> 00:03:21,750 So January's and December's day of the week, only Sundays. 58 00:03:21,870 --> 00:03:22,170 Okay. 59 00:03:22,170 --> 00:03:25,110 We could say because we can. 60 00:03:25,110 --> 00:03:26,220 Okay, so there we are. 61 00:03:26,220 --> 00:03:30,300 Now what we've got 23, 59 every day of the month. 62 00:03:31,170 --> 00:03:37,110 But so what this is going to do 2359 in January, in December, but only on Sundays. 63 00:03:37,380 --> 00:03:37,800 So there we are. 64 00:03:37,800 --> 00:03:44,130 And it's going to echo because we can have for example, now there's a great website called Cron Tab 65 00:03:44,130 --> 00:03:51,120 Dot Guru that you can take a look at and play around with to see what different Cron tab expressions 66 00:03:51,120 --> 00:03:53,490 mean and I'll put a link to it in the resources section. 67 00:03:53,490 --> 00:03:54,030 It's awesome. 68 00:03:54,030 --> 00:03:54,450 Okay. 69 00:03:54,900 --> 00:04:00,450 But a great use of cron is to schedule automated backups of the file system. 70 00:04:00,450 --> 00:04:06,360 So let's say we want to run weekly backups every Friday at 2359 for every month of the year. 71 00:04:06,720 --> 00:04:11,070 So if I delete these, these examples here, delete these. 72 00:04:11,070 --> 00:04:15,390 So 2359 on Fridays for every month of the year. 73 00:04:15,390 --> 00:04:19,230 So we know that 59 is the minute we know the hour is 23. 74 00:04:19,890 --> 00:04:23,460 It doesn't matter the day of the month like numerically, it doesn't matter whether it's the first or 75 00:04:23,460 --> 00:04:27,870 the seventh, just every Friday of every month of the year, but only on Fridays. 76 00:04:27,960 --> 00:04:34,620 And then we'd put the command, which is going to back up our stuff and, you know, that's how it would 77 00:04:34,620 --> 00:04:35,040 work. 78 00:04:35,040 --> 00:04:39,180 And but we know how to do this because we can make a script for it. 79 00:04:39,660 --> 00:04:43,470 So let's save our cron tab so far just like that. 80 00:04:43,470 --> 00:04:45,030 And you can see it's work 24 lines. 81 00:04:45,030 --> 00:04:49,200 But if I actually look just by the way, while we've been editing that, if I look in the text, you 82 00:04:49,200 --> 00:04:54,450 can see we've got so many more hello worlds being added to that as our cron. 83 00:04:54,480 --> 00:04:57,330 Her command that we set before has been continuing to run. 84 00:04:57,330 --> 00:04:58,380 So that's really cool, right? 85 00:04:58,380 --> 00:04:59,700 It's automated and it's running. 86 00:04:59,770 --> 00:05:00,760 In the background. 87 00:05:02,530 --> 00:05:02,980 All right. 88 00:05:02,980 --> 00:05:05,840 So let's set up the script that we want to run now. 89 00:05:05,860 --> 00:05:10,480 You remember in the last few videos when I was telling you about bash scripts, we made a folder called 90 00:05:10,480 --> 00:05:11,020 Bin. 91 00:05:11,020 --> 00:05:14,380 And inside that bin folder is where we started putting our scripts. 92 00:05:14,380 --> 00:05:16,570 And we had this one file called Backup. 93 00:05:16,570 --> 00:05:19,390 So let's take a look inside that backup script again. 94 00:05:19,750 --> 00:05:26,860 Well, in here, what it does is it compresses into into a archive on our desktop, every single one 95 00:05:26,860 --> 00:05:31,480 of the contents of our documents, downloads, desktops, pictures and videos, folders. 96 00:05:31,750 --> 00:05:36,970 And it also outputs some the error, like the standard error, like the, the log messages that come 97 00:05:36,970 --> 00:05:40,870 from the command into the, the Bitbucket. 98 00:05:40,870 --> 00:05:43,420 So they're just sent to, to the void. 99 00:05:43,500 --> 00:05:45,130 They're no longer printed to the screen. 100 00:05:45,130 --> 00:05:45,540 Okay. 101 00:05:45,550 --> 00:05:46,540 So this is good. 102 00:05:46,540 --> 00:05:48,850 This is this is going to do a backup for us. 103 00:05:49,390 --> 00:05:55,360 Let's say that actually, instead of sending them to the desktop, let's say that in our in our home 104 00:05:55,360 --> 00:06:00,070 directory, let's say let's say we made a directory called backups. 105 00:06:01,240 --> 00:06:01,990 I spelled that wrong. 106 00:06:01,990 --> 00:06:08,890 So if I rename backups with two S's into just backups with one s, there we go. 107 00:06:08,890 --> 00:06:14,140 We've now got a folder called backups and let's say we wanted to save the backups in there. 108 00:06:14,140 --> 00:06:24,130 So if we go to the bin folder and we edit our backup script now we can say, okay, go into instead 109 00:06:24,130 --> 00:06:30,490 of our desktop, go to our backups folder and then save the backup. 110 00:06:31,150 --> 00:06:34,360 GZ That's good and that's good for that. 111 00:06:34,360 --> 00:06:42,070 But what we want as well is we want to also log the date and time that the backup was created into a 112 00:06:42,070 --> 00:06:42,850 log file. 113 00:06:42,850 --> 00:06:48,760 Now the way that we could do that is we could use the date command and we could append that data to 114 00:06:48,760 --> 00:06:54,700 a file in our backups folder called, let's say, backup backups log. 115 00:06:54,700 --> 00:06:55,450 Why don't we say that? 116 00:06:55,450 --> 00:06:55,620 Okay. 117 00:06:55,630 --> 00:06:58,480 So we're going to call it backups log. 118 00:06:58,480 --> 00:07:03,640 And that's just going to keep in there the date and time that the last log happened. 119 00:07:05,050 --> 00:07:08,560 So if we save that, actually, we could we could make it a bit more advanced. 120 00:07:08,560 --> 00:07:11,050 We could actually now let's leave it like that. 121 00:07:11,650 --> 00:07:13,630 Let's not make this any more complicated than it needs to be. 122 00:07:13,630 --> 00:07:13,910 Okay? 123 00:07:13,990 --> 00:07:18,160 So we're going to take that date and we're going to append it to our backups log file. 124 00:07:18,160 --> 00:07:22,750 And you can see here that I'm using the double arrows to make sure that as this script runs, each time 125 00:07:22,750 --> 00:07:25,150 it's going to it's going to add it. 126 00:07:25,150 --> 00:07:26,970 So the file gets longer as time goes on. 127 00:07:26,980 --> 00:07:27,290 Okay. 128 00:07:27,400 --> 00:07:30,430 So we've now got this script that we want to run. 129 00:07:30,430 --> 00:07:33,250 And if we've run it just we just run backup from anywhere. 130 00:07:33,250 --> 00:07:35,470 If we run backup, you can see that. 131 00:07:35,470 --> 00:07:42,520 Now, if I just put this back to a normal size, if we go into our backups folder, we've now got backup 132 00:07:42,520 --> 00:07:45,610 star GZ and we've also got this backups log. 133 00:07:46,120 --> 00:07:51,280 If I open with that with nano backups log, you can see that it's logged the date and time. 134 00:07:51,490 --> 00:07:56,440 So if I just remove remove everything in here using RAM, you can see they've all disappeared. 135 00:07:56,710 --> 00:07:57,340 So we're good. 136 00:07:57,340 --> 00:07:58,690 We know that script works. 137 00:07:58,690 --> 00:08:01,270 Let's try and add it to our cron tab. 138 00:08:01,720 --> 00:08:02,500 So here we are. 139 00:08:02,500 --> 00:08:05,410 We've said that we want every Friday at 2359. 140 00:08:05,410 --> 00:08:06,100 We want that. 141 00:08:06,130 --> 00:08:07,450 We want that script to run. 142 00:08:07,450 --> 00:08:08,560 So how can we do that? 143 00:08:08,830 --> 00:08:11,230 Well, the way we can run that is because it's a bash script. 144 00:08:11,230 --> 00:08:12,580 We'll type the bash command. 145 00:08:12,580 --> 00:08:16,840 So just bash like that and then we'll give it the path to the script that we want to run. 146 00:08:16,840 --> 00:08:22,690 So in our home directory in the bin folder, we're going to run the backup script. 147 00:08:22,690 --> 00:08:23,950 So that's all we've got to do, right? 148 00:08:23,950 --> 00:08:26,980 We just type bash and then give it the path to the script that we want to run. 149 00:08:26,980 --> 00:08:30,850 And then that script will now run at 2359 on a Friday. 150 00:08:30,850 --> 00:08:35,740 But to save you waiting all the way till 2359 on next Friday, let's go ahead and make this run every 151 00:08:35,740 --> 00:08:36,909 minute so we can watch it happen. 152 00:08:37,090 --> 00:08:38,049 So look what I'm going to do. 153 00:08:38,049 --> 00:08:39,370 Nice hack, hack, hack. 154 00:08:39,370 --> 00:08:43,000 Okay, so I'm going to cut the line and uncut the line and then cut the line again. 155 00:08:43,000 --> 00:08:46,540 So that's a nice copy and paste and I'm going to make this run every minute. 156 00:08:46,840 --> 00:08:49,630 So if we go, we're going to change all of these to stars. 157 00:08:52,840 --> 00:08:53,440 So there we go. 158 00:08:53,470 --> 00:08:55,750 Now, that should run every minute now. 159 00:08:55,750 --> 00:09:00,950 So if I write the file and close it, you can see it's installed the new cron tab. 160 00:09:00,970 --> 00:09:02,980 Now in backups, there's nothing in here. 161 00:09:02,980 --> 00:09:08,320 But if I look at the date, we can see that we've got about 40 seconds until this runs again. 162 00:09:08,360 --> 00:09:10,040 Needs to be very excited, I'm sure. 163 00:09:10,060 --> 00:09:12,260 So we've got about about 30 seconds. 164 00:09:12,260 --> 00:09:14,980 So I'm going to cut the video here and come back closer to the time. 165 00:09:16,110 --> 00:09:16,620 So here we are. 166 00:09:16,620 --> 00:09:17,520 Let's take a look. 167 00:09:17,700 --> 00:09:19,800 And it's just gone past the minute. 168 00:09:19,810 --> 00:09:25,140 So let's take a look at our backups folder and hooray, we have indeed run our script correctly. 169 00:09:25,140 --> 00:09:33,780 And if we look inside the log folder, if we look at backups log, we can see that it ran at just past 170 00:09:34,170 --> 00:09:36,000 the third minute of today. 171 00:09:36,000 --> 00:09:42,090 So the reason that it runs at just the minute past, it's just like a like, you know, there's just 172 00:09:42,090 --> 00:09:43,110 1/2 error. 173 00:09:43,110 --> 00:09:44,580 I think it's just a clock thing. 174 00:09:45,450 --> 00:09:49,770 It's, you know, the computer can only process things at a certain time resolution. 175 00:09:49,770 --> 00:09:52,950 And 1/2 is really not that much of a bad error. 176 00:09:52,950 --> 00:09:53,400 Okay. 177 00:09:53,400 --> 00:09:55,980 So it'll you'll tend to see things like this. 178 00:09:55,980 --> 00:10:01,680 I think it actually does run at exactly the time the minute goes, but the date command takes just about 179 00:10:01,680 --> 00:10:03,540 a second or so to run or something like that. 180 00:10:03,540 --> 00:10:06,930 So there's a slight delay, but really 1/2 is not bad at all. 181 00:10:06,930 --> 00:10:09,600 So now we have indeed ran a command. 182 00:10:09,600 --> 00:10:11,580 And look, it might it might even happen again. 183 00:10:11,610 --> 00:10:12,810 It might even happen again. 184 00:10:12,810 --> 00:10:17,070 So if we're at four more seconds, one more second. 185 00:10:17,430 --> 00:10:18,450 Yes. 186 00:10:18,450 --> 00:10:21,330 Now we look inside our backups log. 187 00:10:22,230 --> 00:10:23,700 Backups log. 188 00:10:23,700 --> 00:10:27,480 We can see that another one is run again just at 1/2 past the fourth minute. 189 00:10:27,480 --> 00:10:31,050 So now we are making backups every single minute. 190 00:10:31,050 --> 00:10:35,910 But that might be a bit of a a bit of a a bit of a burden on our computer here. 191 00:10:35,910 --> 00:10:40,380 So let's just go ahead control and K to delete the line and then we can save our cron tab and leave 192 00:10:40,380 --> 00:10:44,190 it so that it just runs at 2359 on Fridays. 193 00:10:44,190 --> 00:10:45,660 So there you are, guys. 194 00:10:45,660 --> 00:10:49,200 How awesome and how epic is that? 195 00:10:50,710 --> 00:10:56,410 So I'm sure you can see the power of combining together bash scripts and cron scheduling to control 196 00:10:56,410 --> 00:10:58,990 whatever you want about your computer. 197 00:10:59,020 --> 00:11:04,870 Now, these two skills together are incredibly powerful combination, and you now have the foundation 198 00:11:04,870 --> 00:11:05,960 in how they work. 199 00:11:05,980 --> 00:11:08,800 So let's have a quick recap of what we've learned in this video. 200 00:11:08,800 --> 00:11:14,410 So in this video, you took a look at how to use cron to schedule when commands and scripts should be 201 00:11:14,410 --> 00:11:14,710 run. 202 00:11:14,710 --> 00:11:19,780 And you learn that every user has what is known as a cron tab and that you can edit cron tabs using 203 00:11:19,780 --> 00:11:24,010 the cron tab command with the E option with the E standing for edit. 204 00:11:24,010 --> 00:11:29,200 Now cron tabs are text files that are broken up into many rows and you can have as many rows as you 205 00:11:29,200 --> 00:11:29,620 like. 206 00:11:29,620 --> 00:11:35,260 And each command or script has its own row and each row has six columns. 207 00:11:35,260 --> 00:11:40,300 The first five columns are scheduling information and the sixth and final column details which command 208 00:11:40,300 --> 00:11:42,730 or script should be run at that scheduled time. 209 00:11:42,730 --> 00:11:45,640 So the columns are for, for the scheduling columns. 210 00:11:45,640 --> 00:11:47,980 You've got the, the, the minutes of the hour. 211 00:11:47,980 --> 00:11:54,430 So for example, if you put 15 in that column, the command would only run at the 15th minute of each 212 00:11:54,430 --> 00:11:54,790 hour. 213 00:11:54,790 --> 00:11:59,110 So things like 11, 15 to 15, three, 15 and so on. 214 00:11:59,110 --> 00:12:01,450 Then you've got the hour itself. 215 00:12:01,450 --> 00:12:04,690 So that's a number that you can put in 24 hour clock time. 216 00:12:04,930 --> 00:12:09,370 So from 0 to 23 you've got, then the third day of the column is the day of the month. 217 00:12:09,370 --> 00:12:13,600 So again, from one to the end of the month, whatever that might be, the fourth column is the month. 218 00:12:13,600 --> 00:12:17,260 So that could be numbers with one being January and 12 being December. 219 00:12:17,260 --> 00:12:22,030 Or you can just type in capital letters the three letter version of the month. 220 00:12:22,030 --> 00:12:28,270 So Jan for January, DC, for December and so on, and the fifth column is the day of the week, so 221 00:12:28,270 --> 00:12:33,100 that could be 0 to 6 with zero being Sunday and six being Saturday. 222 00:12:33,100 --> 00:12:39,160 You could also enter seven as Sunday again if you wanted, but a good way to do it is just to enter 223 00:12:39,160 --> 00:12:40,360 the day names. 224 00:12:40,690 --> 00:12:44,140 So Mon being Monday, soon being Sunday. 225 00:12:44,140 --> 00:12:45,580 So three letter day names. 226 00:12:46,060 --> 00:12:49,660 And you also saw some advanced ways to to schedule this stuff as well. 227 00:12:49,660 --> 00:12:56,470 And a great place to look at these these scheduling things is to look at a website called Cron Tab dot 228 00:12:56,470 --> 00:12:57,310 guru. 229 00:12:57,460 --> 00:12:59,620 I'll put a link to that in the resources section. 230 00:13:00,530 --> 00:13:05,060 Um, because it's a great website for you to practice setting up these scheduling conditions, and it 231 00:13:05,060 --> 00:13:08,240 also will be useful for you at some point in the future. 232 00:13:08,240 --> 00:13:11,990 And as I should mention as well, columns can be separated by as many spaces as you like. 233 00:13:11,990 --> 00:13:16,790 All cron wants is that you separate each column by at least one space, but feel free to format each 234 00:13:16,790 --> 00:13:18,440 rows however you like. 235 00:13:18,920 --> 00:13:24,560 And you also saw in this video how to use cron tabs and batch scripts to schedule automated backups 236 00:13:24,560 --> 00:13:29,390 of your home directory, which is an incredibly useful thing to make sure that all your files stay safe 237 00:13:29,390 --> 00:13:33,620 and it built upon what you learned in the last section about file compression and archiving. 238 00:13:33,620 --> 00:13:35,120 So that's super awesome as well. 239 00:13:35,150 --> 00:13:39,710 Now the key thing that I want you to remember from the last two videos is that in Linux, practically 240 00:13:39,710 --> 00:13:42,080 anything can be edited using the command line. 241 00:13:42,080 --> 00:13:46,940 Now, since you can write bash scripts to execute these commands in a controlled sequence and then use 242 00:13:46,940 --> 00:13:52,430 Cron to schedule when these scripts should be run, you have the necessary toolkit to do whatever you 243 00:13:52,430 --> 00:13:57,500 want on your system whenever you want to do it, which is a crazily powerful combination. 244 00:13:57,500 --> 00:14:01,340 All you need now is a bit of imagination to decide what you might like to do now. 245 00:14:01,340 --> 00:14:06,680 Batch scripts are a whole massive topic in and of themselves, so we only really had time to scratch 246 00:14:06,680 --> 00:14:10,370 the surface and cover just a small bit of what they can do in this course. 247 00:14:10,610 --> 00:14:14,270 But at least now you have an understanding of what batch scripts are, how they work, and where you 248 00:14:14,270 --> 00:14:15,020 might use them. 249 00:14:15,020 --> 00:14:20,180 And if you'd like me to create a specialized course on just bash scripts, then send me a message. 250 00:14:20,180 --> 00:14:23,900 And if I get enough of you saying that you want that, then I can I can potentially put that together 251 00:14:23,900 --> 00:14:24,470 for you. 252 00:14:24,470 --> 00:14:29,330 But for what we've learned in this course so far, I want to make sure you get some practice in using 253 00:14:29,330 --> 00:14:29,510 it. 254 00:14:29,510 --> 00:14:33,770 So in the next video you're going to be getting the brief for your next project assignment. 255 00:14:33,770 --> 00:14:38,150 Now, it will be a rather simple and fun one this time, but it allow you to go off and try writing 256 00:14:38,150 --> 00:14:42,680 batch scripts and crosstabs for yourself so you can get a hands on understanding of how these incredibly 257 00:14:42,680 --> 00:14:44,000 powerful skills work. 258 00:14:44,000 --> 00:14:48,050 So for the brief of your next assignment, I'll see you in the next video.