Cron is a Linux based utility for scheduling time-based jobs that run automatically at a set time, date, or after a specific interval. You can automate various repetitive administrative tasks (e.g. database backups, email reminders, etc) using cron jobs.
You can use crontab to automate and schedule tasks on your Linux machine. This is simply a file-based configuration table with lists of jobs and timings when tasks are scheduled to run.
This step-by-step guide will walk you through the whole process of automating and scheduling tasks with crontab on AWS cloud a certified consulting partner in the AWS Partner Network, Zindagi Technologies provides consulting, development, and support for companies with AWS deployments. We rely on AWS capabilities to deliver enterprise cloud solutions, microservices, infrastructures as code, CI/CD cycle automation, and more.
We assist our customers in determining the best cloud technology for their needs. We advise organizations on the best tech stacks to help them optimize their operations by improving software characteristics (performance, functional maturity, device compatibility) and lowering maintenance costs.
- A valid AWS Cloud Account is Required.
- An AWS instance running Rhel 7 or Ubuntu 16.04 Operating System.
- A non-root user with Sudo privileges.
Step 1: SSH To Your AWS Instance
Identify the public IP address associated with your AWS instance. Then, using an SSH client, connect to your server. If you are running Windows on your local computer, you can use PuTTY. However, Linux and macOS have built-in command line interfaces that you can use without downloading third-party applications.
Note:- some basic commands used to be remembered before working on cronjobs:
Step 2: Configuring the Crontab File
Next, we are going to open the crontab configuration file using nano for editing purposes. The file is located at /etc/crontab. Run the command below to open it:
The basic syntax for writing cron jobs is listed towards the end of the file with a few scheduled tasks to get you started.
First, Let’s go over the structure and arrangement of jobs before we start creating cron jobs.
Step 3: Examples of Real-Life Cron Jobs on Rhel 7
With our well-thought and complete explanation of how cron job syntax is written above, let us create and explain some cron jobs that you can use in real-life to prove the concept.
A Cron Job for Downloading a File at Midnight
The cron job below will download a file from the example.com website in the middle of the night exactly at 00:00 hrs. Please note we have used zeros for the first 2 parts. The wildcard characters (*) on the other parts instruct the cron to run every month, day or weekday. This means the cron will run daily at midnight.
A Cron Job That Runs Every Minute
Sometimes, you might feel the need to create a cron job that runs every minute. You can use the syntax below. However, use this sparingly as it may haunt your server resources if the command involves long running tasks.
A Cron Job That Runs Once a Year at July 21st at 8:37 AM
The below values represent the months of the year:
The below cron will run once a year on the 21st day of July (7th Month) specifically at 8:37 AM:
We have specified * on the dow part to let the cron run irrespective of the day of the week when our date falls.
A Cron Job That Runs Each Week on Wednesday at 5:00 AM
The days of the week are represented with the below values ;
So to schedule a cron job to run on Wednesday, we will use a value of 3 on the dow(day of the week) part. Also, we will use 5 on the h(hour) part to signify 5 PM and 0 on the minute part to signify the top of the hour. We can use * on the other values to let the cron run irrespective of the date or month of the year.
Step 4: Setting Multiple Values on Cron Jobs
Although you can set multiple cron jobs to run at different timings, you can make the configuration file cleaner by setting multiple timings on one line by separating values with commas.
For instance, if you want a cron job to run on the 1st, 17th, and 59th minutes, you can use the syntax below:
Likewise, to run the cron at 4, 6 and 9 AM use the syntax below
To run a task on the 1st and 16th day of the month at midnight, use the below syntax
To run a task on January, April, July, and October (Quarterly) at midnight on the 1st day of the month, use the syntax below:
To run a task on Wednesdays and Sundays at 4:00 AM, use the syntax below:
Step 5: Setting Up a Cron Job to Run After a Specific Time Interval
You can use the modulus operator(/) to run a cron job after every specific time interval. For instance, to run a cron job after every 15 minutes, use the syntax below:
This means the cron will run on the 0th, 15th, 30th, and 45th minutes. That is 4 times every hour.
Step 6: Executing PHP Scripts Using Cron Jobs
You can execute a PHP script using cron job by using the below syntax:
For instance, the cron job below will trigger a PHP script that creates automatic MySQL backups for a server each day at midnight:
Step 7: Setting Email Notifications When a Cron Job Is Executed
For testing purposes, you can configure crontab to send an email every time a cron job is executed. To do this, include the line below at the top of your /etc/crontab file:
Remember to replace [email protected] with the correct email address where you would like to receive the emails.
By default, you will receive an email every time a cron job is executed. If you think this is not desirable for some of the tasks that you have scheduled, redirect their output to >/dev/null 2>&1 as shown below:
We hope this article explains how to automate and schedule tasks with crontab in Rhel 7. If you need further help in any such task or if you are looking for professional services for collaboration, IT consulting or data centre implementation then your search for the IT consulting partner stops here.
Zindagi Technologies Pvt. Ltd. is a leading IT consultant and managed cybersecurity company situated in New Delhi, India. We have expertise in planning, designing, and deployment of collaboration environments, large-scale data centers, and Private/Public/Hybrid cloud solutions. We believe in “Customer First” and provide quality services to our clients always. To get in touch with us drop us a message at 9773973971 or you can also reach us via email.