In this article I am going to discus one of the most promising feature of *nix systems.
1. What is crontab
crontab is the functionality offered by almost all *nix systems, which gives us the ease to automate any action on the machine at specific time frame.
2. How the crontab looks
crontab looks like five * marks following by a specific command. these five * marks do carries different meaning.
$ crontal -l
* * * * * /usr/home/sudarshan/myScript.sh
3. Defination of five * marks
These five * marks in the beginning of every cron entry do have following meaning -
* - Minute when the cron will execute (* means every minute)
* - hour when the cron will execute (* means every hour)
* - date when the cron will execute (* means every day of a month)
* - Month when the cron will execute (* means every month)
* - day of week when the cron will execute (* means every day of week)
Examples -
*/5 * * * * script (script will be executed every five minutes)
5 10 * * * script (script will be executed everyday at 10:05 AM)
15 18 * * WED script (script will be executed every wednesday at 6:15 PM)
5 11 3 * * script (script will be executed on 3rd of every month at 11:05 AM)
5 13 5 6 * script (script will be executed on 5th June at 1:05 PM)
4. How to view/edit crontab entry
crontab entries can be viewed/edited by the help of following commands -
crontab -l (list down all the crontab entries for current user)
crontab -u username -l (list down all the crontab entries for a specific user*)
crontab -r (removes crontab entries for current user)
crontab -u username -r (removes crontab entries for a specific user*)
crontab -e (edit crontab entries for current user)
crontab -u username -e (edit crontab entries for a specific user)
* -- for another user specific commands you need to be root
5. Best practices when setting a crontab
It is always better to set crontab entries from a external file. Benefits of this are.. if someone or you by mistake removes the crontab entries then you don't have to remember all the entries. You simply need to call the same file again and you will get your cron entries back.
personally this happened with me as I had more then 25 cron jobs in my crontab and suddenly I lost all of them, as by mistake I hit 'crontab -r' command.
So to keep the sanity it is advised to set the cron jobs from a file.
you can write all from cron jobs in any file and set the file in your crontab.. make sure that you copy all the previous cron jobs and incorporate them in your file.. as by using the file you will loose your initial cron job entries.
command to do the same -
crontab /path/to/the/file/crons.txt
6. Setting PATH in crontab
there can be a situation that you will be having multiple scripts in a directory which you want to call in cronjob. For this you don't have to mention the entire path every time.
Simply the pach then the entry -
PATH=/path/to/scripts/
* * * * * script1.sh
* * * * * script2.sh
* * * * * script3.sh
7. Keywords in crontab
This is quite interesting. crontab do support miltiple keywords for entries -
@yearly script (this will execute the script file at 00:00:00 Hrs on 1st Jan of every year)
@monthly script (script execution on 1st minute of every month)
@daily script (script execution on first minute of everyday)
@reboot script (script will be executed everytime when machine is rebooted)
Have fun with one of the most interesting part of *nix systems.
No comments:
Post a Comment