Cron

Cron allows to start programs periodically. Every user can have its own settings.

Every full minute cron checks for some scheduled action, therefore it has a resolution of one minute.

To not disturb cron, cron is edited via crontab -e that opens an editor.

The sequence in this cron table is is: minute, hour, day of the month,month,day of the week

* means does not care

A */15 in the minute position means start at 0min 15min 30min 45min.

There are different cron implementations and therefore differences in the configuration files.

There is also a cron guide:

https://wiki.gentoo.org/wiki/Cron

Vixie-cron

Vixie-cron works different than other crons. It has cron tables (crontab) per users and not the single central /etc/crontab. So editing /etc/crontab will not make you not happy. Also Kcron fails with vixie-cron for the same reason. What a pain (except you know why)?

/var/spool/cron is where cron holds its temporary files. Every user can have a crontab file in /var/spool/cron/crontabs. The file /var/spool/cron/crontabs/root is everything that root did and is waiting to be served.

Cron users have to be in the user group cron. Additionally, vixie-cron supports two files to control who can use cron:

/var/spool/cron/allow holds all users that are not allowed, if empty all users of the cron group are allowed.

/var/spool/cron/deny holds all users that are not allowed

The syntax of vixie-cron entries is the following:

min hour day month weekday command

This table is usually not being edited directly to avoid that cron conflicts with the user.

Create the script /root/rootscript and make it executable

#! /bin/bash
date >> /tmp/cron.txt
echo the root script did it >> /tmp/cron.txt 

now do:

crontab -e

Then a editor opens put the following line in and exit. Don't be worried about the strange temporary filename, it is that cron will not try to run a file that you have currently opened, blocked and maybe with a wrong syntax, since you are in the middle of editing it. When done cron takes care and merges the entry into /etc/crontab.

*/2 * * * * /root/rootscript

Note: It follows the same syntax as in /etc/crontab except the user is missing.

Every two minutes 0,2,4.... the script is executed what can be observed in /tmp/cron.txt

Be aware * */2 * * * /root/rootscript

does not trigger two hour, it continuously triggers every two hours with the cron clock, to trigger it every two hours write 0 */2 * * * /root/rootscript

Instead of editing it manually, it is also possible to create some file on (a more safe place than the /var directory) and let vixie-cron read out the contents:

crontab<some filename>

After a while the modification of the crontab will be seen with the command crontab -l that shows what is scheduled.

crontab -l will show the cron tables of the current user

crontab -l -u <username> of an user

To remove cron entries editing the crontabs are not recommended, do instead:

crontab -r

or

crontab -r -u<username>

Other crons

Other crons are dcron and fcron and require after setup the command:

crontab /etc/crontab

They have a central cron table that has the same syntax are /home/lindegur/Desktop/vixie-cron except the username is also present. This central cron table is /etc/crontab

A more easy way (that does not work with vixie-cron) than editing /etc/cron.weekly is putting a script into one of the directories /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly or /etc/cron.monthly.

Create a script, make it executable and copy it into /etc/cron/hourly

#! /bin/bash
date >> /tmp/cron.txt
echo /etc/cron.hourly did it >> /tmp/cron.txt

Some hours later check /tmp/cron.txt

Frontends

Finally there are some graphical front ends as Kcron. However Kcron does not work with vixie-cron.


Linurs startpage