Crontab

On Unix systems, the Cron daemon is used to compute time based commands. With this it is possible to plan tasks and to execute them at a defined time. It may sounds a bit complicated but the configuration for this is quite simple. On a Debian based system you can find the crontab here:

Configuration

You can setup new task by editing the crontab table. You can start by defining a computation time (minute, hour, day, month, year). A * can be used to define that this should be done multiple times. If you set 0 0 * * *, then the task would be executed each year, each month and each day on 0:00. This means daily. There are many other combinations possible. You can find more on this at Wikipedia or here.

After you configured date and time you have to set user and command. This command is the same you would type into command line.

Anacron

It is possible that there are already task in that crontab table. There may be a line with a command like /usr/bin/anacron. If you can see this, then there are normally 4 pre defined tasks /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly. There are also directories for this 4 entries. But whats this?

If you place a script into one of this pre defined directories, then it will be executed at some time. If you put it into cron.daily, it will be executed once a day. But when? This information can be found at our crontab table. But wait! You are using Anacron. The difference to Cron is, that the scripts are executed exactly at the defined time, but when you computer is powered off at night, a script for 0:00 will never be executed with Cron. With Anacron it will be executed if it is marked with daily, so it is guaranteed, that if you turn on your machine every day, that the daily script is computed once.

Cron daemon

Cron is a daemon, so it runs in background and waits for the next script launch. If Cron is not running, you can start it with:

You can also stop it with ‘stop’ or restart it with ‘restart’ (if you changed the crontab for example).

Problems?

If scripts are not executed without an obvious reason, then you should check the logs. Cron logs can be found here:

To only display Cron related content, use the following command:

Normally the log messages are very good and helpful. If you cannot find the problem, an internet search may help. You can also ask me.

(Visited 343 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *