Cron task is used to automate tasks in Unix/Linux. It is something that you need to trigger commands / scripts @ specific interval of time. It is sort of task scheduler that windows has. But it is much more customizable and is command line based. So lets see how can we add cron tasks.
Lets say we have a shell script that displays nothing but todayz date. It is named date_today.sh and for some reason you want to run it every day @ 00.00 hrs.
The first thing you should know about cron task is the format in which you specify the time for automating your tasks. There are five fields to set the time.
* * * * * <command to execute>
You need to set the five stars as per your requirement. These 5 *s are five fields for
1. minute (0-59)
2. hour (0-23)
3. day (1-31)
4. month (1-12)
5. day of week (0-6, 0 being Sunday)
6. command to execute
So now to execute our date_today.sh file, we need to add the cron task to the system. To do that issue this command:
$ crontab -e
Depending on your login, your cron tasks will differ. So once you are presented with your editor to edit the file, enter the following line:
0 0 * * * /home/user_name/date_today.sh
This will make sure that your shell script runs everyday @ midnight. If you want to run it every 5 minutes, use this instead:
*/5 * * * * /home/user_name/date_today.sh
0 1 29 1 1 /home/user_name/date_today.sh
will help you setup the execution on 1.00 AM, 29th January, Monday. You could also use this for same result….
0 1 29 jan mon /home/user_name/date_today.sh
Once you have entered the time, exit and wait for it to be executed.😀
Cron tasks can be used for several purposes like backing up data, deleting logfiles / temporary files, copying data etc. So unless you have a lot of time to spend in front of your computer for the right moment to trigger commands on your own, I think you shud be using cron tasks.😀