X
X
X
X

Knowledge Base

HomepageKnowledge BaseHostingWhat is CronJob?

What is CronJob?

Definition

In Unix or Unix-like operating systems, Cron is a job scheduler that is based on time. Cron can be used for job scheduling, such as running shell scripts or commands at specific times, intervals, or dates. That enables the option of automating the system management or maintenance, the download of files or sending emails regularly, for instance. It is a daemon, which means it is a background process always running on the server. CronJobs are the tasks that Cron must complete. Cron derives its name from the Greek god of time, Chronos.

Application of CronJobs

CronJobs are usable for executing specific commands or automating the execution of recurrent sequential tasks, such as cleaning up databases by eliminating log files, comments, and obsolete entries or generating regular statistics on the number of visitors to a site.

Other uses of the CronJobs involve publishing new content to a site, automating newsletter delivery, updating RSS feeds, and generating multiple invoices. CronJobs can also be used to schedule database backups.

Structure and syntax of a CronTab file

Cron makes use of special configuration files known as CronTab files, which include a list of cron jobs that require execution. Cron Table is the definition of CronTab. A CronJob is represented by every line inside the CronTab file. It resembles a row of columns divided by a gap. Each line indicates when and how frequently a script or a specific command should be run.

Blank lines or lines starting with tabs, #, or spaces are disregarded in a CronTab file. If there is a # at the start of a line, it is denoted as a user comment.

A CronTab's active lines either specify a CronJob or declare an environmental variable. There is a restriction for user comments when it comes to active lines.

In a command-line interpreter, the use of "crontab-e" opens the CronTab file. The Cron daemon checks the commands alongside their runtime in the CronTab file before executing them in the background of the system. The following basic structure is seen in every CronTab, six columns arranged in a table-like fashion:

* * * * * command to execute

The first five columns provide time values that specify the execution time of a particular command. From left to right, these are:

  • Minute, specified as 0-59
  • Hour, specified as 0-23
  • Day, specified as 1-31
  • Month specified as 1-12
  • Weekday, specified as 0-7, 0 or 7 are Sundays

The wildcard "*" is used for execution at any moment (that is, at any minute, hour, day, weekday, or month), "*/n" for activation each n (hours, days, etc.), and "n,x,y" for execution on/at n, x, or y. The final column defines whether the script should be performed in a relative or absolute path. The A CronJob's execution is performed when the time on the schedule matches the current time.

Special strings are also supported by certain Cron implementations. These strings replace the first five lines in the CronTab. Every string defines a different frequency:

  • @yearly / @annually: Execution once a year at midnight of January 1st (0 0 1 1 *)
  • @monthly: Execution once a month at midnight of the first day of the month (0 0 1 * *)
  • @weekly: execution once a week at midnight on Sunday (0 0 * * 0)
  • @daily: Execution once a day at midnight (0 0 * * *)
  • @hourly: execution at the beginning of each hour (0 * * * * *)
  • @reboot: one-time execution at system start

CronJob examples

A CronJob might include backing up a database every day at 2:00 a.m. The following would be in the CronTab file in this case:

0 2 * * */bin/sh backup.sh

It is sometimes required to arrange task execution for specific months exclusively. In the months of January, May, and August, the following sample script runs:

* * * 1,5,8 */bin/sh script.sh

If your desire is for tasks to execute on specified days and times, like every Friday and Sunday at 5 p.m., the CronTab script should look like this:

0 17 * * 0,5/bin/sh script.sh

Importance for online marketing

CronJobs allows you to automate numerous processes in online marketing, enhancing efficiency. CronJobs can be used to compile visitor statistics for a site and send them to a recipient at predefined times, which is in addition to the automation of time-consuming operations. CronJobs can be used in conjunction with CMS to generate metrics automatically or deliver newsletters on particular dates as part of marketing initiatives.

In summary: CronJob FAQs

What does * mean in Cron?

In Cron, the asterisk * acts as a wildcard. It causes a task to be executed at any time.

What is Cron used for?

Cron lets you plan jobs and enables the automation of regular processes like newsletter delivery.

What is a CronTab?

Each CronJob is stored in a CronTab file.

How do you schedule a CronJob that does the backup at 2:00 am every day?

That's how a daily backup at 2 a.m. might look like:

0 2 * * */bin/sh backup.sh

Can't find the information you are looking for?

Create a Support Ticket
Did you find it useful?
(5508 times viewed / 1238 people found it helpful)

Top