Linux Cron Guide

    • Official Post

    Linux Cron Guide


    The question of how to make cronjobs run at a specific time crops up often so here is a quick reference guide.


    Introduction


    Every user as well as administrator of the linux system very often needs to execute some programs on regular basis.
    For example, an administrator may need to monitor a disk usage of a system. In this case a cron scheduler is very handy tool to achieve this. Let's say that root needs to execute /usr/local/sbin/backup.sh script every Sunday at 2:36AM he would edit his crontab file as shown on the figure below:


    Code
    crontab -e



    The format is simple, 6 fields separated with spaces or tabs. The rest of the line is the command, and it's parameters to be executed. The sixth field - user name (in blue) is used only in the system wide cron scheduler.





    Cron job examples


    Crontab Example 1: This crontab example runs updatedb command 35 minutes past every hour.

    Code
    35 * * * * updatedb


    Crontab Example 2: This crontab example runs /usr/local/bin/diskusage.sh every 5 minutes (e.g. 0, 5, 10, 15, ...).

    Code
    */5 * * * * /usr/local/bin/diskusage.sh

    [HR][/HR]


    Crontab Example 3: This crontab example runs /usr/local/bin/diskusage.sh at 1:25 AM, 1:50 AM every Tuesday and on 15th of every month.


    Code
    25,50 1 15 * 2 /usr/local/bin/diskusage.sh

    [HR][/HR]


    Crontab Example 4: This crontab example runs /usr/local/bin/diskusage.sh at 2:00 PM on 10th of March, June, September and December.


    Code
    00 14 10 3,6,9,12 * /usr/local/bin/diskusage.sh

    [HR][/HR]


    Crontab Example 5: This crontab example runs '/usr/local/bin/diskusage.sh user@linuxconfig.sh' at 9.00 PM every Monday, Wednesday, Friday. Note: Using names for the week day and months is extension in some versions of crontab.


    Code
    00 21 * * Mon,Wed,Fri /usr/local/bin/diskusage.sh user@linuxconfig.sh

    [HR][/HR]


    Crontab Example 6: This crontab example runs /usr/local/bin/diskusage.sh every 5 minutes during the 5 working days (Monday - Friday), every week and month.


    Code
    */5 * * * 1-5 /usr/local/bin/diskusage.sh

    [HR][/HR]


    Crontab Example 7: This crontab example runs /usr/local/bin/diskusage.sh every minute during the 4-th hour only in Sunday, every week and month. This is every minute from 0:00 till 0:59, then from 4:00 till 4:59, etc.


    Code
    * */4 * * sun /usr/local/bin/diskusage.sh

    [HR][/HR]

  • master G

    Added the Label Article

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!