Page 1 of 4 123 ... LastLast
Results 1 to 10 of 191

Thread: How-to for crontab

Hybrid View

  1. #1
    Join Date
    Nov 2005
    Beans
    729
    Distro
    Ubuntu 6.06
    What Is Crontab?

    A crontab is a simple text file that holds a list of commands that are to be run at specified times. These commands, and their related run times, are controlled by the cron daemon and are executed in the system's background. More information can be found by viewing the crontab's man page. We will run through a simple crontab example later.


    How Does It Work?

    The system maintains a crontab for each user on the system. In order to edit or create a crontab, you must use the text editor that is specified by your system. The nano text editor is the default text editor on my system. This text editor must be opened with the command crontab using the -e option. To create a crontab open a term and type:

    Code:
    crontab -e
    The nano text editor will open with a blank window for the desired times and commands to be entered. Each line represents a seperate crontab entry - also known as a "cron job". If you are not familiar with the nano text editor you should obtain a tutorial for it as that information is beyond the scope of this post.


    Crontab Sections

    Each of the sections is separated by a space, with the final section having one or more spaces in it. No spaces are allowed within Sections 1-5, only between them. Sections 1-5 are used to indicate when and how often you want the task to be executed. This is how a cron job is layed out:

    minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday), command

    Code:
    01 04 1 1 1 /usr/bin/somedirectory/somecommand
    The above example will run /usr/bin/somedirectory/somecommand at 4:01am on any Monday which falls on January 1st. An asterisk (*) can be used so that every instance (every hour, every weekday, every month, etc.) of a time period is used. Code:

    Code:
    01 04 * * * /usr/bin/somedirectory/somecommand
    The above example will run /usr/bin/somedirectory/somecommand at 4:01am on every day of every month.

    Comma-seperated values can be used to run more than one instance of a particular command within a time period. Dash-seperated values can be used to run a command continuously. Code:

    Code:
    01,31 04,05 1-15 1,6 * /usr/bin/somedirectory/somecommand
    The above example will run /usr/bin/somedirectory/somecommand at 01 and 31 past the hours of 4:00am and 5:00am on the 1st through the 15th of every January and June.

    The "/usr/bin/somedirectory/somecommand" text in the above examples indicates the task which will be run at the specified times. It is recommended that you use the full path to the desired commands as shown in the above examples. The crontab will begin running as soon as it is properly edited and saved.


    Crontab Options

    The -l option causes the current crontab to be displayed on standard output.
    The -r option causes the current crontab to be removed.
    The -e option is used to edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables.

    After you exit from the editor, the modified crontab will be checked for accuracy and, if there are no errors, installed automatically.


    Crontab Example

    Below is an example of how to setup a crontab to run updatedb, which updates the slocate database: Open a term and type "crontab -e" (without the double quotes) and press enter, type the following line, substituting the full path for the one shown below, into the editor:

    Code:
    45 04 * * * /usr/bin/updatedb
    Save your changes and exit the editor.

    Crontab will let you know if you made any mistakes. The crontab will be installed and begin running if there are no errors. That's it. You now have a cron job setup to run updatedb, which updates the slocate database, every morning at 4:45.

    Note: The double-ampersand (&&) can also be used in the "command" section to run multiple commands consecutively.

    Code:
    45 04 * * * /usr/sbin/chkrootkit && /usr/bin/updatedb
    The above example will run chkrootkit and updatedb at 4:45am daily - providing you have all listed apps installed.

    I hope this little tutorial is of help to the community.

  2. #2
    Join Date
    Apr 2005
    Beans
    645
    Distro
    Dapper Drake Testing/

    Re: How-to for crontab

    Nice HOWTO very usefull for crontab newbies, just want to add a simple thing but usefull, you can overwrite system editor by running
    Code:
    EDITOR=vi && crontab -e
    replace vi with your favorite one
    [My Blog] | [My Sites] | [My Ubuntu Guides]

    doc.gwos.org, the real successor of Ubuntu Guide

  3. #3
    Join Date
    Jun 2005
    Beans
    33
    Distro
    Kubuntu 6.10 Edgy

    Re: How-to for crontab

    Excellent howto!! This is very helpful stuff. Thanks a lot.

  4. #4
    Join Date
    Nov 2005
    Beans
    729
    Distro
    Ubuntu 6.06

    Re: How-to for crontab

    Quote Originally Posted by evs
    Excellent howto!! This is very helpful stuff. Thanks a lot.
    You're welcome, glad it is helpful.

  5. #5
    Join Date
    Nov 2005
    Beans
    729
    Distro
    Ubuntu 6.06

    Re: How-to for crontab

    Quote Originally Posted by Gandalf
    Nice HOWTO very usefull for crontab newbies, just want to add a simple thing but usefull, you can overwrite system editor by running
    Code:
    EDITOR=vi && crontab -e
    replace vi with your favorite one
    Thanks, that solves part of it. Any way to get that to be permanent?

  6. #6
    Join Date
    Jun 2005
    Location
    France
    Beans
    7,100
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How-to for crontab

    Quote Originally Posted by ardchoille
    Thanks, that solves part of it. Any way to get that to be permanent?
    Just put this line in your .bashrc, if you want to use gedit (for exemple) when you type crontab -e :
    Code:
    export EDITOR=gedit

  7. #7
    Join Date
    Nov 2005
    Beans
    729
    Distro
    Ubuntu 6.06

    Re: How-to for crontab

    Quote Originally Posted by frodon
    Just put this line in your .bashrc, if you want to use gedit (for exemple) when you type crontab -e :
    Code:
    export EDITOR=gedit
    Thank you very much

  8. #8
    Join Date
    Apr 2005
    Beans
    27

    Re: How-to for crontab

    Thank you.

  9. #9
    Join Date
    Jan 2008
    Beans
    184
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: How-to for crontab

    Quote Originally Posted by ardchoille View Post
    Thanks, that solves part of it. Any way to get that to be permanent?
    (He was talking about selecting the editor from which the cron file is edited)

    I don't know if this was answered to you in the way I did below, as I did not read every page in this thread, but check it out:

    Code:
    sudo select-editor
    **America != USA | American != Usanian**
    Own a Dell XPS M1530? Join the group!

  10. #10
    Join Date
    Dec 2008
    Beans
    1

    Re: How-to for crontab

    Quote Originally Posted by Gandalf View Post
    Code:
    EDITOR=vi && crontab -e
    I think this should be...

    Code:
    EDITOR=vi crontab -e

Page 1 of 4 123 ... LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •