Tuesday, October 14, 2014

Lesson 32: how to schedule a crontab/job

Lately i need to create 2 cron jobs for my work in office. It's quite easy actually, all you need to do is just edit the current cron jobs with command crontab -e and then add another line of command you wish to execute with cron, after you're finished just save the crontab like you save ordinary file in linux. You can view the crontab list using the command crontab -l

The one that's interesting I think is the combination of interval used to run a cron job.
For example if you want to run a job every minute you can use this line of command:
* * * * * /opt/some-script.sh
all those five "*" symbols have their own meaning:
# field #   meaning        allowed values
# -------   ------------   --------------
#    1      minute         0-59
#    2      hour           0-23
#    3      day of month   1-31
#    4      month          1-12 (or names, see below)
#    5      day of week    0-7 (0 or 7 is Sun, or use names)

For example when you want to 5 minutes after every hour:
# this command will run at 12:05, 1:05, etc.
5 * * * * /usr/bin/some-script.sh

For crontab to run everyday at 4.30 am
30 4 * * * /opt/bin/some-script.sh

If you want to run a cron job every 5 minutes you can use this command:
*/5 * * * * /var/opt/bin/somescript.sh

Recently, I just found out a website that can check a cronjob and give the meaning
http://cronchecker.net/

All the examples above are taken from this web
For additional examples you can check this web

Anyway, if you're not sure about the cron job you created, you can just cross-reference with cronchecker website.

No comments:

Post a Comment