Install Cron as Automate Tasks on Ubuntu 18.04

Cron, the thing that always heard on my ears for couple year ago. That  time i was involved with one project that need a job timer and that time one of the developer used Cron Job instead that usualy we always use it with Job Task Schedule, Sharepoint Job Timer or Sql Job.

What is Cron ?

Cron is a time-based job scheduling daemon found in Unix-like operating systems, including Linux distributions. Cron runs in the background and tasks scheduled with cron, referred to as “cron jobs,” are executed automatically, making cron useful for automating maintenance-related tasks.

So on this demo, i will digging a litte bit about how to install Cron on Ubuntu and test it. Here are the steps as follow :

1.Apt-Update

sudo apt update
sudo apt-get upgrade

2. Install Cron

sudo apt install cron

You’ll need to make sure it’s set to run in the background too:

sudo systemctl enable cron

cron1

Done and pretty easy right 🙂  We already installed the Crown and already running as well. So next action are we can use that for scheduling your task job.

Syntax Structure on Cron

Tasks scheduled in a cron are structured like this:

minute hour day_of_month month day_of_week command_to_run

Here’s a functional example of a cron expression. This expression runs the command curl http://www.google.com every Tuesday at 5:30 PM:

30 17 * * 2 curl http://www.google.com

There are also a few special characters you can include in the schedule component of a cron expression to make scheduling easier:

  • *: In cron expressions, an asterisk is a wildcard variable that represents “all.” Thus, a task scheduled with * * * * * ... will run every minute of every hour of every day of every month.
  • ,: Commas break up scheduling values to form a list. If you want to have a task run at the beginning and middle of every hour, rather than writing out two separate tasks (e.g., 0 * * * * ... and 30 * * * * ...), you could achieve the same functionality with one (0,30 * * * * ...).
  • -: A hyphen represents a range of values in the schedule field. Instead of having 30 separate scheduled tasks for a command you want to run for the first 30 minutes of every hour (as in 0 * * * * ...1 * * * * ...2 * * * * ..., and so on), you could just schedule it as 0-29 * * * * ....
  • /: You can use a forward slash with an asterisk to express a step value. For example, instead of writing out eight separate separate cron tasks to run a command every three hours (as in, 0 0 * * * ...0 3 * * * ...0 6 * * * ..., and so on), you could schedule it to run like this: 0 */3 * * * ....

Here are some more examples of how to use cron’s scheduling component:

  • * * * * * – Run the command every minute.
  • 12 * * * * – Run the command 12 minutes after every hour.
  • 0,15,30,45 * * * * – Run the command every 15 minutes.
  • */15 * * * * – Run the command every 15 minutes.
  • 0 4 * * * – Run the command every day at 4:00 AM.
  • 0 4 * * 2-4 – Run the command every Tuesday, Wednesday, and Thursday at 4:00 AM.
  • 20,40 */8 * 7-12 * – Run the command on the 20th and 40th minute of every 8th hour every day of the last 6 months of the year.

The more detail this you can read on this article https://crontab.guru/ and here https://linuxhint.com/run_cron_job_every_minute/

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.