Working with Cron
Task scheduling is an important part of the Industry I belongs to. So when I first met with the scheduling, I found a powerful tool 'Cron' that is for me, is an inbuilt tool of UNIX. I'm a Linux user, so it becomes easy for me to introduce myself to such a powerful tool. you just have to schedule the tasks once and the 'Cron' will handle the rest.
Steps to schedule a task in Linux
You just need to perform a few simple steps to schedule the task.
- Open the terminal and type < $ crontab -e > to edit or write a task/job.
2. Select your text editor. In my case, Linux asked me to select an editor.
3. When you have done. Cron file will be open in your editor. just put your command here.
Format of the schedule
A Crontab schedule consists of 6 fields on a single line and separated by space.
Format: minute hour day month day-of-week command-to-execute(* * * * *).
Minute takes the value 0 - 59
Hour takes the value 0 - 23
Day takes the value 1 - 31
Month takes the value 1 - 12
Day-of-week takes the value 0 - 7
Your command will look like
If we want a Cron job to execute at 8 am every day, we write:
0 8 * * * /command/to/execute
If we want a commend to execute at 8:00 am on 1st January, we write:
0 8 1 1 * /command/to/execute
How to list all the Cron Jobs?
$ crontab -l
If in case, cron job returns an output, by default it will mail it. you have to install and configure your mail delivery agents (MTA) like postfix. for example, I installed postfix in my ubuntu.
$ sudo apt-get install postfix
If you want to save the output in logs, you can simply add 2>&1 | logger -t <tag name> in your command by editing Cron.
how to see these logs
$ grep 'log' /var/log/syslog
Where 'log' is the tag name that I have mentioned above.