Main Menu
Articles Home
Most Popular Articles
Top Authors
Submit Articles
Submission Guidelines
Link to Us
Bookmark
Contact Us



Partners
 
Home / Computers

Automating Tasks in Linux using Cron

By:Vinu Thomas


Linux has a powerful task scheduler called Cron. Cron will
allow you to run commands automatically at times specified
by you. Cron is similar to the task scheduler you find in
Windows. To keep track of the schedules and tasks it has
to run, Cron requires a file called Crontab (CRON TABle).
All the Cron schedules and tasks should be stored in this
table. The Crontab files cannot be directly edited. You can
add or delete entries in the crontab file using the crontab
command.
What's Cron and Crontab ?
You must be wondering what the difference between cron and
crontab or wether they are the same. Cron is a process or
program which wakes up every minute and looks for jobs it
has to execute at that moment. Crontab is the list of jobs
and times at which they have to execute.
Crontab Format:
Each entry in Crontab has at least 6 fields separated by a
single space.

Field 1 Minute Range of Values : 0-59
Field 2 Hour Range of Values : 0-23
Field 3 Day Range of Values : 1-31
Field 4 Month Range of Values : 1-12
Field 5 Day of week Range of Values : 0-6 (Sunday being 0)
Field 6 Command to Execute
Now let's see how to make a crontab entry. Let's say you
want to run a script backup.sh every day at 6:00pm.The
entry would look like this:
0 18 * * * /home/user/backup.sh
The asterisk (*) is used to indicate that every instance of
the particular time period will be used (i.e. every hour,
every weekday, etc.). I've used to full path to the script
/home/user/backup.sh instead of just using backup.sh. This
is because cron runs as root, you should fully qualify your
path names to any scripts that will be run. Let's see some
more examples :
* Let's run the script printinvoices.sh every sunday
at 12:45pm.
45 12 * * 0 /home/account/printinvoices.sh
* How about clearaccount.sh every month beginning
at 1:32am ?
21 1 1 * * /home/accont/clearaccount.sh
* Let's see how to schedule a task to run only on
weekdays(monday to friday)
0 10 * * 1-5 /home/account/cleartemp.sh
Adding and Editing Entries in Crontab
and
Now that you know how crontab entries are formated, it's time
to put some of your entries into the crontab list. To do this,
you can use the crontab command. By specifying the -e option,
you'll be taken to the default text editor to add and edit
your crontab list. [crontab -e]
Another method of manipulating your crontab entries is to create
and save a text file with your crontab entries. You can load
your list into crontab by using the following command:
[crontab mycrontablist]
where mycrontablist is the file containing your entries.
Viewing Crontab: [crontab -l]
You can view your current crontab list by specifying the -l
option. Issuing this command will print out a list of all your
current jobs in the crontab list
Removing Crontab: [crontab -r]
The -r option removes your current crontab file. Issuing this
command will empty the contents of the current user's crontab
file
Output from cron
Usually the output of cron gets mailed to the owner of the
process or the person or email id specified in the MAILTO
variable. To set the MAILTO variable, you'll have to add the
following command to the top of your crontab :
MAILTO="your_email_address@domain.com"
If you have a command that is run frequently, and you don't
want the output to be emailed each time, you can redirect the
output to a log file cmd >> log.file, so your job would be
something like this.
0 18 * * * /home/user/backup.sh>>log.file
If you don't want any output at all, you can redirect the
output to a null file : cmd>>/dev/null
0 18 * * * /home/user/backup.sh>>/dev/null



Digg del.icio.us Blink Stumble Spurl Reddit Netscape Furl

Article Source: http://www.articles2k.com


Vinu Thomas is a consultant on Web design and Internet Technologies.
His website is http://www.vinuthomas.com. You can read more articles
on Linux @ http://www.vinuthomas.com/sections-listarticles-6.html



Top Computers Articles
  • 1). The danger behind peer-to-peer file sharing  By : Migz Mon
    File sharing is very common nowadays. You can get a P2P (peer-to-peer) program for free, and soon start sharing stuff like music, videos and programs. We use these applications freely and most of us don’t know the risks involved when using them. Questionable reliability of files – Once we get our hands on a P2P program, we download everything we want like crazy.

  • 4). Is The Internet Censored?  By : J Schipper
    When most people think about Internet censorship, the usual suspects immediately come to mind : Communist China and repressive Islamic countries. Unfortunately, Westerners have an embarrassment all their own : Australia's net censorship law, established in 2000. Freedom House, a non-profit U.S. organization dedicated to spreading human rights and freedom, undertakes regular surveys on press freedom and censorship.

  • 5). Toshiba Libertto U100, smaller yet better than you might imagine!  By : john
    Have you ever wondered how small a laptop can be? Well, utilizing today’s technology Toshiba does a perfect job at demonstrating it; the Libertto U100 is one of the very few laptops that have such a rich feature set fitted altogether within such a small space. Of course, if you believe that a fingerprint authentication system is too much for such a.

  • 6). Does CCTV Really Reduce Crime?  By : Robert Michael
    In the UK, home of twenty percent of the world's CCTV cameras, there is mounting concern about its invasion and lack of regulation. "UK Public CCTV Surveillance Regulation Campaign." The thought-provoking questions they ask are good ones. They want to know, for instance, why, if television programming and licensing is so heavily regulated in the UK.

  • 8). Preparation Tips For Comptia A+ Certification  By :
    The Industry Standard CompTIA A Plus Certification exam has two parts; approximately 80 questions for each part. * Core Hardware: $153 - min. passing score 515 * Operating Systems Technology: $153 - min. passing score 505 (CompTIA corporate members can take the exams for $98, employers that require this test are usually corporate members and will inform you of this.

  • 9). Can Cell Phones Harm Your Health?  By : D Ruplinger
    It seems like everywhere a person goes there is at least one person in view with a cell phone to their ear whether it is on the road, in a store, in a parking lot, walking down the street, etc. Even in places where cell phone usage is banned such as concert halls or movie theaters there is the occasional offender, or more likely, at least a few people using the text messaging feature on their phone.


New Computers Articles
  • 5). If You Want Flexibility And Portability Consider A Laptop  By : Gregg Hall
    If you are one of those who works with a computer on a daily basis like me you should consider a laptop. I have actually owned three now and I think I would be lost without mine. I still use my desktop PC quite a bit at home but the laptop comes in extremely handy when I am away.

  • 7). Becoming A DVD Repair Expert Online  By : Maxine Schel
    Many conventional colleges and universities are now offering online DVD repair courses, which are the exact same as those taught on a traditional college campus, to distance learners. Online education is generally taught through the use of several different methods, including online satellite feeds, correspondence or online lessons. This technique permits the student to obtain a DVD repair degree without ever having to step inside of a classroom.

  • 8). Compatible Inkjet Cartridge  By : Isabel Rodrigues
    Once the printer ink runs dry it has to be replaced with another inkjet cartridge. There are many reputed companies like Canon, Epson, Dell, and Lexmark that provide the necessary cartridges to replace the empty cartridges. Replacing inkjet cartridge can add to a very big cost. It could be worse if you have to replace the empty cartridges frequently every month.

  • 9). Best Passwords  By : Richard Romando
    No sane person would ever like someone else reading her email. Or for that matter some other person using her password and breaking into a financial institution. You should, therefore, choose a strong, secure password in such a manner that would be a hard nut to crack for others and easy for you to remember. The more random and mixed-up you make it, the harder it is for others to crack.

  • 10). Home Printing Machines  By : Peter Vermeeren
    Home printing machines are available on the market today in many sizes, prices and capabilities. Many different forms of printing can be accomplished in the home. Whether you want to create unique tee-shirts using a home screen printing machine or you want to produce elegant laser printed letters, the equipment is readily available to you for home use.



 


© 2006 articles2k.com - Privacy Policy