Cron and Anacron

I recently did a post on that went in depth of rc scripts and chkconfig utility that can help you setup tasks at specific events. Like when the PC boots or shuts down, generally saying whenever it enters a runlevel. But what if you want a job to run at a specific time in a day, in a week, in a month or what about in a year? Head on to find out.



If you have surfed a tech-related manual or heard someone saying, “I do that too often, So I setup a cronjob for it”. Well, I read it some time ago, and just thought that it’d be one of those dreaded things in Linux that beginners should stay away from. But, that’s actually not the case. If you know basic editors and cd commands, then you are good to go! And moreover, these terms give you the Linux swag! So, let’s get on with the details and the how-to.


Do you know what’s a daemon? A daemon is a process that runs in the background rather than being in the direct control of the user. That’s the d in the crond. Crond is the daemon that schedules the commands to be run at a specific time. But how does it know which command to be run? Simple, it stores the time and the command in a file. The file is known as crontab which is short for crontable.

These files are stored in 3 directories basically.


The first one is the system’s crontab . Only root can edit it. Other than the time and command it also includes the username under whom that command is to be run. It also inherits basic environment variables like MAILTO  and PATH.

cron.d is helpful for packages that wish to run a cronjob, and for sake of more customization, it doesn’t inherit the environment variables.

Final is the /var/spool/cron this consists the crontables with the details as required. If you have one, that there will be a file with your username and if the root has one, then, with root name too.

cron checks these directories using 2 methods. Using modtime and another one is inotify . modtime checks the crontables every minute and if the modtime is changed then the file is checked. In inotify which starts when the daemon is started, whenever a crontable is changed, it accesses the crontable.

to disable inotify -i flag can be used with crond command.

Now, let’s go to creating a crontab file.

A crontab file has the following syntax, I’d also include a screenshot of /etc/crontab file. It has wonderfully depicted the format. And for more details, you can also read the file format man page for crontab. For this type:

man 5 crontab

The syntax for crontab file goes like:

minutes hours day month weekDay command

Note that I skipped the user-name part. It is not necessary if you are editing the crontab file as yourself. No root. If you simply use crontab command. But if you use system’s crontables, then you need to specify the username. If you simply type:

crontab -e

This will then open up a temporary file using the editor set by your environment variable. If the syntax is correct then that crontab file will be saved in /var/spool/cron under your username.

minutes, hour go normally and can be zero. The day can’t be zero. Similarly month can’t be zero. But if you specify 0 or 7 in weekday, it’ll be counted as Sunday. For months and weeks, you can also use three letter names like: jan, feb, mar, apr, may and for weekdays: sun, mon, tue, wed, thu and so on. And you can specify to* consider all accepted values. Like * for weekdays will consider all days in the week.

Other ways to enter them are making a list of values like for crontab to be run at 2, 8, 17 of Jan every year, you can do like:

0 0 2,8,17 * * echo "Hello world!"

Notice there is no space in between the items of the list.

Similarly, range can be specified by a dash. For example 0-15 minutes. Moreover, you can specify the steps also! For example, if you want the command to run on odd days you can simply put this in the days slot:  */2 so it would first take 1, and then increment with the step 2 so the next date would be 1+2 = 3. Syntax is : [/steps]

To list crontab for yourself you can use:

crontab -l

Remember that you can also enter the same line for yourself by typing crontab and then enter the line. But the major drawback is that it’d keep only the content you entered and would delete the existing contents of the crontable. So avoid using it. And to exit from the input box press ctrl + d .

You will occasionally find a run-parts command. This command is used to run all the executable files in a folder. For example, if you see the file: /etc/cron.d/0hourly it’d include one such case. This helps to group scripts with similar time requirements under a single folder. 🙂

And to deny or allow the rights to cron you can write the user-name in cron.allow or cron.deny Each time a user tries to add or delete a cron job, access is checked using these files. They only contain one username and nothing else in one line.


Now, setting the timed commands you felt such relaxation that you wished to go for a walk and shut the PC so that the poor device can rest after your rigorous cron research. You come back hail and hearty and find that the service scheduled to run at the time the PC was shutdown was simply skipped. And then you know, it’s time to learn another command probably that doesn’t skip the timed task. That’s when you find anacron!

Anacron reads the job list from /etc/anacrontab . And here job entries specify period instead of exact time. What anacron does special is, when it goes to execute the job, it first checks if the job has been executed earlier in the said time period. If it hasn’t been executed within that time, then anacron runs the command specified.

Lets quickly now brush up on the format of anacrontab file.

Its basic format is like:

Period-In-Days  Delay-in-minutes  Job-Identifier Command

Days can be specified by an integer, and also by  a macro. Like : @daily @weekly @monthly .

Minutes delay tells the minutes after calling of anacron the job is required to start. It prevents many jobs to get started at same time.

Job-Identifier, is a name with which entries would be made in log files.

Command is what you want to do.

You can also set environment variables like START_HOURS_RANGE , like :


Notice there are no spaces around the equal symbol.

Sample file can be of the sorts:

2  3  backupScript  /home/USERNAME/

You will find a command named nice in files like /etc/anacrontab . This is a command, not an identifier. Nice commands deals with how nice a process is. If a value of 19 is specified then it means the program is very nice, in the sense, it’ll allow others to run first. And for highest priority you can make it -20. To test it, you can create a process using nice command and then watch its nice value using ps -o pid,comm,nice -p Specifying, nothing with nice, gives nice value of 10.

Cron something useful, or maybe anacron? 🙂



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s