Cron is a Linux service that runs in the background. On a dedicated server, cron is extremely valuable because you can automate repetitive commands and tasks. For example, if you want a certain log to be purged every day, you can create a script to perform the task and then tell cron to run the script daily.
There are two primary ways to tell cron when to run something. The first is to edit /etc/crontab and enter the exact day and time for the task to run. The second method is to copy your script or link to it in one of the cron directories: cron.daily, cron.hourly, cron.monthly, or cron.weekly.
The second method is just as easy as it sounds. The first requires a little more thought. Just follow this brief guide.
1. Login to your server (either directly or via SSH)
2. Become root:
(then enter your password when prompted)
3. Decide how frequently you want your command to execute. For this example, we will run our task each day at 6:30 PM.
4. Edit the crontab file:
nano /etc/crontab OR
Crontab is divided into five columns: Minute, Hour, Day of the Month, Month, and Day of the Week.
5. On a new line, enter the following string:
30 18 * * * your-command
30 means thirty minutes after the hour. 18 is the hour of the 24-hour day, and each * means that you want every day of every month, in every month, and every day of the week.
As another example, let’s say you wanted a cache file removed at only midnight on the 1st, 7th, and 14th of each month. You would enter:
0 0 1,7,14 * * rm /home/user/public_html/cache/filename
6. Finally, cron sends an email to the default user every time a job is successful. If you do not need this, put this on the last line of the file:
Save the crontab file, and you are all done.
- How to Create Cron Jobs with Webmin
- How to Change the Timing of Cron.daily
- Scheduling Tasks on a Windows Server
- How to Automatically Optimize MySQL Databases
- Server End of Year Tasks 3: Remove Deactivated Accounts