Cron job vs Crontab

Cron job vs Crontab

We learned how to take a backup using scripts, and with just one command, the backup is complete.

However, suppose we must perform a daily backup at a set time. Therefore, you must log in at that time before running the backup script. It's possible that we forgot to run the script one day.

Let's now assume that we hired someone to execute that script every day, with their sole responsibility being to wake up and execute a backup script to back up our data. which will simplify our work and reduce the likelihood of errors.

Cron is the program or service that lets us schedule or automate jobs in Linux.

The crontab is a list of commands that you want to run on a regular schedule, and also the name of the command used to manage that list. Crontab stands for “cron table, ” because it uses the job scheduler cron to execute tasks;

Let's ask Linux about Crontab and how to use it using thecat /etc/crontab command.

To check the crontab available, the command is crontab -l. currently there is no crontab available for root user.

let's create a crontab

crontab -eThis will open the crontab file in a text editor. we can use the format and give the time data and the commands to run.

after saving the crontab file we can check it using the crontab -l which will list down all the crontab available.

output: We can see that at the exact time it completed the task.

Task 3:

Cron and Crontab, to automate the backup Script and update a log file that "backup is completed".

Cron is the system's main scheduler for running jobs or tasks unattended. A command called crontab allows the user to submit, edit, or delete entries from cron. A crontab file is a user file that holds the scheduling information.

Yesterday, we created a backup script that automatically creates a backup and stores it in a folder.

Now we need to write a crontab so it will automatically run daily at some specific time.

the script for the backup is:

#!/bin/bash

src_dir=/root/Scripts
target_dir=/root/backups

curr_timestamp=$(date "+%Y-%m-%d-%H-%M-%S")
backup_files=$target_dir/$curr_timestamp.tgz
echo "taking backup on $curr_timestamp"
tar czf $backup_file --absolute-names $src_dir

echo "backup Completed"

crontab for the daily backup will be

output: At the time mentioned, the backup script will automatically get executed.

Thanks for reading! I hope you understood these concepts and learned something.
If you have any queries, feel free to reach out to me on LinkedIn.

Did you find this article valuable?

Support Ritik Gupta Blog's by becoming a sponsor. Any amount is appreciated!