Sometimes you find yourself in a situation where you want to know what your server is doing but you can't get to a secure terminal to SSH in. What do you do? You look to cron.
Cron is a daemon that can be found in Unix and Unix-like operating systems. What it allows you to do is execute commands at any hour of the day and you can even use it to run commands multiple times a day. Cron may be a daemon but it's an angel to most sysadmins.
There are two main ways to use cron. You can either put a script into a cron subdirectory or you can edit the crontab file.
Using the subdirectories is very simple. You can find them in /etc and they are: cron.hourly, cron.daily, cron.weekly and cron.monthly. As I am sure you can figure out the scripts inside these subdirectories are executed based on the name of the subdirectory. Sometimes you need a little more control over when your script is to execute, this is where crontab comes in.
When using crontab you edit the /etc/crontab and insert a line of code that tells cron when you want your command to run. You can customize the time frame down to the minute. You can have it run every 15 minutes or even have it run every 15 minutes but only between the hours of 9:00 and 17:00.
My favorite Unix command has to be top. You can get a wealth of information from this simple command but you need to be at a terminal to run it, or do you?
By using crontab you can set top to run at preset intervals throughout the day and have the results emailed to you.
*/15 * * * * root /usr/bin/top | mail -s "top results" email@example.com
That little bit of code above will have the top command run every 15 minutes (as root) and the output will be sent in an email to firstname.lastname@example.org with the subject "top results". Not bad, eh?
You could also use this to email yourself error logs, netstat information, anything really. Cron is a sysadmin's best friend.
No comments yet.
Sorry, the comment form is closed at this time.