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
/etc/cron.allow - If this file exists, it must contain the user's name for that user to be allowed to use cron jobs.
/etc/cron.deny - If the cron.allow file does not exist but the /etc/cron.deny file does exist then, to use cron jobs, users must not be listed in the /etc/cron.deny file.
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
Bibliography
https://en.wikipedia.org/wiki/Cron
AWShttps://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html
lock/mutex fileshttps://dev-random.net/linux-lockfile-explained-how-to-use-them-the-easy-or-hard-way/ https://unix.stackexchange.com/questions/288446/use-of-lockfile-in-script-and-it-significancehttps://en.wikipedia.org/wiki/Lock_(computer_science)https://stackoverflow.com/questions/1715137/what-is-the-best-way-to-ensure-only-one-instance-of-a-bash-script-is-runninghttps://gist.github.com/przemoc/571091
Crontab Fileshttps://www.unix.com/shell-programming-and-scripting/132338-how-check-crontab-edited-date-time.html