*MAFIA* Forums

*MAFIA* Forums

  • May 05, 2024, 05:25:46 AM
  • Welcome, Guest
Please login or register.

Login with username, password and session length
Advanced search  

News:

Welcome back the Arcade! Play over 100+ games to get the high score and compete against other forum members.

http://www.mafiaowns.com/index.php?action=arcade;sa=list;sortby=a2z;

Poll

Who amongst us would care to learn a little bash and perl scripting?

ZOMG!  Skriptin' is kool
- 3 (33.3%)
I ain't no script kiddie, damn it.
- 0 (0%)
What the fuck are you talking about?
- 6 (66.7%)

Total Members Voted: 8


Author Topic: A Delve Into Crontab  (Read 1230 times)

Rahl

  • Forum Member
  • Reputation: 279
  • Offline Offline
  • Posts: 379
    • View Profile
A Delve Into Crontab
« on: September 07, 2005, 08:41:43 PM »

First things first -- what the hell is a crontab?  A crontab (CRON TABle) is just a file that contains a system schedule for when certain processes should be run.  In the Windows world, you're probably already familiar with this.  You've got Disk Defrag running every night, Spyware and Adware checkers going hourly, and you're refreshing your backups to a flash drive every 10 minutes.  We can set up a similar thing in Linux.

Let's begin with a look at the setup of a cron file.

Generally at the top of the default crontab, you'll see something like this
HOME=/home/rahl
PATH=/usr/bin:/usr/sbin:.
SHELL=/bin/bash
MAILTO=rahl

This sets up the environment that the jobs will be run in.  HOME is the variable of your home directory, with PATH being those directories you want directly accessible without having to use the fully qualified pathname.  And SHELL speaks to which sh you're using, be it bash, csh, what have you.  MAILTO is where to send any notifications such as errors to.  This is just another username.

After this, we begin to see the actual commands that are to be run.  A general statement looks like this.

30     18     *     *     *         rm /home/rahl/tmp/*

The first five columns set the time that the process is to run.  These columns are:
minutes (0-59)     hour (0-23)     day of month (1-31)     month (1-12)     day of week(1-7, 1=Monday)
So, for example, if you wanted something to run January 10, 4:20 a.m., it would be 20 4 10 1 *
The asterik ( * ) character is a metacharacter which matches every possible input.  In the previous example, it would execute for any day of the week that met the other criteria.  You can also specify ranges like 2-15, or discreet times, like 0,15,30,45.

And the statement rm /home/rahl/tmp/* is the statement to be executed by the bash shell.  In this case, it removes all the files located in the tmp directory under /home/rahl/.  Note that it only deletes the files (no subdirectories) at the first level because we didn't specify the -dR flags which were mentioned in a previous tutorial.

One of the handy uses that every crontab should include is for running the updatedb command.  updatedb creates a database of all the files on your computer with their fully qualified paths.  using the command 'locate', the updatedb database can be quickly queried for all matching files and know exactly where each one is at.  Improvements over Window's Find feature?  Quicker searching.  Drawback?  What if the database isn't up-to-date?

The remedy to this is -- you guessed it -- cronjobs!  What would the crontab look like to run updatedb every hour on the hour?
HOME=/root
PATH=/usr/sbin:/bin:/usr/bin:/sbin:/usr/X11R6/bin
SHELL=/bin/bash

0 * * * *   updatedb

Now that we know how to write a crontab, we might want to know how to get the system to read from it.  There are actually a couple of ways this can be done.  The way I prefer is to create a file, call it something like myCron, and edit that how you want it.  Then, with root permission, cd /etc/, and type crontab /path/to/myCron.  This will set the crontab to read from your file, and considering everything is in the proper working order, you're all set.  

The other way is to edit the file directory.  By cd /etc (again with root permission), you can type in crontab -e which will bring up the file in an editor of your choice.  You can alter which editor comes up by issuing the command export EDITOR=ed.

If you've read the other post about some useful commands, you already have a feel what flags for a command are.  I'm not going to repeat the explanation here, but rather just provide a brief list of the flags for crontab.

crontab -e     Edit your crontab file, or create one if it doesn't already exist.
crontab -l      Display your crontab file.  (Useful to ensure the changes took)
crontab -r      Remove your crontab file.
crontab -v      Display the last time you edited your crontab file. (This option is only available on a few systems.)

That should about wrap things up.  If I left anything out, Toby, let me know.  And of course, if anyone has questions, we're only a PM away.

EDIT -- I added the poll just to see what sort of interest there would be in the subject.  If there proves to be a sufficient amount, I might consider doing a few weekly installments of scripting tutorials (a few bash and a few perl).
« Last Edit: September 07, 2005, 09:31:12 PM by *MAFIA* Rahl »
Logged
 

Page created in 0.039 seconds with 30 queries.