Shell Scripting / Shell Script Automation

Scheduling Shell Scripts with Crontab

This tutorial will walk you through the process of scheduling shell scripts with Cron Jobs. You will learn how to automate tasks to run at specific times.

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Covers automating repetitive tasks using shell scripts.

Introduction

This tutorial aims to guide you on how to schedule shell scripts using Crontab. The Crontab is a simple yet powerful tool in Unix-like operating systems that enables you to automate tasks to run at specified times.

By the end of this tutorial, you will learn:
- What Crontab is and how it works
- How to create, edit, and manage cron jobs
- How to schedule a shell script to run at specific intervals

Prerequisites
You should be familiar with basic Unix/Linux command line syntax. Additionally, you should have a Unix/Linux environment to practice the examples given in this tutorial.

Step-by-Step Guide

Crontab, short for 'cron table', is a configuration file that specifies shell commands to run periodically on a given schedule. The name 'cron' comes from the Greek word 'chronos', meaning 'time'.

Each line of a crontab file represents a job and follows a particular syntax to schedule the job. The syntax is as follows:

* * * * * command

Each asterisk can be replaced with:

  • Minute (0 - 59)
  • Hour (0 - 23)
  • Day of the month (1 - 31)
  • Month (1 - 12)
  • Day of the week (0 - 7) (Sunday = 0 or 7)

Let's now dive into practical examples.

Code Examples

Example 1: Edit Crontab File

To edit the crontab file, we use the crontab -e command. This command opens the crontab file in the default text editor.

crontab -e

Initially, this file will be empty. You can add your jobs one by one.

Example 2: Schedule a Job

Let's say you want to schedule a shell script (script.sh) to run every day at 12 PM. Here is how to do it:

# Open the crontab file
crontab -e

# Add the following line
0 12 * * * /path/to/script.sh

The above line means: "Run the script.sh file at 0 minutes (the beginning) of the 12th hour of every day, every month, every day of the week."

Example 3: View Crontab Jobs

To list all the cron jobs currently scheduled for your user, use the crontab -l command:

crontab -l

This will list all the jobs you have scheduled.

Summary

In this tutorial, we have learned about cron jobs and how to schedule them using the crontab command in Unix/Linux. We have seen how to create, edit, list, and understand the syntax of a cron job.

The next steps would be to explore more complex scheduling and using cron jobs to automate more tasks. You can learn more about advanced crontab options from the Crontab Guru website.

Practice Exercises

Exercise 1: Schedule a script to run every minute.

Solution: Open the crontab file (crontab -e) and add the following line: * * * * * /path/to/script.sh. This means the script will run every minute of every hour of every day.

Exercise 2: Schedule a script to run every Monday at 5 PM.

Solution: Open the crontab file (crontab -e) and add the following line: 0 17 * * 1 /path/to/script.sh. This means the script will run at 5 PM (17:00) every Monday.

Keep practicing with different schedules to become more comfortable with crontab!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

File Size Checker

Check the size of uploaded files.

Use tool

JavaScript Minifier & Beautifier

Minify or beautify JavaScript code.

Use tool

Random Password Generator

Create secure, complex passwords with custom length and character options.

Use tool

QR Code Generator

Generate QR codes for URLs, text, or contact info.

Use tool

Favicon Generator

Create favicons from images.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help