Cron Job using Crontab

Posted on 2009-02-12
Medium Priority
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/update.sh' to the line it says 'Crontab: no crontab - using an empty one'

I then tried adding '00 04 * * * /crons/mysql/update.sh' 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
ID: 23625408
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/update.sh"

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).

Expert Comment

ID: 23626606
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
ID: 23628094
crontab -l > crontab.txt
edit crontab.txt file
add line to this file:
0 4 * * * /crons/mysql/update.sh
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).
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

LVL 40

Accepted Solution

omarfarid earned 2000 total points
ID: 23630633
if you want to avoid using editors, you can do

crontab -l > mycron
echo '00 04 * * * /crons/mysql/update.sh' >> mycron
crontab mycron

Author Comment

ID: 23713925

When I use your code it says:

'crontab: non crontab for user

Author Comment

ID: 23714062
I think I have it now

Author Closing Comment

ID: 31546239
LVL 40

Expert Comment

ID: 23725003
welcome :)

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can use conditional statements using Python.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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 find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

839 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