Cron Job using Crontab

Posted on 2009-02-12
Last Modified: 2013-12-26
I need to create a cron job that executes a file every morning at 4am. I have need used cron jobs before so pretty much lost right. I've read alot of things but still not sure. Can someone give me a step by step help from the very begining.

I am not sure how to access the crontab or write to it.

I need to run this file every morning at 4am.

I use 'crontab -e' then try to add '00 04 * * * /crons/mysql/' to the line it says 'Crontab: no crontab - using an empty one'

I then tried adding '00 04 * * * /crons/mysql/' then says 'isn't a vi command'

What do I do?


This is what I Have so far
Question by:SaltyDawg
    LVL 33

    Expert Comment

    by:Todd Gerbert
    Check out "man vi" for some info on using the VI editor.  If you have X windows installed there might be a GUI application...never used one though, so I'm not sure.

    enter "crontab -e" on a command line, which will either 1) Open you current cron file in vi, or 2) open an empty new file in vi

    Use the arrow keys to go to the last line in the file, if there are any, then press "A" (that's a capital A, no quotes) - this takes VI out of command mode and into edit mode (specifically the A tells VI to append to the end of the current line)

    Press enter to go to the next line down, and enter your cron job schedule "00 04 * * * /crons/mysql/"

    Then press the Esc key to go back into command mode, then type :wq! (the colon goes to VI's command prompt, the w means write the file, the q means quit and the exclamation point means don't ask for confirmation).
    LVL 6

    Expert Comment

    Or, you can change your default editor like :

    Let say your prefered editor is nano (pico), or whatever...

    Do this:

    export EDITOR

    then crontab -e will open the crontab in nano.
    LVL 23

    Expert Comment

    by:Maciej S
    crontab -l > crontab.txt
    edit crontab.txt file
    add line to this file:
    0 4 * * * /crons/mysql/
    save the file
    crontab crontab.txt

    I think your problem is vi related. It looks, that your are not saving this file (are you quitting with :q! command from vi editor? If so, try :wq or :wq! command). However above method (crontab -l > ...; editing file; crontab this.file should work).
    LVL 40

    Accepted Solution

    if you want to avoid using editors, you can do

    crontab -l > mycron
    echo '00 04 * * * /crons/mysql/' >> mycron
    crontab mycron
    LVL 1

    Author Comment


    When I use your code it says:

    'crontab: non crontab for user
    LVL 1

    Author Comment

    I think I have it now
    LVL 1

    Author Closing Comment

    LVL 40

    Expert Comment

    welcome :)

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
    Recently, an awarded photographer, Selina De Maeyer (, completed a photo shoot of a beautiful event ( in An…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now