Cron is the system by which periodic jobs can be scheduled. Anacron is an optional supplement to cron for machines that aren’t running 24 hours a day.
Note: I do not really understand cron and anacron yet. These notes to myself represent my still incomplete self-study of the topic and might not be of much use to others. Your best bet is to see the references below, particularly the first.
In /etc/cron* is found the system cron; best to leave this for system jobs. Better is to use user-specific entries, which these notes illustrate with an example of tasks to be run daily at 1:30 pm. Root is used as an example only. You should run your cron jobs as a user with the least permissions needed.
1. As root, save the following as /root/crontab_daily, making it executable:
#!/bin/sh # Performs daily cleanup # Debugging: Makes a trace in the output, which will be mailed to root # set -x -v # Remove vtiger database backups older than 2 days find /home/user/vtigercrm_bak/ -type f -mtime +2 -delete # Remove user .xauth files older than 2 days find /home/user/ -name ".xauth*" -type f -mtime +2 -delete
Before doing so, make sure that each line executes properly from the terminal. After saving it, check your work by executing it manually: as root, “/root/crontab_daily”.
2. Save the following as /root/crontab to run the above script at 1:30 pm daily:
30 13 * * * nice -n 19 /root/crontab_daily
See the references for an explanation of the fields, but a few common ones are:
- * Every increment (e.g. every minute, every hour)
- */10 Every 10 increments (e.g. every 10 minutes, every 10 hours)
- 2,4 Every 2nd and 4th increment (e.g. every hour at 2 minutes and 4 minutes past the hour)
The last line of the file must be blank (not shown above), which is good practice in any case.
3. Then, make it so: as root, “crontab /root/crontab”. Check your work with “crontab -l”. Further check your work by waiting and allowing cron to run the script. Because it is niced, don’t expect it to run at the exact moment specified.
To check for errors from cron, look at /var/log/cron/*
Add “set -x -v” as the first line in the script after the hashbang, so that there will be a trace in the output, which will be mailed to root. Make sure you have postfix installed, and the alias for root set to your id in /etc/postfix/aliases.
CRON JOBS WITH WEBMIN
Being lazy, I prefer to create cron jobs with Webmin. To illustrate its use, let’s give ourselves the task of performing a daily deletion of messages in Evolution’s local Sent mail folder that are more than 30 days old.
Log in to webmin, usually found at https://localhost:10000/. In System – Scheduled Cron Jobs – Create a new scheduled cron job, fill out the form as desired. Following our example, that would be:
- Execute cron job as: Your username (a good choice for this example) or an appropriate system user. Always run cron jobs as a user with the lowest permissions necessary for the task.
- Active?: Yes
- Command: archivemail –quiet –delete –days=30 /home/user/.evolution/mail/local/Sent (changing the path as apprpriate)
- Input to command: Here, left blank
- Description: “Delete locally stored sent mail more than 30 days old”
- When to execute: “Simple schedule; Daily (at midnight)”
- Date range to execute: “Run on any date”
Press “Create”. See how much easier it is to use Webmin?
CRON JOBS IN CPANEL
Select Advanced – Cron Jobs. In the Cron Email Notification section, ensure that email notification is enabled. Select “Update Email Notification” and enter your email address.
In the Add New Cron Job section, enter settings as desired and add the job. For testing purposes I set the job to run today at a specified time in a few minutes.
When the job runs next, you will be sent an email confirmation. Once you have confirmed that the cron job works as desired, return to delete the testing job. Create a new job with the time set to run at the desired time.
CRON JOBS ON PAIR NETWORKS
Enter Pair’s Account Control Center. Open Advanced Features – Manage Cron Jobs – Add New Cron Job. Set the frequency of the job and select “Proceed”. Set the time to run the job. For testing purposes I set the job to run today at a specified time in a few minutes. Enter your command, in the form “/path/to/executable /path/to/file”, using the full path to the executable.
Select “Proceed”. Check that your email address for cron job output is correct. You are done; wait for an email confirmation of the job.
Once debugged, return to delete the testing job. Create a new job with the time set to run at the desired time. Whenever practical, it is good form to allow the script to run when the server is at low load.