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>
@reboot <command to execute>
/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.
systemctl status crond
journalctl -u crond
G to go to end of fileCtrl-B to go back 1 pagels /etc/cron.hourly/jobs.deny 2>/dev/null | grep 0anacron | wc -l
Return values:0 - Anacron is Enabled1 - Anacron is Disabledcat /etc/anacrontab
RANDOM_DELAY - specifies the maximum random number of minutes that can be added to the start time of each job. 0 = no Random Delay.START_HOURS_RANGE - specifies a time window (start hour to end hour) when jobs can run.To avoid spikes where jobs are run on multiple systems at the same time (which could cause issues where any resources are shared e.g. shared disk, or virtualised environments or where there could be a bottleneck in onward processing) configure randomization in /etc/sysconfig/run-parts...
RANDOMIZE=1
Can be 1 (enabled) or 0 (disabled)RANDOM=5
Can be any integer.
TODO
/etc/crontab
/var/spool/anacron
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 or 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
To see the timestamp of the last change to the crontab...
Solaris
ls -l /var/spool/cron/crontabs/user