Another of the most useful tools in server administration is the Crontab – you use this to schedule jobs (programs) to run at regular intervals.
These tasks can be varied, from restarting servers, services – creating files, changing permissions, runing batch programs, deleting files etc…
Of course sometimes crontab is not the best solution as it can have several problems – in which case other choices are available – including writing your own – however we will cover that another time.
First a coouple of facts about cron:
- Cron has evolved a bit since inception but the basic principles are the same – old server admins are as good as new ones with this tool!
- Cron will run new jobs at most once a minute – that’s the smallest resolution you can have.
- Old systems used to wake up every minute and check for new work – the same as a simple replacement service might do – however this didn’t scale well when cpu power was low but users high on old mainframe systems – new systems still only execute once a minute.
- the cron service loads up any relavent cron files (which exist in users home directories or in /etc/cron.d/) and saves the information so it only needs to run when there is really jobs to run – when you edit the crontab it reloads it’s list so it doesn’t need to keep checking for changes – it’s important to edit it right!
- Even if a previous program hasn’t finished – cron can run it again or run more jobs – this can bring down badly configured systems! so be careful how you use it.
Generally to edit the crontab – which is where you would add things to run type in “crontab -e” – this will open it in your system editor (vim is my choice, but others can be used).
you will probably see some content – similar to this:
1 0 * * * shutdown -r now
This means that at one minute past midnight the server will reboot (execture the immediate shutdown and restart command with no delay) each day. The * are wildcards saying that the instruction should run no matter the criteria of that column (if it’s all * then it will run each minute).
The fields are the following: I recommend pasting this in the top of your crontab file – the # at the start mean it will ignore the entries.
# .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * *
Remember if the program has output you can store it in a file the same as you can on the command line – just type ” > /var/log/logfile”
in the entry after the command and it will log output to the file in question.