Advanced Linux Shell Scripting for DevOps Engineers with Crontab (Post-5/Part-III)
Backups are an important part of DevOps Engineer's day to Day activities The video in References will help you to understand How a DevOps Engineer takes backups (it can feel a bit difficult but keep trying, Nothing is impossible.) Watch this video
What is Cron?
Cron is a time-based job scheduler in Unix-like operating systems. It allows you to schedule tasks or scripts to run automatically at specific intervals or times. Cron jobs are widely used for automation, maintenance, and various other tasks in the background of a system.
What is Crontab?
Crontab is a command used to create, view, edit, and manage the list of cron jobs for a user. It stands for "cron table." The crontab command allows you to schedule and manage your cron jobs easily. Each user on a Unix-like system can have their own crontab file to define their scheduled tasks.
Creating you First ever Cronetab
Here's an example of how to create your first crontab entry:
Open a terminal window.
Type
crontab -e
to open your user's crontab for editing.Add a line to the file to schedule a task. The general syntax is:
* * * * * /path/to/your/script.sh
Each asterisk represents a time component (minute, hour, day of the month, month, day of the week). You can replace them with specific values or use
*
to indicate any value.Save and exit the editor.
For example, to run a backup script every day at 2:00 AM, you can use:
0 2 * * * /path/to/backup/script.sh
How DevOps Engineers Think?
How DevOps Engineers Think: DevOps engineers focus on breaking down the barriers between development and operations teams to facilitate a culture of collaboration, automation, and continuous improvement. They prioritize automating processes, streamlining deployments, and ensuring that software applications are delivered efficiently and reliably. DevOps engineers typically:
Automate Everything: They aim to automate repetitive tasks, from building and testing code to deployment and monitoring.
Infrastructure as Code: They treat infrastructure provisioning and management as code, using tools like Terraform or Ansible to ensure consistency and reproducibility.
Continuous Integration/Continuous Deployment (CI/CD): DevOps engineers emphasize implementing pipelines that automate the build, test, and deployment processes. This accelerates development cycles and reduces errors.
Monitoring and Feedback: They focus on setting up monitoring, logging, and alerting systems to gather data about application and infrastructure performance. Feedback loops help in continuous improvement.
Collaboration: DevOps encourages collaboration between development, operations, and other teams to break down silos and improve communication.
Security and Compliance: They integrate security practices into the development and deployment process to ensure that applications are secure and compliant with regulations.
Automating Tasks with Cron
Executing sell scripts using Crone (Disk/ Backup)
o execute shell scripts using Cron for disk and backup management, you'll need to follow these steps:
Create your shell script: Write a shell script that performs the disk or backup-related tasks you want to automate. Save this script with a
.sh
extension (e.g.,backup_script.sh
). Make sure the script is executable using thechmod
command.Open the Cron table: Use the
crontab -e
command to edit the current user's Cron table. This table contains the schedule and the commands to be executed.Add a Cron job: In the Cron table, you'll define when and how often your script should run. The format of a Cron job entry is as follows:
minute hour day month day_of_week command_to_execute
minute
: The minute when the script should run (0-59).hour
: The hour when the script should run (0-23).day
: The day of the month when the script should run (1-31).month
: The month when the script should run (1-12).day_of_week
: The day of the week when the script should run (0-6, where Sunday is 0).command_to_execute
: The command to execute, which will be the path to your shell script.
Examples:
Run the backup script every day at 2:00 AM:
0 2 * * * /path/to/backup_script.sh
Run the cleanup script every Sunday at 3:30 PM:
30 15 * * 0 /path/to/cleanup_script.sh
Run a disk check script on the 1st day of every month at 1:00 AM:
0 1 1 * * /path/to/disk_check_script.sh
Save and exit: After adding the desired Cron job(s), save and exit the editor.
Verify: You can verify your Cron jobs by listing the current user's Cron table using the
crontab -l
command.
Please note that Cron runs tasks in the background, and output from the scripts will typically be sent to the user's email. If you want to capture the output or handle errors, you can redirect output to a file within your script.
Remember to be cautious while using Cron, especially when dealing with critical tasks like backups and disk management. Test your scripts thoroughly before deploying them in a production environment.
Thank you for giving your precious time for reading this blog/article and also follow Shahaji Kadam for more such blogs/articles.
HashTags: #90daysofdevops #devops #cloud #aws #awscloud #awscommunity #docker #linux #kubernetes #k8s #ansible #grafana #terraform #github #opensource #90days #challenge #learning