DEV Community

Cover image for Automate your tasks with Cron and Crontab
Phillip L. Cabrera M.
Phillip L. Cabrera M.

Posted on

Automate your tasks with Cron and Crontab

"In the digital age, efficiency and automation are essential to keep workflows agile and streamlined. Often, we find ourselves with tasks that need to be executed on a regular basis, whether it's backing up data, cleaning systems or generate reports.Instead of relying on manual intervention, which can be error-prone and time-consuming, wouldn't it be great to have a tool that allows us to schedule and automate these tasks?If you work with UNIX systems, you're in luck In this post, I'll introduce you to two powerful tools, Cron and Crontab, that can transform the way you manage and schedule tasks on your system. I'll share my recent experience with them and the essential lessons I've learned in the process."

I was recently given an assignment at work to learn how to use what is Cron and Crontab so I immediately began to investigate what these tools are and what they are used for, and here are the conclusions I could reach.

Cron

It is a daemon-type service that comes installed with any UNIX system which is responsible for reading certain types of instructions from Crontab-type files.

Crontab

It is a text file in which each line indicates the frequency and the scheduled script that you want to be executed either once or several times.

Suppose we have prepared a certain bash script that we want to run constantly.

1. Backup a folder recursively

We have the following script that is responsible for compressing the information of a certain folder.

#!/bin/bash
# Folder you want to compress
SOURCE_DIR="/path/to/your/folder"
# Name of the compressed file to be generated
OUTPUT_FILE="/output/path/my_compressed_file.tar.gz"
# compress the folder
tar -czvf $OUTPUT_FILE $SOURCE_DIR
echo "The folder $SOURCE_DIR has been compressed into $OUTPUT_FILE"

We could manually execute said script every day at the times we deem necessary, but this could become somewhat repetitive and, above all, take away valuable time that we could be using in other types of tasks. Here comes what is Cron and Crontab.

  • Open our terminal.
  • Run the command vim crontab -e This will open a screen similar to the following or it may show you a file with no text, which makes no difference. Now suppose we have saved the script to backup the information with the name makeBackup.sh and we want it to run every day at 8 am, 365 days a year.
  • Click the I key to put the VIM editor in edit mode and place the following line of code 00 08 * * * /path/makeBackup.sh

Let's explain this little snippet

  • 00 Will be executed at minute 00
  • 08 It will be executed at time 08
  • * It will be executed on any day of the month
  • * It will be executed in any month of the year
  • * Will run on any day of the week

Here you have in more detail the syntax structure of each line of a crontab file.

Position Description Values Example
1 Minute of the hour 0-59 00
2 Time of day 0-23 08
3 Day of the month 1-31 *
4 Month 1-12 (or names like JAN, FEB, etc.) *
5 Day of the week 0-7 (0 and 7 are Sunday) *

Note: The asterisk (*) in the crontab syntax is used to indicate "any value" or "always".

2. Create a DB Backup Script

1. First, write a bash script that backs up your database. of data.

#!/bin/bash

# Variables
DB_USER="your_username"
DB_PASS="your_password"
DB_NAME="database_name"
BACKUP_DIR="/backup/path"
DATE=$(date +"%Y%m%d")

# backup
mysqldump -u$DB_USER -p$DB_PASS $DB_NAME > $BACKUP_DIR/db_backup_$DATE.sql

echo "Backup completed for $DATE"

Save this file as db_backup.sh and grant execute permissions with:

chmod +x db_backup.sh

2. Configure Crontab

To schedule the task, we are going to use Crontab.

  • Open your personal Crontab with the following command:
crontab -e
  • To perform a daily backup at 2 a.m., add the following line:
0 2 * * * /path/to/script/db_backup.sh

This line indicates:

  • 0 Minute 0.
  • 2 Time 2 a.m.
  • * Any day of the month.
  • * Any month.
  • * Any day of the week.

3. Verify

After adding the task to Crontab, make sure everything is working correctly. You can set the task to run in the next minute and verify that the backup file is created in the specified location.

This example shows how to automate daily backups of a MySQL database. The key is the combination of the script that performs the backup and Crontab that schedules the regular execution of the script. I hope this example is useful to you and inspires you to explore more about the automation possibilities with Cron and Crontab!

Recommendations

It is important to take into account the following aspects when working with task automation with Cront and Crontab.

  • Always place an email at the beginning of the crontab file with the snippet MAILTO="yourmail@hotmail.com", this is quite useful for when the Cron daemon have to execute the script if there is an error in the script's syntax, you will receive the error in your email.
  • Use absolute paths to your scripts
  • Test your scripts by scheduling tasks to run within 5 minutes, so you can instantly debug any possible errors either in the way the task is scheduled, the script path, or in the script itself .
  • When creating your .sh scripts give it execute permissions, you can use chmod +x file.sh

Did you find this tutorial on Cron and Crontab helpful? Task automation is just the tip of the iceberg when it comes to maximizing efficiency on UNIX systems. If you want to learn more and keep your skills up to date, subscribe to my blog!. And if you think this article can help someone else, don't hesitate to share it on your social networks! Remember, sharing knowledge is a way to empower our tech community.

Top comments (0)