go to  ForumEasy.com   
JavaPro  
 
 
   Home  |  MyForum  |  FAQ  |  Archive    You are not logged in. [Login] or [Register]  
Forum Home » Java Deploying » crontab -- schedule tasks on Linux
Email To Friend  |   Set Alert To This Topic Rewarding Points Availabe: 0 (What's this) New Topic  |   Post Reply
Author Topic: crontab -- schedule tasks on Linux
Linux
member
offline   
 
posts: 120
joined: 01/24/2011
from: San Jose, CA
  posted on: 09/03/2012 05:10:11 PM    Edit  |   Quote  |   Report 
crontab -- schedule tasks on Linux
If you want some maintenance tasks (creating backups, synchronizing caches, and scheduling update on statistics) to be automatically run in the background at regular intervals, you can achieve it on Linux by crontab.

The crontab (cron derives from chronos, Greek for time; tab stands for table) command, found in Unix and Unix-like operating systems, is used to schedule commands to be executed periodically. To see what crontabs are currently running on your system, you can open a terminal and run:
$ sudo crontab -l

To edit the list of cronjobs you can run:
$ sudo crontab -e

 Profile | Reply Points Earned: 0
Linux
member
offline   
 
posts: 120
joined: 01/24/2011
from: San Jose, CA
  posted on: 09/03/2012 05:27:01 PM    Edit  |   Quote  |   Report 
Crontab format
Cronjobs are expressed in two formats: in number or in keyword.

Numberical format
* * * * * /bin/execute/this_script.sh


As you can see there are 5 stars. The stars represent different date parts in the following order:
  • 1st * -- minute (from 0 to 59)
  • 2nd * -- hour (from 0 to 23)
  • 3rd * -- day of month (from 1 to 31)
  • 4th * -- month (from 1 to 12)
  • 5th * -- day of week (from 0 to 6) (0=Sunday)

    Note: If you leave the star, or asterisk, it means every. By default, ***** means that the task is being executed every minute.

    Keyword format

    @<keyword> /bin/execute/this_script.sh
    


    Here are some useful keywords instead of a number:
    @reboot     Run once, at startup
    @yearly     Run once  a year     "0 0 1 1 *"
    @annually   (same as  @yearly)
    @monthly    Run once  a month    "0 0 1 * *"
    @weekly     Run once  a week     "0 0 * * 0"
    @daily      Run once  a day      "0 0 * * *"
    @midnight   (same as  @daily)
    @hourly     Run once  an hour    "0 * * * *
    


  •  Profile | Reply Points Earned: 0
    Linux
    member
    offline   
     
    posts: 120
    joined: 01/24/2011
    from: San Jose, CA
      posted on: 09/03/2012 05:33:14 PM    Edit  |   Quote  |   Report 
    Examples
    Example #1. Run task at 1AM every Monday
    0 1 * * 5 /bin/execute/this_script.sh
    



    Example #2. Run task at 1AM every Monday to Friday
    0 1 * * 1-5 /bin/execute/this_script.sh
    



    Example #3. Run task daily
    @daily /bin/execute/this_script.sh
    

     Profile | Reply Points Earned: 0
    Linux
    member
    offline   
     
    posts: 120
    joined: 01/24/2011
    from: San Jose, CA
      posted on: 09/07/2012 05:28:54 PM    Edit  |   Quote  |   Report 
    Where is my crontab file actually stored?
    Check here:

    /var/spool/cron/crontabs/
    
     Profile | Reply Points Earned: 0
    Linux
    member
    offline   
     
    posts: 120
    joined: 01/24/2011
    from: San Jose, CA
      posted on: 09/07/2012 05:43:32 PM    Edit  |   Quote  |   Report 
    What's file /etc/crontab then? What's difference between /etc/crontab and /var/spool/cron/crontabs ?
    There are both crontab files with different format. They are both loaded when system is on but run in different environments:

    /var/spool/cron/crontabs/ --> user defined and run in user's environment

    /etc/crontab --> system's environment

    Here is the default content of /etc/crontab
    $ sudo cat /etc/crontab
    
    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root
    HOME=/
    
    # run-parts
    36 * * * * root run-parts /etc/cron.hourly
    50 1 * * * root run-parts /etc/cron.daily
    0 4 * * 0 root run-parts /etc/cron.weekly
    30 3 14 * * root run-parts /etc/cron.monthly
    
    


     Profile | Reply Points Earned: 0
    Linux
    member
    offline   
     
    posts: 120
    joined: 01/24/2011
    from: San Jose, CA
      posted on: 09/07/2012 06:35:17 PM    Edit  |   Quote  |   Report 
    /etc/cron.d vs. /etc/cron.daily
    What's difference between those two directories /etc/cron.d and /etc/cron.daily ?

    As shown above in system crontab file /etc/crontab, all scripts within directory /etc/cron.daily will be executed daily (50 1 * * *) by system root's environment . For example, the script logrotate which rotates log file is a very good example to be here, as it needs to be run daily to ensure the disk capacity.

    $ cat /etc/cron.daily/logrotate
    
    #!/bin/sh
    
    /usr/sbin/logrotate /etc/logrotate.conf
    EXITVALUE=$?
    if [ $EXITVALUE != 0 ]; then
        /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
    fi
    exit 0
    


    /etc/cron.d, however, contains scripts of package level. In addition to read /etc/crontab, cron reads as well the files in /etc/cron.d: it treats the files in /etc/cron.d as in the same way as the /etc/crontab file (they follow the special format of that file, i.e. they include the user field). However, they are independent of /etc/crontab: they do not, for example, inherit environment variable settings from it. The intended purpose of this feature is to allow packages that require finer control of their scheduling than the /etc/cron.{daily,weekly,monthly} directories to add a crontab file to /etc/cron.d.

    So, two key points:
    1) Varies in control;
    2) Varies in environment settings.

    For example, the script awstats which update web access statistics based on access_log is a very good example to be here, as it needs to be run on package level (per vHost) and on finer control (you may want to run it at different time frame for each vHost).

    List of scripts:
    $ ls -l /etc/cron.d
    -rw-r--r--  1 root root  47 Jul  7  2008 awstatcronsetup
    -rw-r--r--  1 root root 151 Sep  6 23:55 awstats
    


    AWStats cron setup:
    $ cat /etc/cron.d/awstatcronsetup
    
    55 23 * * * root /usr/share/awstats/cronsetup
    
    


    AWStats script setup:
    $ cat /usr/share/awstats/cronsetup
    
    #!/bin/bash
    
    hour=`echo $[$RANDOM % 24]`
    
    if [ $hour -gt 9 ]; then
    	echo "00 $hour * * * root /usr/share/awstats/awstats_updateall.pl now 
        -awstatsprog=/var/www/cgi-bin/awstats/awstats.pl 
        -configdir=/etc/awstats/ >/dev/null 2>&1" >/etc/cron.d/awstats
    else 
    	echo "00 0$hour * * * root /usr/share/awstats/awstats_updateall.pl now 
        -awstatsprog=/var/www/cgi-bin/awstats/awstats.pl 
        -configdir=/etc/awstats/ >/dev/null 2>&1" >/etc/cron.d/awstats
    fi
    
    /sbin/service crond restart >/dev/null 2>&1
    
    


    AWStats update cron script:
    $ cat /etc/cron.d/awstats
    
    00 18 * * * root /usr/share/awstats/awstats_updateall.pl now 
    -awstatsprog=/var/www/cgi-bin/awstats/awstats.pl 
    -configdir=/etc/awstats/ >/dev/null 2>&1
    
    


     Profile | Reply Points Earned: 0

     
    Powered by ForumEasy © 2003-2005, All Rights Reserved. | Privacy Policy | Terms of Use
     
    Get your own forum today. It's easy and free.