How to Automate a Bash Script by using the Terminal and Cron in Ubuntu

How to Automate a Bash Script by using the Terminal and Cron in Ubuntu

Overview

This is an introductory article to explain how to automate Bash scripts in Ubuntu by using the Terminal and a cronjob or a technique called cron.

Materials

  • Computer

  • Ubuntu Operating System

  • Terminal

  • Packages required: nano, cron

How to Install Nano and Cron

Before installing any package, it is good practice to run two commands: 'apt update' and then 'apt upgrade'.

'apt update' to receive the latest updates to any existing packages or retrieve new packages that are available to download.

apt update

After running 'apt update', the new packages or updates can be downloaded by running the command 'apt upgrade'.

apt upgrade

After updating and upgrading the operating system, any packages can be downloaded if specified, including nano and cron.

Nano is a command that can be used to make new files or edit existing files in the terminal. Install nano:

apt install nano

cron or a cronjob, enables a script to be executed on a specified schedule. Instead of manually running the script every time, a computer can be told to run the script at a specific time by using a cronjob. Install cron:

apt-get install cron

If the installation commands did not work, use sudo before the commands to use administrator privileges to download the packages.

Procedure

After cron is installed, type the command 'crontab -l'. The snapshot generates a lot of commented text explaining what steps to take and how to make a cronjob to execute a script. It also shows what scripts will be running. In this demonstration, a script called "example_script.sh" will be created and ran.

crontab -l

The "example_script.sh" is chosen to be run since this file can be edited by using the 'crontab -e' command to run any specific script.

crontab -e

Implement the Command to Automate a Script

The command below was implemented after editing the crontab:

"* * * * * /bin/bash -c /home/andrew/Documents/Scripts/example_script.sh" >> "example_script_output.txt"
  • "* * * * *" - Each "*" is a parameter that indicates a schedule for script to follow for it to be executed. The first to the last "*" indicates, when the script will be running for which minute, hour, day, month, or day in the week respectively. Instead of "*", numbers and other symbols can be written instead to execute the script on a different schedule. For a reference to make a cronjob, use the crontab guru website (1).

  • /bin/bash - Since the bash terminal is being used, the "/bin/bash" parameter is needed to execute a shell script.

  • -c - Used to read any "strings" in a script accurately and correctly.

  • /home/andrew/Documents/Scripts/example_script.sh - Specify the file and the directory it is located in the format: directory/..../file_name.file_name_extension. "..." means there can be more than one directory or folder where the file is located in. In this case, the file that was run was "example_script.sh" which was located within four directories.

  • ">>" - The commands in "example_script" will be appended to the specified file, which is "example_script_output.txt".

  • "example_script_output.txt" - Receives commands executed from "example_script.sh". Since a directory was not specified with the file, it will be generated in the home directory or the default directory when the Terminal is first opened.

By using the nano command, any file with any file extension can be created in the Terminal. Below shows the "example_script.sh" was created.

nano example_script.sh

In the "example_script.sh" the bash shebang and an echo statement were included to execute the string "Hello! Just running a sentence here."

Since the cron job command is designed to append what is executed to "example_script_output.txt", it can be checked after the "example_script.sh" file was automated to run a couple of minutes.

After checking the "example_script_output.txt", the string "Hello! Just running a sentence here." is repeated four times since the cron job runs the script for every minute that passed, meaning four minutes have passed to execute the script.

By using crontab -l and crontab -e, scheduling a cronjob command and other commands can be used to automate a script. Be careful, that using one ">" instead of two ">>" overwrites the most recent content with the latest content, which is shown below.

After editing the crontab, it always shows new or no modifications have been made to the crontab. After making modifications, it's also good practice if the files are located in the right place and the syntax for the cron job and the files are right to make sure it keeps executing.

By using one ">" it erased all the existing "Hello! Just running a sentence here." because it overwrote all existing content with the latest command.

Sources

https://crontab.guru/ (1)