Book Image

Bash Cookbook

By : Ron Brash, Ganesh Sanjiv Naik
Book Image

Bash Cookbook

By: Ron Brash, Ganesh Sanjiv Naik

Overview of this book

In Linux, one of the most commonly used and most powerful tools is the Bash shell. With its collection of engaging recipes, Bash Cookbook takes you through a series of exercises designed to teach you how to effectively use the Bash shell in order to create and execute your own scripts. The book starts by introducing you to the basics of using the Bash shell, also teaching you the fundamentals of generating any input from a command. With the help of a number of exercises, you will get to grips with the automation of daily tasks for sysadmins and power users. Once you have a hands-on understanding of the subject, you will move on to exploring more advanced projects that can solve real-world problems comprehensively on a Linux system. In addition to this, you will discover projects such as creating an application with a menu, beginning scripts on startup, parsing and displaying human-readable information, and executing remote commands with authentication using self-generated Secure Shell (SSH) keys. By the end of this book, you will have gained significant experience of solving real-world problems, from automating routine tasks to managing your systems and creating your own scripts.
Table of Contents (15 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

Creating and configuring cron Jobs for task scheduling


In this section, we are going to learn how to configure Cron Jobs. We are going to use crontab to set up a Cron Job.

How to do it...

  1. Open your terminal and go to the /etc folder and check the /cron folders. You will see the following cron folders:
    • /etc/cron.hourly
    • /etc/cron.daily
    • /etc/cron.weekly
    • /etc/cron.monthly
  2. Now, we will copy our shell script into one the preceding folders. 
  3. If you need to run your shell script to run daily, place it in the cron.daily folder. If you need to run it hourly, place it in the cron.hourly folder, and so on.
  4. Example: Write a script and place it in the cron.daily folder. Make the script executable by giving the necessary permissions.
  5. Now, run the crontab command:
$ crontab -e
  1. Press Enter and it will ask for the editor of your type. By default, it will open vi editor. In my case, I selected nano. Now, create a cron command. The syntax for creating the cron command is:
    • The number of minutes after the hour (0 to 59...