Advertisement

You are here: Home > Irc > Crontab


Quick Links


UnrealIRCd Crontab
Anope Services Crontab
EggDrop Crontab
psyBNC Crontab
BOPM Crontab
CentOS Crontab


The crontab is a list of commands that you want to run on a regular schedule, and also the name of the command used to manage that list.

crontab stands for "cron table," because it uses the job scheduler cron to execute tasks; cron itself is named after "chronos," the Greek word for time.


How do I add cron job under Linux or UNIX like operating system?

Cron allows Linux and Unix users to run commands or scripts at a given date and time. You can schedule scripts to be executed periodically. Cron is one of the most useful tool in a Linux or UNIX like operating systems. It is usually used for sysadmin jobs such as backups or cleaning /tmp/ directories and more. The cron service (daemon) runs in the background and constantly checks the /etc/crontab file, and /etc/cron.*/ directories. It also checks the /var/spool/cron/ directory.

crontab command

You need to use the crontab command to edit/create, install, deinstall or list the cron jobs in Vixie Cron. Each user can have their own crontab file, and though these are files in /var/spool/cron/crontabs, they are not intended to be edited directly. You need to use crontab command for editing or setting up your own cron jobs.

Types of cron configuration files

There are different types of configuration files:

  1. The UNIX / Linux system crontab : Usually, used by system services and critical jobs that requires root like privileges. The sixth field (see below for field description) is the name of a user for the command to run as. This gives the system crontab the ability to run commands as any user.
  2. The user crontabs: User can install their own cron jobs using the crontab command. The sixth field is the command to run, and all commands run as the user who created the crontab
  3. Note: This faq features cron implementations written by Paul Vixie and included in many Linux distributions and Unix like systems such as in the popular 4th BSD edition. The syntax is compatible with various implementations of crond.

    How Do I install or create or edit my own cron jobs?

    To edit or create your own crontab file, type the following command at the UNIX / Linux shell prompt:
    $ crontab -e

    Do I have to restart cron after changing the crontable file?

    No. Cron will examine the modification time on all crontabs and reload those which have changed. Thus cron need not be restarted whenever a crontab file is modified.

    Syntax of crontab (field description)

    The syntax is:

     
    1 2 3 4 5 /path/to/command arg1 arg2
     

    OR

     
    1 2 3 4 5 /root/backup.sh
     

    Where,

    • 1: Minute (0-59)
    • 2: Hours (0-23)
    • 3: Day (0-31)
    • 4: Month (0-12 [12 == December])
    • 5: Day of the week(0-7 [7 or 0 == sunday])
    • /path/to/command - Script or command name to schedule

    Easy to remember format:

    * * * * * command to be executed
    - - - - -
    | | | | |
    | | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
    | | | ------- Month (1 - 12)
    | | --------- Day of month (1 - 31)
    | ----------- Hour (0 - 23)
    ------------- Minute (0 - 59)

    Your cron job looks as follows for system jobs:

    1 2 3 4 5 USERNAME /path/to/command arg1 arg2
    

    OR

    1 2 3 4 5 USERNAME /path/to/script.sh
    

    Example: Run backup cron job script

    If you wished to have a script named /root/backup.sh run every day at 3am, your crontab entry would look like as follows. First, install your cronjob by running the following command:
    # crontab -e
    Append the following entry:
    0 3 * * * /root/backup.sh
    Save and close the file.

    More examples

    To run /path/to/command five minutes after midnight, every day, enter:
    5 0 * * * /path/to/command
    Run /path/to/script.sh at 2:15pm on the first of every month, enter:
    15 14 1 * * /path/to/script.sh
    Run /scripts/phpscript.php at 10 pm on weekdays, enter:
    0 22 * * 1-5 /scripts/phpscript.php
    Run /root/scripts/perl/perlscript.pl at 23 minutes after midnight, 2am, 4am ..., everyday, enter:
    23 0-23/2 * * * /root/scripts/perl/perlscript.pl
    Run /path/to/unixcommand at 5 after 4 every Sunday, enter:
    5 4 * * sun /path/to/unixcommand

    How do I use operators?

    An operator allows you to specifying multiple values in a field. There are three operators:

    1. The asterisk (*) : This operator specifies all possible values for a field. For example, an asterisk in the hour time field would be equivalent to every hour or an asterisk in the month field would be equivalent to every month.
    2. The comma (,) : This operator specifies a list of values, for example: "1,5,10,15,20, 25".
    3. The dash (-) : This operator specifies a range of values, for example: "5-15" days , which is equivalent to typing "5,6,7,8,9,....,13,14,15" using the comma operator.
    4. The separator (/) : This operator specifies a step value, for example: "0-23/" can be used in the hours field to specify command execution every other hour. Steps are also permitted after an asterisk, so if you want to say every two hours, just use */2.

    How do I disable email output?

    By default the output of a command or a script (if any produced), will be email to your local email account. To stop receiving email output from crontab you need to append >/dev/null 2>&1. For example:
    0 3 * * * /root/backup.sh >/dev/null 2>&1
    To mail output to particular email account let us say [email protected] you need to define MAILTO variable as follows:
    MAILTO="[email protected]"
    0 3 * * * /root/backup.sh >/dev/null 2>&1

    Task: List all your cron jobs

    Type the following command:
    # crontab -l
    # crontab -u username -l

    To remove or erase all crontab jobs use the following command:
    # Delete the current cron jobs #
    crontab -r

    ## Delete job for specific user. Must be run as root user ##
    crontab -r -u username

    Use special string to save time

    Instead of the first five fields, you can use any one of eight special strings. It will not just save your time but it will improve readability.

    Special stringMeaning
    @rebootRun once, at startup.
    @yearlyRun once a year, "0 0 1 1 *".
    @annually(same as @yearly)
    @monthlyRun once a month, "0 0 1 * *".
    @weeklyRun once a week, "0 0 * * 0".
    @dailyRun once a day, "0 0 * * *".
    @midnight(same as @daily)
    @hourly Run once an hour, "0 * * * *".

    Examples

    Run ntpdate command every hour:
    @hourly /path/to/ntpdate
    Make a backup everyday:
    @daily /path/to/backup/script.sh

    More about /etc/crontab file and /etc/cron.d/* directories

    /etc/crontab is system crontabs file. Usually only used by root user or daemons to configure system wide jobs. All individual user must must use crontab command to install and edit their jobs as described above. /var/spool/cron/ or /var/cron/tabs/ is directory for personal user crontab files. It must be backup with users home directory.

    Understanding Default /etc/crontab

    Typical /etc/crontab file entries:

    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root
    HOME=/
    # run-parts
    01 * * * * root run-parts /etc/cron.hourly
    02 4 * * * root run-parts /etc/cron.daily
    22 4 * * 0 root run-parts /etc/cron.weekly
    42 4 1 * * root run-parts /etc/cron.monthly

    First, the environment must be defined. If the shell line is omitted, cron will use the default, which is sh. If the PATH variable is omitted, no default will be used and file locations will need to be absolute. If HOME is omitted, cron will use the invoking users home directory.

    Additionally, cron reads the files in /etc/cron.d/ directory. Usually system daemon such as sa-update or sysstat places their cronjob here. As a root user or superuser you can use following directories to configure cron jobs. You can directly drop your scripts here. The run-parts command run scripts or programs in a directory via /etc/crontab file:

    DirectoryDescription
    /etc/cron.d/ Put all scripts here and call them from /etc/crontab file.
    /etc/cron.daily/ Run all scripts once a day
    /etc/cron.hourly/ Run all scripts once an hour
    /etc/cron.monthly/Run all scripts once a month
    /etc/cron.weekly/Run all scripts once a week

    How do I use above directories to put my own scripts or jobs?

    Here is a sample shell script called clean.cache. This script is created to clean up cached files every 10 days. This script is directly created at /etc/cron.daliy/ directory. In other words create a text file called /etc/cron.daily/clean.cache as follows.

     #!/bin/bash
    # A sample shell script to clean cached file from lighttpd web server
    CROOT="/tmp/cachelighttpd/"
     
    # Clean files every $DAYS
    DAYS=10
     
    # Web server username and group name
    LUSER="lighttpd"
    LGROUP="lighttpd"
     
    # Okay, let us start cleaning as per $DAYS
    /usr/bin/find ${CROOT} -type f -mtime +${DAYS} | xargs -r /bin/rm
     
    # Failsafe 
    # if directory deleted by some other script just get it back 
    if [ ! -d $CROOT ]
    then
            /bin/mkdir -p $CROOT
            /bin/chown ${LUSER}:${LGROUP} ${CROOT}
    fi

    Save and close the file. Set the permissions:
    # chmod +x /etc/cron.daily/clean.cache

    How do I backup installed cron jobs entries?

    Simply type the following command to backup your cronjobs to a nas server mounted at /nas01/backup/cron/users.root.bakup directory:
    # crontab -l > /nas01/backup/cron/users.root.bakup
    # crontab -u userName -l > /nas01/backup/cron/users.userName.bakup

    See also


    UnrealIRCd Crontab


    #!/bin/sh
    #
    # UnrealIRCD Crontab v2.1
    # $Id$
    #
    # This is a script suitable for use in a crontab. It checks to make sure
    # your ircd is running. YOU NEED A SEPARATE CRON JOB FOR EACH IRCD. If your
    # ircd isn't found, it'll try to start it back up.
    #
    # You'll need to edit this script for your ircd.
    #
    # To check for your ircd every 10 minutes, put the following line in your
    # crontab:
    # 0,10,20,30,40,50 * * * * /home/mydir/ircdchk
    # And if you don't want to get email from crontab when it checks you ircd,
    # put the following in your crontab:
    # 0,10,20,30,40,50 * * * * /home/mydir/ircdchk >/dev/null 2>&1
    #
    # change this to the mail address to mail output to:
    # MAIL=me

    # These values shouldn't need to be changed

    # The path to the ircd binary
    ircdexe="@[email protected]"

    # The path to the ircd pid file
    ircdname="@[email protected]/ircd.pid"

    ########## you probably don't need to change anything below here ##########
    if test -r $ircdname; then
    # there is a pid file -- is it current?
    ircdpid=`cat $ircdname`
    if `kill -CHLD $ircdpid >/dev/null 2>&1`; then
    # it's still going
    # back out quietly
    exit 0
    fi
    echo "UnrealIRCd Crontab notice:"
    echo ""
    echo "Stale $ircdname file (erasing it)"
    rm -f $ircdname
    fi
    echo ""
    echo "Couldn't find the ircd running. Reloading it..."
    echo ""
    $ircdexe

    Cron job

    From UnrealIRCd documentation wiki
    Jump to: navigation, search

    Usually you want to make sure UnrealIRCd starts on system startup or restarts after a crash. See below.


    UnrealIRCd 4.0.1 and higher

    • Run crontab -e to edit your crontab. It will fire up an editor.
    • Add the following two lines to the file (make sure there is an enter at the end):

    */5 * * * * /home/yourusername/unrealircd/unrealircd croncheck
    @reboot /home/yourusername/unrealircd/unrealircd croncheck
    

    In UnrealIRCd-4.0.12.1 croncheck is renamed to unrealircd, so you will have to edit that like:

    */5 * * * * /home/yourusername/unrealircd/unrealircd unrealircd
    @reboot /home/yourusername/unrealircd/unrealircd unrealircd
    
    • Save it. Done!

    The first line makes the unrealircd script check if UnrealIRcd is running every five minutes (hence the */5). The second line will ensure that UnrealIRCd is started as soon as possible after the system has rebooted (hence the @reboot). It's possible to do without the 2nd line but then it may take up to 5 minutes after a reboot for the IRCd to be started.

    You can also add the following line at the top of your crontab to make sure you are e-mailed whenever UnrealIRCd is (re)started through cron:

    [email protected]

    Previous versions (4.0.0)

    NOTE: If you are on 4.0.1 or higher then don't follow below. Read the text above instead.

    In UnrealIRCd's 4.0.0 release the cron script was broken. As a workaround you can put the following in crontab -e. This will only ensure that UnrealIRCd is started after a reboot (not after a crash):

    @reboot /home/yourusername/unrealircd/unrealircd start
    

    CentOS Crontab

    when editing it after finishing press esc, then type

    :wq
    and press enter, that will save the crontab and exit

    or just
    :w
    will save it

    Or if you are using WinScp as edit, edit thew file here: /var/spool/cron/Username

    Anope Services Crontab


    Setting up a crontab

    A crontab entry will allow you to check periodically whether Anope is still running, and restart it if not.

    First rename the example.chk script that is in Anope path (by default, this is ~/services/data) to services.chk and edit it. You'll need to. modify the CONFIGURATION part of the file. Then ensure that the file is. marked as executable by typing chmod +x services.chk, and try to launch the script to see if it works (Anope must not be running when you do this ;))

    When this is done, you'll have to add the crontab entry. Type crontab -e. This will open the default text editor with the crontab file. Enter the following (with correct path):


    */5 * * * * /home/ircd/services/data/services.chk >/dev/null 2>&1
    

    The */5 at the beginning means "check every 5 minutes". You may replace the 5 with other another number if you want (but less than 60). Consult your system's manual pages for more details on the syntax of the crontab file. Interesting manpages are crontab(5), crontab(1) and cron(8).

    Save and exit, and it's installed.



    EggDrop Crontab


    Editing the botchk file

    The botchk script and crontab are used to automatically restart the bot if the shell it's on reboots or if the bot process is killed for some other reason. You can find the botchk file in the scripts directory (in the directory you installed the bot to). Newer versions of Eggdrop (from 1.3.24i) have a script included that automatically configures botchk and crontab for you. In telnet, simply switch to the scripts directory and type chmod 700 autobotchk then ./autobotchk <config> -dir /home/botdir -noemail, where /home/botdir is the directory you installed the bot to and <config> is the name you chose for your config file.

    Otherwise, you can edit the botchk file and insert the required crontab entry manually. There are only four things you need to set in the botchk file, all of which are pretty self explanatory. Once you've edited the botchk file, you need to add an entry to your crontab. Here's the best method:

    1) Your crontab line should look like:


    0,10,20,30,40,50 * * * * /home/ircd/scripts/botchk >/dev/null 2>&1


    This will run the botchk script every 10 minutes, which checks that the bot is running and restarts it if it isn't. You just need to change the /home/botdir part to the correct path to the bot on your shell (type pwd to show this). Type the line in Notepad or some place where you can highlight and copy it from.

    2) Type crontab -e. This should bring up the vi editor (it will appear as a bunch of lines starting with the ~ character), but may open up the pico editor instead.

    3) For vi, do the following - hit ctrl-L, hit i, paste the crontab line you created earlier, hit Esc, type :wq! then hit Enter (if you make a mistake doing this, just hit Esc and start over). For pico - paste the crontab line you created earlier, hit ctrl-X, hit Y when prompted to save, hit Enter when prompted for a filename.

    You can view your current crontab entries by typing crontab -l. To clear your crontab, use crontab -r (may be crontab -d on some shells).



    psyBNC Crontab


    When your server stops working and you must reboot it, the psyBNC canít start automatically. But with this simple trick, youíll be able to start you psyBNC automatically when the server starts from a reboot.

    First thing to do is you must edit psbncchk file in your psyBNC folder.

    $ nano psybncchk

    Search for this line:

    PSYBNCPATH=/home/foobar/psybnc

    Change the path into your psyBNC path, look at this example:

    PSYBNCPATH=/home/daniel/psybnc

    Save the file and exit the editor.

    Change the default editor by using this command:

    $ export EDITOR=nano

    Ok, next step is editing the crontab file:

    $ crontab -e

    Insert this line to the file:

    0,10,20,30,40,50 * * * * /home/ircd/psybnc/psybncchk >/dev/null 2>&1

    Remember, change the path to your psybnc path.

    This crontab command means it will run the psybncchk file every 10 minutes.


    BOPM Crontab


    #!/bin/sh
    #
    # Blitzed Open Proxy Monitor Crontab Script - Restart BOPM if needed.
    #
    # From [email protected]
    #
    # To install this script:
    #
    # Edit the first line of the script below to reflect the directory
    # you installed BOPM in.
    #
    # Copy this script to the directory you want it to stay in. This
    # could be the directory you installed BOPM in, or something neutral,
    # such as ~/crontabs or something.
    #
    # Run crontab -e
    #
    # To have the script run every minute, type this:
    #
    # * * * * * ~/crontabs/bopmchk
    #
    # Save, and exit. The script will now run every minute.

    cd /home/zemra/bopm/

    # Leave the rest alone.

    if [ -f var/bopm.pid ] ; then
    PID=`cat var/bopm.pid`
    if `kill -CHLD $PID >/dev/null 2>&1`; then
    exit 0;
    fi
    rm -f var/bopm.pid
    fi

    bin/bopm
    exit 0;