How to Schedule Your Cron Jobs for Scheduled Tasks
Are you too busy to issue commands? Using cron to schedule tasks means you don’t have to stay up late to run programs.
System administrators have to perform tasks when they would prefer to sleep. Some tasks, such as regularly occurring ones, must be run overnight or on weekends when fewer people are expected to use computer resources. As an evening worker, there often is no time to run commands or scripts that require off-hour performance. Backups or major updates should not take place in the wee hours of the morning without some type of system automation.
A type of service utility that can be used for system automation and allows you to run commands, programs, and tasks at predetermined times is a cron service. The cron service enables administrators to set future times for running tasks. The cron service can schedule tasks on a repetitive basis, such as daily, weekly, or monthly.
In this article, I’ll introduce the cron service and how to use it.
What are Cron Jobs and why use it?
A cron job is a Linux command that schedules tasks for execution in the future. This is normally used to schedule regular tasks, such as sending out a notification every morning.
Cron jobs in Linux are used for the following reasons:
- Allows OS to take scheduled backups of log files and databases.
- Removes old log files
- Archives and purges database tables
- Sends out notifications, such as newsletters, Message of the Day (MOTD), or password expiration emails
- Cleans up cached data regularly
- Automates system maintenance
What is Cron Tab and How Do You Use it?
Cron derives its name from the Greek word Chronos, meaning time. This is a process that runs on the system automatically according to a schedule. Regular schedule tasks are run using its commands. Crontab is short for “cron table”. It allows you to perform tasks using the job scheduler, also known as cron.
This program, called Cron tab, also edits schedules. Shell commands are specified in a crontab file and are executed on a specified schedule.
Three components are present in most crontabs:
- The time and date of execution.
- The script to be executed or called (this is called the command execution).
- The destination where the output of the command is stored. This could be placed in a system file or sent out as an email notification, for example.
What is the Linux Crontab format?
In Linux, there are six fields in the Contab. The first five columns define the time and date of the execution, and the sixth is used to specify the command’s execution.
Below is the Crontab syntax:
Before you can get to the crontab, it is imperative for you to know how to call the crontab from your terminal. Below are a few commands that you can place in your CLI in order to observe, edit, or manipulate the crontab:
New cron files are empty, so commands must be added from scratch. Here are some crontab command examples for understanding the formatting. In these examples, I have created a script named webTester.sh that validates whether a website exists or not. If the website exists, it sends back an “ok” message; if the website does not exist, it sends back an error message, saying either the website cannot be found or the website is down. The crontab will automate checking to see if my the website I am testing against my script is valid or causes an error. The website being tested in these examples is “google.com”.
Given command to execute at 10AM and 6PM daily:
Given command to execute every 7 minutes:
Given command to execute every Wednesday at 5AM:
Given command to execute every 10 minutes:
Given command to execute every 3 months (quarterly):
Given command to execute every Monday and Wednesday at 3:15PM:
Given command to execute at beginning of hour, every 6 hours:
Automating our tasks as System Administrators may enable us to perform these such tasks in a more efficient, error-free, and effective manner. You can find more information on crontab’s manual page by typing ‘ man crontab’ in your terminal. If you would like more information on how to format your crontab command correctly, please visit the following websites: