Crontabs
What is a crontab?
On almost every Linux server a program is run that can execute commands on specific time intervals (indicated by the user). This way it is possible to have a backup created, check external mailboxes every minute or send a mailing every Friday. Every system user can administer their own crontabs by editing a text file which contains the time intervals and commands to be executed.
At Nxs Internet you can configure a crontabs on shell.nxs.nl.
How does it work?
Every line in your crontabs means literally “run this command on this time interval”. These lines are defined as follows:
[minute] [hour] [day of month] [month] [day of week] [command] 0-59 0-23 1-31 1-12 0-7 (0 and 7 are sunday) /path/to/script
Note: Every line is closed by a line feed (Enter), otherwise it is not active.
Note some remarks:
Instead of a number also a wildcard, a '*' can be entered, which comprises every value of the field concerned. A * in the minute field means ‘every minute’. The following command makes sure that the script ‘runme.sh’ is run every minute:
* * * * * /home/username/script.sh
It is also possible to generate a list of values, separated by commas; the following line makes sure that the script is run at 9.05 AM and 5.05 PM:
5 9,17 * * * /home/username/script.sh
The dash can also be used to define a series, this line makes sure that the command is executed every minute from 9 AM to 4.59 PM:
* 9-16 * * * /home/username/script.sh
It is also possible to enter a slash, as follows:
*/7 * * * 1 /home/username/script.sh
This can also be written as::
0-59/7 * * * 1 /home/username/script.sh
And means:
0,7,14,21,28,35,42,49,56 * * * 1 /home/username/script.sh
Moreover, you can use the names of the days and months, which can make the command above more clear and turn it into the following:
*/7 * * * monday /home/gebruikersnaam/draaimij.sh
Note: Days are defined in two different ways, which can lead to confusion. The rule that needs to be followed is: If both fields do not contain a wildcard (a ‘*’), a combination of the fields is active. Have a look at the following:
* * 1,10 * 5 /home/username/script.sh
This will run the script:
- Every minute on the first and tenth of every month, and
- Every minute on each Friday
Furthermore, you can add remarks by typing‘#’ at the beginning of each line, for instance:
#this is a remark * * 1,10 * 5 /home/username/script.sh
How do I configure my crontabs with Nxs?
First, you need to be logged in at shell.nxs.nl with a SSH connection. You can read more about this in the 'SSH' section of the helpdesk on our website.
If you are logged in, you can check the crontabs by executing the command ‘crontabs-I’, the command 'crontabs-e' allows you to edit the text.
Again to be clear: Every line is closed by a line feed (Enter), otherwise it is not active.
You can enter insert mode by clicking ‘i’. If you finish entering your lines, exit insert mode by clicking ‘Esc’, next, save your file by pressing [SHIFT] z (2x).
A good way of testing your crontabs is the ‘touch’ command, which will change the date of files to the current time and create files if they didn't exist before.
The following command should create ‘file' every minute:
* * * * * touch /home/username/file
By checking the files, you can see when the script has been executed. You can do this by executing the command "Is- -I" on shell.nxs.nl, after which a list will appear, containing all files and their date:
[username@shell ~]$ ls -l total 0 -rw-r--r-- 1 username vhosting 0 Feb 24 14:52 file [username@shell ~]$

