SLAC ESD Software Engineering Group

 

UNIX DEVELOPMENT ENVIRONMENT

 

 

How to Create a UNIX Cron Job

SLAC Detailed

SLAC Computing

Software Home

Software Detailed

Documentation and Web Suport


 

Introduction

Cron is a UNIX facility that allows you to schedule programs for execution at regular intervals. Regularly scheduled commands (i.e., what to run and when to run) are specified by crontab entries in a crontab file. Users manage their cron jobs for submitting, listing, and removing with the crontab command. There are two kinds of crontab in SLAC: trscrontab and crontab. We use both in production. The major differences between trscrontab and crontab are:

      trscrontab entries execute with an AFS token,

      trscrontab file is maintained by a central server while crontab file is maintained locally,

      trscrontab execution runs on one or more specified hosts while crontab execution occurs locally.

Because time driven events are scheduled centrally and then executed remotely using the ssh command, trscrontab is meant to be used for reasonably long running (i.e., 1 or more minutes) infrequently scheduled (i.e., no more than about once an hour) processes that need an AFS token to properly execute. One should use crontab to schedule frequent or short running tasks that don't require an AFS token. Using trscrontab for such tasks will add an unacceptable amount of system overhead.

This document describes how to create a cron job using both crontab and trscrontab. Examples are given using cddev account.

Crontab

On standalone UNIX machines such as PEPII gateways on which there is no access to AFS, one has to use crontab. One also should use crontab to schedule frequent or short running tasks that don't require an AFS token on a SCS tailored machine.

A user's crontab file lists commands that are to be executed on behalf of that user at specified times on specified dates. Each entry in the crontab file has six fields with the following format:

minute hour day-of-month month weekday command

The fields are separated by spaces. Each field can be a single number, a pair of numbers separated by a dash (indicating a range of numbers), a comma-separated list of numbers and ranges, or an asterisk (representing all valid values for that filed).

Below is an example crontab entry that is defined under cddev account on opi00gtw04:

0,5,10,15,20,25,30,35,40,45,50,55 * * * * (/afs/slac/g/cd/soft/ref/app/channelArchiver/script/log_scanner_nlcta.csh) > /tmp/nlcta_monitor.log 2>&1

It runs log_scanner_nlcta.csh, a Channel Archive Monitor process for NLCTA, on opi00gtw04 every 5 minutes, and redirects the standard output and error to /tmp/nlcta_monitor.log. Please note that the entry must be in one line. It is a good practice to create a log file which is useful for debug when needed.

To add crontab entries, use the following steps:

  create a backup crontab file

$ crontab -l >! crontab.cddev

Edit crontab.cddev using emacs and add entries by following the format described above. The name convention for the backup crontab file should be like crontab.username.

$ emacs crontab.cddev

      submit the cron jobs with

$ crontab < crontab.cddev

      to list or verify the cron jobs submitted, use

$ crontab –l

      to remove the cron jobs, use

$ crontab –r

Trscrontab

The format for trscontab entries has an additional field, parameters:

parameters minute hour day-of-month month weekday command

Field parameters has a form of

host[,host[,...]][;tlife]

where host is the name of the host where command is to be run. When command starts it receives a token that expires in 15 minutes. You can override this default by specifying tlife which is the number of minutes that must elapse before the token expires. Nomarlly default time is enough.

Below is an example trscrontab entry under cddev account, defined in $HOME/trscrontab.cddev:

slcs2 0,5,10,15,20,25,30,35,40,45,50,55 * * * * (/afs/slac/g/cd/soft/prod/solaris/bin/import2gif_nlcta /afs/slac/g/cd/soft/html/images opi00gtw04) > /tmp/import2gif_nlcta.log 2>&1


It runs every 5 minutes on slcs2 and redirects the standard output and 
error to /tmp/import2gif_nlcta.log. The command which is enclosed in parentheses 
(/afs/slac/g/cd/soft/prod/solaris/bin/import2gif_nlcta /afs/slac/g/cd/soft/html/images opi00gtw04) 

captures a NLCTA CUD display and make it available on WEB.

Creating a trscrontab job follows the similar steps as crontab:

 create a backup trscrontab file

$ trscrontab -l >! trscrontab.cddev

      Edit trscrontab.cddev using emacs and add entries by following the format described above. The name convention for the trscrontab file should be like trscrontab.username. For example:

$ emacs trscrontab.cddev

      submit the cron jobs with

$ trscrontab < trscrontab.cddev

      to list or verify the cron jobs submitted, use

$ trscrontab –l

      to remove the cron jobs, use

$ trscrontab –r

You can submit the cron jobs (or list or remove) from any public UNIX 
machine, there is no need to log in the machine on which you want to run your cron
 jobs. Trscrontab server will schedule and redirect your job to a machine you
 specified in trscrontab entries. Please make sure that /usr/local/bin is included in
 your PATH in order to use trscrontab command. 







For a trscontab job running as shared AFS acount such as laci or acctf, one must have a password in order to submit the job. Be sure that
.ssh is owned by the shared account.  


Cron Shell


A UNIX cron shell, an added layer to UNIX cron system, has been developed, which helps setting up environment, parsing the augments and executing command. With this cron shell, we are now enable to support the release escalation for cron jobs. See /afs/slac/g/cd/soft/prod/solaris/bin/cronwrapper An example to use cronshell in trscrontab is
slcs2 0,5,10,15,20,25,30,35,40,45,50,55 * * * * (/afs/slac/g/cd/soft/prod/solaris/bin/cronwrapper import2gif_nlcta /afs/slac/g/cd/soft/html/images opi00gtw04) > /tmp/import2gif_nlcta.log 2>&1


Contact: Jingchen Zhou (X4661, jingchen@slac). Last edited on June 17, 03