Check out a free preview of the full Complete Intro to Linux and the Command-Line course
The "cron" Lesson is part of the full, Complete Intro to Linux and the Command-Line course featured in this preview video. Here's what you'd learn in this lesson:
Brian explains that developers use the cron command when a specific job or program needs to be executed following a schedule, and demonstrates how to build a cron job.
Transcript from the "cron" Lesson
[00:00:00]
>> So you've probably heard me refer to this. I've said jobs, right? Like you wanna run this job later or something like that. That term refers refers to cron jobs. And so I don't actually really know why it's called cron. I assume it's something to do with like chronological.
[00:00:15]
But the idea of cron is that you have some tasks and you want it to be run at some interval, right? So maybe I have like a logs folder and I want that logs folder to take all those logs, upload them to a server and then delete them.
[00:00:29]
And I want that to happen every hour, right? You can use cron to do that. So how do you do that? Well, there's a couple ways of doing it. One, which works for Ubuntu but doesn't necessarily work for every distribution of Linux, there is a etc/cron folder and then etc/cron .daily, .hourly, .monthly, and .weekly.
[00:00:47]
If you put a script in there, it will just run daily. I don't actually know what time it runs at but you can be guaranteed that that task's going to be run daily. So if we come back over here to my shell here. Just close that one. And I go to etc/cron and I hit Tab.
[00:01:09]
You can see that there is a cron daily, a cron hourly, a cron month, and a cron weekly. As long as it has the correct permissions and everything set, and that it's an executable file, it will run that every hour every month as long as that script is inside of that folder.
[00:01:22]
So I don't think any of these will have anything in them. I mean, we can check etc/cron .hourly or monthly? Nope, so these are empty. Look, there's some for daily. So you can see here aptitude has one that's run, password has something that it's running, a port app compatibility, these things have put things into this cron folder for them to be run every looks like day, right?
[00:01:54]
So these are all running daily. And keep in mind your computer has to be running for the cron job to run, right? It doesn't run while it's off, I think that makes sense, but sometimes bears mentioning. So, this is this works, this is a totally valid way to do it.
[00:02:10]
Sometimes you need a bit more granular control, like maybe you need something to run during off peaks hour, right? So if you're going to be transferring a bunch of data off your server and into your database, you don't want to do that during peak traffic hour. You want to do that like 2 A.M when most of your users are sleeping.
[00:02:27]
So that's when cron is gonna come in handy. Yeah. So just the one thing to make sure if you're putting something inside of these cron folders, make sure you give it the chmod +x. It must be an executable file right? That's one thing I'll tell you. All right, so, let's go into our my bin again, my bin, and let's make a new file in here.
[00:03:03]
We'll call it vi make new file like that. And I'm going to put in here my hash bangs /bin/bash and I'm going to chmod +x make new file. Okay, so now I have this make new file And I'm going to vi make new file, and I'm gonna put in here, make dir-p temp-files.
[00:03:44]
I'm gonna cd into the temp-files, and I'm going to touch file- and then I'm going to do a subcommand here for the date, date+%s.txt. So what does this do? Remember dollar signs, parentheses, that does a subcommand. So this is going to run some sort of command and whatever it returns is going to put in here.
[00:04:16]
The plus is telling it what format you want the date in and the %s is the Linux timestamp or the Unix timestamp, right? So it's how many seconds have elapsed since January 1, 1970? Okay, so all this is gonna do is it's going to create the file of the folder, cd into the folder, and it's going to create a new file with the file-timestamp.txt.
[00:04:44]
That's all it does. So actually, just to show you, I'll show you how to run this as well. Okay, so I'm gonna write and quit here. If I run this date plus that, right, it'll just be a long number, right? So this is how many seconds have elapsed since January 1, 1970.
[00:05:03]
Okay, now if I run make new file like that and I go into what did I call that, temp files? You can see here it created that. But let's say I want this to be run every minute. Well, let's go do that. I'm going to say crontab -e and then it's going to ask you what editor you want to use to edit your crontab.
[00:05:36]
And you can use nano that's where as you can see there it says points of this being the easiest. And definitely do not say for. We learned that ed is not the one that you want to choose. And you can see, I actually don't know the difference between vim.basic and vim.tiny, but I'm gonna go with 2, cuz that's just vim.
[00:05:52]
But feel free to use nano if you want to, okay? Notice, these are all comments. This is kind of leaving you some helpful comments of how to interact with this. But I'm gonna show you. So the first thing we're gonna do is there's how many stars here, five?
[00:06:11]
Yeah, so star star star star star. And then you want to put the thing that you want to run. So in my case, I want it to run the command that I want to run which is making file which is going to be in /home/ubuntu/my_bin/make_new_file. Okay, so it's going to run this command right, which we made sure that has the right execute permissions, which is great.
[00:06:47]
So let's talk about these stars for a second. This stars is letting Linux know, or cron rather, I want this to run on if this is for minutes, this is for hours. Let's say I have minutes, hours, day of the week, the month, sorry, day of the month.
[00:07:12]
So this is the date. So this would be like, let me start over. This is minutes, hours, day of the month, so this will be the 25th day of the month or the 3rd day of the month. This is which month to run, and so you can say like only run this every December.
[00:07:28]
And then the day of the week is here. So this is like run only on Sundays or run every on Tuesdays, okay? So let's let's just go and do an example here. If I'd replaced this with 5, this would run every hour on the fifth minute of the hour.
[00:07:48]
Or if I did this, 25, this would run every hour on the 25th minute of the hour. Or I could do 5, 25 and this will run on the 5th and the 25th minutes of every hour. If I do this and change this instead, this will run, Every 12th hour, right?
[00:08:18]
Yeah, So as you can see here I get wildly confused because when I think about crontabs, and thank God for cron guru. I this is what exactly what I do every single time is I come to this lovely website, crontab.guru, and I just enter this in until I get what I actually expect so this will take translate crontab speak into Brian speak So, if I do this, That's okay.
[00:08:53]
So, that's what this does is it's gonna run every minute past hour 12. So it's gonna run for one hour basically every day. But if I do this it's gonna run at 12:05 exactly every day. Does that kinda makes sense? Yeah, it's It's confusing, but it's also powerful enough that you can refer to what you actually need to do.
[00:09:18]
So this runs every minute if I do it this way, or here it'll run at minute one every single hour, or if I put here at the end, this will run every minute on Mondays. This will run every minute of every Monday that is the first day of the month.
[00:09:40]
So not frequently I think is the answer to that question. All right, so most of the time I'm not really in here messing too much with it. I'll either come in here and say, I want this to run every hour, which will be like something like that right?
[00:09:57]
Or you can do this like I want this to run every 30 minutes. If you put the /30, that means I want this to run every, whatever this is divisible by, right? So if I do this, this would be every second minute, every fifth minute. Every ten minutes, so on and so forth, right?
[00:10:14]
And there's also a couple that you can do what like this daily. This would be every day at midnight this would run. Another useful one is reboot. This will run every single time that you reboot the computer. It'll rerun it. You could also do that from your bash/rc if you're doing it from interactive shells.
[00:10:32]
So anyway crontab.guru, just go in there, mess with it until it says the English that you anticipated saying. That's exactly what I do. So for hours, I want it to run, let's say every two minutes, so I'm gonna put /2 there. And I'm going to write quit. And then if you're doing it through crontab -e, it'll automatically restart the crontab and it'll start running.
[00:11:00]
So, right now we have just the one file in there. So eventually within the next couple of minutes, hopefully we'll see it create another file and it'll keep going. And keep in mind if you leave it there, it will continue running every two minutes as long as your computer is up.
[00:11:19]
Another thing to keep in mind while we're waiting for this is I put this in, so this is actually going to run this as root, right? Because I didn't specify user and by default, it will run as root. Again, not something I would typically suggest you doing. I would suggest you do this as crontab -u Ubuntu.
[00:11:36]
So run it as you or create a new user and run it as the user and then do this. So you can see here, yeah, it's still in there. Yeah. So yeah, that's good. And you can see there, it created a file. I didn't actually run it. That was the crontab that actually ran it.
[00:11:59]
And by default, I guess it does run it as you so I guess I could come in here and say, run this as root if I had something that had to be run as root and then you have to do sudo. So now, I would be doing this and this would be running as root, but in general you want to run it as yourself or another user with less permissions than root.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops