Category Archives: Administration

crontab

Last post we talked about cron and the basic format of the crontab file. This time we’re going to talk about more advanced examples of crontab. Remember, the fields are:

field Allowed Values
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use names)

I want my computer to beep on Sunday afternoon at 3:15 pm. The first field is 15 and the second field is 15. It isn’t 3, because that would be 3:15AM. I don’t know about you, but I’m asleep then. crontab uses military time where you add 12 to any time after noon. So 6pm becomes 18 for the crontab. I don’t care about the actual day of the month or the month, I want it every Sunday. So the third and fourth entries are * for the wildcard. For the last entry I can use either 0, 7, or Sun. I like Sun because it’s more descriptive. My crontab looks like:

15 15 * * Sun /home/computerlamp/beep.sh

I’ve decided I want my computer to beep on January 1 at midnight. It’s a way to celebrate the New Year! Happy New Year *beep*. The first two entries are 0, because I want 12:00AM, which is 0 minutes and 0 hours. The third entry is 1, for the first day of the month and the fourth is 1 or Jan for the first month of the year. I don’t really care what day of the week the first day of the year is, so I can put a wildcard for the fifth column. That makes my crontab look like:

0 0 1 1 * /home/computerlamp/beep.sh

I could also make it look like:

0 0 1 Jan * /home/computerlamp/beep.sh

We’ve talked about the format of the crontab but I haven’t said anything about how to edit it. That’s a more complex problem and we’ll talk about it in the next post.

cron

In the last post we talked about scheduling things one at a time, which doesn’t work if we have things we want to repeat. Like I want my computer to make a beep every hour on the hour, I can either sit down and repeatedly run the at command until I’m sick of running the at command, or I can use a different system. That different system is cron.

That’s a weird name for a time keeping system. The story is (and who knows if this is true) it was named after the Greek god of time, Cronus.

Cronus

Cronus

Cron is very flexible. You can run things every hour, every day, every month, every week, every 5 minutes, thirty minutes after the hour, you get the idea. Any kind of regular time interval you want, you can run your program. It’s managed by a file called crontab.

Every user on a Linux system can operate a crontab, there’s no restrictions on that. It has a format you have to follow, otherwise it won’t work. The first five entries in the file are time related.
These  entries are minute, hour, day of month, month, and day of week.  Each of them have values you can put in:

field Allowed Values
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use names)

If I want something to run at midnight, then the first field is 0, the second is 0, but what about the third? I can use a ‘*’ to mean a wildcard. This means that on any day of the month, the job will run if the time matches the first two fields. I want it to run at midnight every day, so I put a * in the last two fields. If I want my computer to beep every night at midnight, my crontab would look like:

0 0 * * * /home/computerlamp/beep.sh

That program looks weird, right? Well in order for them to work, you have to give the full path of the program, otherwise the computer doesn’t know where to look.  Just like with at!

We’ll look at more examples of crontab next time.

Linux Schedule

People schedule things for all sorts of reason. There’s school schedules, lunch schedules… I bet Iron Man has a schedule for cleaning his suit. Which makes me wonder, what does Iron Man do for a bathroom if he’s in the suit for a long time?

Anyway, back to scheduling. Suppose I want my computer to make a beep in two hours to remind me to go watch The Avengers.It’s very important that I start watching in two hours because I want to follow it up with The Avengers: Age of Ultron and then I want to go have dinner and see Infinity War.

The Avengers!

The Avengers!

I want my computer to make a beep in two hours. The first question is, how do I make my computer beep?

One way to do it is by this:

Alpha:~ computerlamp$ echo -en "\a"

 

That’s a lot to remember, isn’t it? Let’s use nano and make a file we’ll call beep.sh

Here’s a picture of me making the file:

Making beep.sh

Making beep.sh

Remember, when you save it, call it beep.sh

The next thing to do is to change the permissions on the file. We want this to be executable.

Alpha:~ computerlamp$ chmod 755 beep.sh

Now we’ve made our first shell script. A shell script is a computer program that executes shell commands in the order that they’re in the file. So if we run beep.sh like this:

Alpha:~ computerlamp$ ./beep.sh

 

we’ll get a beep! You’ve now written a computer program. How cool is that?

Now the next problem is how do we schedule this?

We use a program called at. I know, that’s a boring name. Now it’s 11 am and I want the computer to beep me at 1pm.

Alpha:~ computerlamp$ at -f /home/computerlamp/beep.sh 01:00PM

 

Now my computer knows that at 1PM it should run the program beep.sh, which means my computer will beep at me then.

How does this work? Well, there’s a daemon called atd that runs these jobs.

What if I want to run something every hour on the hour? I want to make my own cuckoo clock by making my computer beep at me every hour. We’ll talk about how tod o that next time.