This week, we started our class with a revision session, which I've summarized here. I highly recommend reviewing it if you haven't done so already. Let’s dive into Bash Scripting as thought by our awesome instructors.
In today’s fast-moving IT landscape, automation is king. System administrators and DevOps engineers are expected to maintain reliability while managing increasingly complex infrastructures. Tools like Bash scripting, cron jobs, and Ansible help achieve this by reducing manual work, enforcing consistency, and scheduling repetitive tasks.
Bash Scripting
Bash (Bourne Again SHell) is the default shell in most Linux systems. A Bash script is simply a text file containing a sequence of commands that the system can execute.
Why Bash Scripts?
Automate repetitive tasks
Simplify system administration
Chain multiple commands into one execution
Reduce human error
Basic Structure of a Bash Script:
#!/bin/bash
# This script prints system info
echo "System Information:"
uname -a
df -h
Save this as sysinfo.sh
, then run:
chmod +x sysinfo.sh
./sysinfo.sh
Example Use Case:
Instead of typing multiple commands every morning to check server health, a sysadmin can run a Bash script that displays CPU usage, memory, and disk space in one go.
Bash Script Hands-On: Practical Examples
Example 1: Backup Script
#!/bin/bash
src="/home/user/documents"
dest="/backup/documents_$(date +%F).tar.gz"
tar -czf $dest $src
echo "Backup created at $dest"
This script compresses and stores backups with a date-based filename.
Example 2: User Creation Script
#!/bin/bash
read -p "Enter username: " username
sudo useradd $username
echo "User $username created successfully."
Real-World Value:
Imagine onboarding 10 new employees. Instead of creating accounts manually, a script can do it in seconds, reducing mistakes and saving time.
Cron Jobs: Scheduling Tasks Like a Pro
Cron is a time-based job scheduler in Linux. With crontab, you can automate script execution at defined intervals.
Crontab Syntax:
* * * * * command-to-execute
│ │ │ │ │
│ │ │ │ └── Day of week (0–6)
│ │ │ └──── Month (1–12)
│ │ └────── Day of month (1–31)
│ └──────── Hour (0–23)
└────────── Minute (0–59)
Examples:
- Run backup every night at 2 AM:
0 2 * * * /home/user/backup.sh
- Clear temp folder every Sunday:
0 0 * * 0 rm -rf /tmp/*
Scenario:
A company schedules log rotation, database backups, and cache cleanup via cron, ensuring consistency and freeing admins from routine maintenance.
Configuration Management with Ansible
While Bash and Cron handle automation locally, Ansible takes it to the next level by managing multiple servers simultaneously.
What is Ansible?
Open-source automation tool
Agentless (works over SSH)
Uses YAML-based playbooks
Ideal for provisioning, configuration, and deployment
Example Playbook: Install and start Apache on multiple servers:
- hosts: webservers
become: yes
tasks:
- name: Install Apache
apt:
name: apache2
state: present
- name: Start Apache
service:
name: apache2
state: started
Execution:
ansible-playbook apache.yml
Real-World Example:
Instead of manually installing and configuring Apache on 50 servers, a single Ansible playbook applies consistent configurations across all of them in minutes.
Final Thoughts
Mastering Bash scripting, cron jobs, and Ansible equips administrators with powerful automation skills:
Bash scripts simplify repetitive tasks.
Cron jobs ensure automation runs on schedule.
Ansible scales automation across entire infrastructures.
I truly appreciate your support throughout this journey and your enthusiasm is incredibly motivating! I encourage you to dive deeper into the concepts we've explored together. Remember, practice is key, so keep experimenting and refining your skills!
I’d love to hear your feedback and thoughts, so please leave a comment below to start a conversation!
I’m Ikoh Sylva, a dedicated Cloud Computing enthusiast with several months of hands-on experience in AWS. I’m documenting my cloud journey from a beginner's perspective right here. If this resonates with you, please like and follow my posts, and consider sharing this article with others who are starting their own cloud journeys. Together, we can learn and grow!
Feel free to connect with me on social media as well!
Top comments (0)