ALL ABOUT CRON JOBS FOR A ORACLE DBA/DEVELOPER, A MYSTERY SOLVED - Logan Ramasamy

© 2000 RevealNet, Inc.

When I started as a DBA and tried to run batch files as cron jobs it was unclear to me how to run cron jobs. I tried to learn from Unix Administrators. They didn't have time or they did not want to spare their knowledge. So I learned all about cron jobs and the following is the outcome:

  1. Cron program is used to execute jobs at specified times; used to run programs unattended during the wee hours; to perform periodic operations; to run commands on a regularly scheduled basis; to run programs at night for fear of slowing down other users if it is run during the day, etc.
  2. The very first step in running a cron job is to request Unix administrator to add the Oracle application owner username or the username under whom the cron jobs need to be run, to cron.allow file in /var/adm/cron directory. Once this is done, you are all set to run a cron job.
  3. In order to create crontab entries and run, issue the following command in any directory of a particular user.

    $crontab -e

    In fact, "crontab -e" command opens up the file in vi editor to make new crontab entries or edit the existing entries. The above command automatically runs those entries after creating or editing them.

    Entries are made one per line that will execute automatically at a given time.

    #everything on a line is separated by blanks or tabs

    # minute hour day month day of the week  script to execute
    # (0-59) (0-23) (1-31) (1-12) (0-6 with 0=Sunday) (executable)
    #
    0 8 * * 1-5 /forenoon/meeting
    45 8 * * 1-5 /forenoon/coffee
    0 12 * 2,4 /lunch/applebees
    0 12 * * 1,3,5 /lunch/burger_king
    0 13 * 5-9 6 /lawn/sprinkler_on
    0 20 14 4 *  /taxes/1040

    Numbers are supplied before each command to specify the execution time. The numbers appear in five fields as above. Use hash at the first field for comment, comma between multiple values, a hyphen to indicate a range, and an asterisk to indicate all possible values.

    The first entry in the cronfile is

    8  *  1-5  /forenoon/meeting

    This says that the program /forenoon/meeting is to be run at 8:00 A.M. every Monday through Friday (1-5). The next entry starts the coffee at 8:45, so, as soon as the meeting is over, coffee is ready. It is lunch time at 12:00 PM. Go to Applebees on Tuesdays and Thursdays and to Burger King on Mondays, Wednesdays and Fridays. On Saturday, at one O'clock in the afternoon, during the month from May to September, lawn sprinkler is on. On April 14, at night, one day before the tax due date, 1040 form needs to be filled up.

  4. In order to list and view the valid crontab entries, issue the following command in any directory of the user.

    $crontab -l

    In order to remove all the valid crontab entries from running, issue the following command in any directory of the user,

    $crontab -r

  5. However, the above method has the following lacuna.

    1. After removing all the crontab entries, the same entries can not be run once again.
    2. There is no way in the above method to have a backup copy of the crontab entries by a typical user in one of his directories.
    3. For some reasons, if multiple versions of cronfile need to be saved in user's directory, there is no way in the above method.
     

    So we need to follow the steps given below to overcome those handicaps.

    The cronfile is normally created in the home directory of a particular user. So at Unix prompt, type cd and press RETURN to go to the home directory. Once again type pwd and press RETURN to see the complete path of the home directory.

    $ cd
    $ pwd
    /u06/avalon
    $

    Use vi editor, put all the entries and save it with a meaningful name to identify it as a cronfile e.g., cron_file. Now this file is the master copy of the crontab entries. Issue the following command to run the cronfile.

    $crontab filename

    You can have any number of files with crontab entries but you can run only one file as a cronfile for a particular user at one point of time. The recent file, which was issued, "crontab filename" command is the valid one. Now we can edit and run this file using "crontab -e" command; list the entries by "crontab -l" and remove the entries by "crontab -r" (of course, from any directory).

    However after removing the entries of a cronfile from running, the same file or another file with crontab entries can be run, by issuing "crontab filename"

  6. The following is the summary of crontab commands.

    Crontab filename

     --- to run a new cronfile after creating
                                or
    the existing cronfile after modification

    Crontab -e

     --- to create and run a new cronfile (if there are no valid crontab entries,currently)
                                or
    edit and run the user's current cronfile

    Crontab -l  --- to list and view the content of existing cronfile
    Crontab -r  --- to remove all the entries of a cronfile from running.
    Crontab flags [user] --- A privileged user can run crontab for another user by supplying a user after any of the above flags ( e/ l/ r ).

There is another Unix utility, called "at" to run any one-time job at specified time and date. For you to run "at" jobs, Unix administrator has to make an entry of your username in at.allow file in /var/adm/cron directory.

Note: This article is specific to HP-UX. However, the logic is same for all Unix platforms.