Cron Jobs in Detail

Cron Jobs in Detail

The cron utility runs based on commands specified in a cron table (crontab). 
Each user, including root, can have a cron file. These files don't exist by default but can be created in the /var/spool/cron directory using the crontab -e command that's also used to edit a cron file (see the script below). 
I strongly recommend that you not use a standard editor (such as Vi, Vim, Emacs, Nano, or any of the many other editors that are available). 
Using the crontab command not only allows you to edit the command but also restarts the crond daemon when you save and exit the editor. The crontab command uses Vi as its underlying editor.

----- To create a new Cron Job -----
crontab -e

----- To see current Cron Job ------
crontab -l

For details see man/--help for crontabs:

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed


The first three lines in the code above set up a default environment. 
The environment must be set to whatever is necessary for a given user because cron does not provide an environment of any kind. 
The SHELL variable specifies the shell to use when commands are executed. This example specifies the Bash shell. 
The MAILTO variable sets the email address where cron job results will be sent. 
These emails can provide the status of the cron job (backups, updates, etc.) and consist of the output you would see if you run the program manually from the command line. 
The third line sets up the PATH for the environment. Even though the path is set here, I always prepend the fully qualified path to each executable.

There are several comment lines in the example above that detail the syntax required to define a cron job. I'll break those commands down, then add a few more to show you some more advanced capabilities of crontab files.

01 01 * * * /usr/local/bin/rsbu -vbd1 ; /usr/local/bin/rsbu -vbd2
This line runs my self-written Bash shell script, rsbu, that backs up all my systems. 
This job kicks off at 1:01 a.m. (01 01) every day. 
The asterisks (*) in positions three, four, and five of the time specification are like file globs, or wildcards, for other time divisions; they specify "every day of the month," "every month," and "every day of the week." 
This line runs my backups twice; one backs up to an internal dedicated backup hard drive, and the other backs up to an external USB drive that I can take to the safe deposit box.

03 05 * * * /sbin/hwclock --systohc
This line sets the hardware clock on the computer using the system clock as the source of an accurate time. This line is set to run at 5:03 a.m. (03 05) every day.

# 25 04 1 * * /usr/bin/dnf -y update
I was using the third and final cron job (commented out) to perform a dnf or yum update at 04:25 a.m. on the first day of each month, but I commented it out so it no longer runs.

---------- Other scheduling tricks  ---------- 

Now let's do some things that are a little more interesting than these basics. Suppose you want to run a particular job every Thursday at 3 p.m.:

00 15 * * Thu /usr/local/bin/mycronjob.sh
This line runs mycronjob.sh every Thursday at 3 p.m.

Or, maybe you need to run quarterly reports after the end of each quarter. The cron service has no option for "The last day of the month," so instead you can use the first day of the following month, as shown below. (This assumes that the data needed for the reports will be ready when the job is set to run.)

02 03 1 1,4,7,10 * /usr/local/bin/reports.sh
This cron job runs quarterly reports on the first day of the month after a quarter ends.

The following shows a job that runs one minute past every hour between 9:01 a.m. and 5:01 p.m.
01 09-17 * * * /usr/local/bin/hourlyreminder.sh
Sometimes you want to run jobs at regular times during normal business hours.

I have encountered situations where I need to run a job every two, three, or four hours. 
That can be accomplished by dividing the hours by the desired interval, such as */3 for every three hours, or 6-18/3 to run every three hours between 6 a.m. and 6 p.m. 
Other intervals can be divided similarly; for example, the expression */15 in the minute's position means "run the job every 15 minutes."

*/5 08-18/2 * * * /usr/local/bin/mycronjob.sh
This cron job runs every five minutes during every hour between 8 a.m. and 5:58 p.m.

One thing to note: The division expressions must result in a remainder of zero for the job to run. 
That's why, in this example, the job is set to run every five minutes (08:05, 08:10, 08:15, etc.) during even-numbered hours from 8 a.m. to 6 p.m., but not during any odd-numbered hours. 
For example, the job will not run at all from 9 p.m. to 9:59 a.m.

I am sure you can come up with many other possibilities based on these examples.

<<<<<----- Thank You ----->>>>>
    • Related Articles

    • DB2

      ----- Run DB2 as a Docker Container ----- docker run -d --restart always -p 50000:50000  --name any-name --privileged=true  -e LICENSE=accept -e DB2INST1_PASSWORD=123 -e DBNAME=any-name -v set-path-on-local:/database ibmcom/db2 ----- To get into the ...