Monday, August 4, 2014

Crontab Command in Linux Environment

As i learned the previous week, crontab is useful in setting events that should happen in a periodic basis , automatically if set.
Cron runs in background (Daemon) as default in linux, checks for particular default location "etc/crontab file".

Setting cron has the order as follows :

It has 5 parameters
1)minutes (0-59)
2)hours (0-23)
3)day of month (0-31)
4)month (1-12)
5)day of week (0-7)

Special Characters used :
Asterik(*) – Match all values in the field or any possible value.
Hyphen(-) – To define range.
Slash (/) – 1st field /10 meaning every ten minute or increment of range.
Comma (,) – To separate items.

Commands :
1) Login as root and type" crontab -l" -> This will list the existing root crontab list
2) To view all user entries login as "crontab -u <user> -l"
3) To edit crontab "crontab -e" -> This will deafult edit the currently login user's default crontab ( /tmp/file will be open for editing)
4) To remove currently running / created crontab -> "crontab -r"

Sample setting of a crontab :
  • *17 * * 1 date >> ~/testCron.log
    --> This will set cron for every monday(0 index for sunday) 5 p.m. We can pass the value printed to a log file and can trace the execution.
  • */10 * * * * date >> ~/testCron.log
    --> This "*/10" part means this will execute for every 10 minutes
    So we can wait for 30 minutes and check it the logfile is update 3 times, for every 10 minutes.