Below will be the list of code you will use + an example in which you can see if you have set up a Cronjob correctly. Yes, I'm one of those people whom enjoy peace of mind - knowing something works is better than thinking something works.
Here's what you need - but don't execute them yet, keep reading:
$ sudo crontab -e # To access the root cron file
$ sudo touch /etc/cron.d/ # To refresh the cron process with your new settings
The first time you access the crontab file, it will ask you what editor you wish to use - I use nano. I'm puzzled about the location of the crontab file as I originally thought /tmp files were deleted on shutdown. Anywho...the default crontab might look like this:
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
The information you need to know about the Cronfile, and editing, it are in the Cronfile itself. But here's a quick run down.
You add a cronjob by using the following syntax:
* * * * * /path/to/crontest.sh
Notice there are 5 positions. From left to right they specify:
- Minute, 0 - 59
- Hour, 0 - 23
- Day of month, 1 - 31
- Month, 1 - 12
- Day of week, 0 (Sunday) - 6 (Saturday)
Testing Crontab/Cronjobs
Here's a quick way to test crontab.
1) Open terminal and move to a directory of your choice.
2) Make a new file and call it "crontest.sh".
3) Give crontest.sh user executable permissions. Or 755, which also enables write-perms:
$ sudo chmod u+x crontest.sh
4) In crontest.sh, add this script. This is just a script that echos the date, given the format you desire.
#!/bin/bash
date +"%d-%m-%Y" >> /path/to/the/storage/file.txt # Date ID, >> tells terminal to append to end of file
Save and exit.
5) Open up the crontab file:
$ sudo crontab -e
6) Look at the current time and edit the crontab file with a few minutes added to the current time. So, if the time is 17:40, make the crontab entry look like this:
...
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
45 17 * * 1-5 /path/to/crontest.sh
Close nano and save the changes.
7) Now refresh crontab:
sudo touch /etc/cron.d/
Giving your password where required.
8) Sit and wait. When 17:45 comes around, the script will be run. Check the /path/to/the/storage/file.txt file and see if the date is there. If yes, then your method is good. If no, then check that permissions are correct; check that the code and all pathways are right.
Hope you enjoy this HowTo. I'm probably going to make a cronjob to append CPU temps to a file such that I can graph them and see if certain parameters are better/worse for my CPU performance.
Here are two sites which I found helpful:
http://klenwell.com/press/2010/11/cron-d/
http://kevin.vanzonneveld.net/techblog/article/schedule_tasks_on_linux_using_crontab/
Enjoy, Ciao!