cron

To list the cron schedule...

crontab -l

To edit the cron schedule...

crontab -e

This edits the crontab in your default editor (usually vi)

# ┌───────────── minute (0 - 59)

# │ ┌───────────── hour (0 - 23)

# │ │ ┌───────────── day of the month (1 - 31)

# │ │ │ ┌───────────── month (1 - 12)

# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday; 7 is also Sunday on some systems)

# │ │ │ │ │ ┌───────────── year (on AWS)                                 

# │ │ │ │ │ │

# │ │ │ │ │ │

# * * * * * * <command to execute>

cron.allow / cron.deny

Lock/Mutex files

It is good practice to use a lockfile to prevent multiple instances of your job from running. You can either code this manually or install commands like lockfile of flock.

Note that lockfile has a dependency on postfix which you may not want installed.

Using a lockfile containing PID (do this as first step in your script)...

myLF=/var/lock/myscript.lock

touch ${myLF}

read myPID < ${myLF}

[ ! -z "${myPID}" -a -d /proc/${myPID} ] && exit

echo $$ > ${myLF}

...


# Set variable with lockfile name

# Create an empty lockfile if it doesn't exist

# Read any PID from the lockfile

# If PID is not null and the process exists, then exit

# Add the PID of the current script to the lockfile

# Your script steps go here

Using an empty lockfile...

myLF=/var/lock/myscript.lock

trap " [ -f ${myLF} ] && /bin/rm -f ${myLF}" 0 1 2 3 13 15 

[ -f ${myLF} ] && exit

touch ${myLF}

...

rm -f ${myLF}


# Set variable with lockfile name

# Delete the lockfile when the script terminates

# Check if the lockfile exists and exit if it does

# Create the lockfile

# Your script steps go here

# Delete the file (but trap should handle it if you don't)

Using an empty diretory as a lockfile...

myLF=/var/lock/myscript.lock

trap " [ -d ${myLF} ] && /bin/rmdir ${myLF}" 0 1 2 3 13 15 

[ -d ${myLF} ] && exit

mkdir ${myLF} || exit

...

Works in a similar way to the empty lockfile above but avoids the theoretical situation where two scripts run at almost the same instant, both think the file does not exist, and both touch the same file (which is a valid operation and will not throw an error).

Using lockfile...

myLF=/var/lock/myscript.lock

trap " [ -f ${myLF} ] && /bin/rm -f ${myLF}" 0 1 2 3 13 15 

lockfile -r 0 ${myLF} || exit 1

...

The lockfile -r flag specifies the number of retries

# Set variable with lockfile name

# Delete the lockfile when the script terminates

# Check if the lockfile exists and exit if it does

# Your script steps go here

# Delete the file (but trap should handle it if you don't)

Using flock...

flock -n /var/lock/myscript.lock /tmp/myscript.sh



Flock needs no changes to your script. It uses the PID method under the covers. The -n flag forces it to exit if the process is running. Without it flock will wait for the existing process to finish.

Last Modification Date

To see the timestamp of the last change to the crontab...

Solaris

ls -l /var/spool/cron/crontabs/user