- Kloudnative
- Posts
- 10 Must-Know Bash Scripts for DevOps Beginners to Automate Workflows
10 Must-Know Bash Scripts for DevOps Beginners to Automate Workflows
Practical Bash Scripts to Boost Automation and Optimize Workflows in DevOps
As a Junior DevOps engineer, mastering automation is key to increasing efficiency and reducing manual tasks. Ready to streamline your workflows? Here are 10 essential Bash scripts every beginner should know. Each script is broken down step-by-step, making it simple to implement and supercharge your DevOps processes.
1. Update and Upgrade System Packages
Purpose: Ensure the system is up-to-date with the latest security patches and software updates.
Script:
#!/bin/bash
echo "Updating system packages..."
sudo apt update && sudo apt upgrade -y
echo "System updated successfully!"
Explanation:
apt update
: This command refreshes the list of available packages and their versions from the repositories configured on the system. It does not actually install or upgrade any packages but ensures that the local package index is up-to-date with the latest available versions from the repositories.apt upgrade -y
: Once the package index is updated, this command upgrades all installed packages to their latest available versions. The-y
flag automatically accepts all prompts and confirms the installation of the updates, so you don’t need to manually approve each one. This is particularly useful when automating the update process, as it ensures no interruptions during execution.sudo
: This is used to execute commands with superuser (root) privileges. Since package updates and upgrades affect system-level configurations and files, administrative access is required. Thesudo
command ensures that the script can make the necessary changes to system files and install updates without permission issues.
This script automates the process of keeping your system secure by ensuring that all installed packages are up-to-date with the latest bug fixes and security patches. Regularly running this script helps prevent vulnerabilities and ensures the system is running the most stable and secure software versions.
Kloudnative is committed to staying free for all our users. We kindly encourage you to explore our sponsors to help support us.
Unlock Windsurf Editor, by Codeium.
Introducing the Windsurf Editor, the first agentic IDE. All the features you know and love from Codeium’s extensions plus new capabilities such as Cascade that act as collaborative AI agents, combining the best of copilot and agent systems. This flow state of working with AI creates a step-change in AI capability that results in truly magical moments.
☝️ Support Kloudnative by clicking the link above to explore our sponsors!
2. Monitor Disk Space
Purpose: Check disk usage and alert if usage exceeds a specified threshold.
Script:
#!/bin/bash
THRESHOLD=80
echo "Checking disk usage..."
USAGE=$(df / | tail -1 | awk '{print $5}' | sed 's/%//')
if [ "$USAGE" -gt "$THRESHOLD" ]; then
echo "Disk usage is above $THRESHOLD%! Current usage: $USAGE%"
else
echo "Disk usage is below $THRESHOLD%. Current usage: $USAGE%"
fi
Explanation:
df /
: This command checks the disk space usage for the root (/
) partition of the system. It reports various information such as the total disk space, used space, available space, and usage percentage for the root file system.
tail -1
: Thedf
command provides multiple lines of output, with the first line containing column headers. Thetail -1
command ensures we only get the last line of the output, which contains the actual disk usage statistics.awk '{print $5}'
: This command usesawk
, a text processing tool, to extract the fifth field of the output, which is the disk usage percentage (e.g.,80%
).sed 's/%//'
: Thesed
command is used to remove the%
symbol from the usage percentage (e.g.,80%
becomes80
). This is necessary because we want to compare a numeric value without the%
symbol.USAGE=$(...)
: This assigns the cleaned disk usage percentage (as an integer) to the variableUSAGE
.if [ "$USAGE" -gt "$THRESHOLD" ]; then
: Thisif
statement compares the current disk usage (stored inUSAGE
) with the specified threshold (in this case,80
). The-gt
operator stands for "greater than," so if the usage exceeds the threshold, the script proceeds to print a warning message.echo "Disk usage is above $THRESHOLD%! Current usage: $USAGE%"
: If the disk usage exceeds the threshold, this message is displayed, alerting you that your disk usage is critically high.else
: If the disk usage is not above the threshold, the script proceeds to theelse
block.echo "Disk usage is below $THRESHOLD%. Current usage: $USAGE%"
: If the disk usage is under the threshold, this message is printed, confirming that disk usage is within acceptable limits.
Why this script is useful:
This script helps monitor the health of your system by providing an alert if the disk usage exceeds a specified threshold. This is especially useful in server environments where disk space might fill up unexpectedly, potentially causing system performance issues or failures. By automating disk space checks, you can take proactive action (such as cleaning up files or adding more storage) before reaching critical levels.
3. Automate File Backups
Purpose: Back up a directory to a specific location.
Script:
#!/bin/bash
SOURCE="/path/to/source"
DEST="/path/to/backup"
DATE=$(date +%Y-%m-%d)
echo "Backing up files from $SOURCE to $DEST/$DATE..."
mkdir -p "$DEST/$DATE"
cp -r "$SOURCE"/* "$DEST/$DATE"
echo "Backup completed!"
Explanation:
Replace
/path/to/source
and/path/to/backup
with the actual paths.date +%Y-%m-%d
generates a timestamp for the backup folder.mkdir -p
creates the backup directory if it doesn't exist.cp -r
recursively copies all files.
4. Deploy a Simple Web Server
Purpose: Quickly set up a web server with Nginx.
Script:
#!/bin/bash
echo "Installing Nginx..."
sudo apt update
sudo apt install -y nginx
echo "Starting Nginx service..."
sudo systemctl start nginx
sudo systemctl enable nginx
echo "Nginx web server is up and running!"
Explanation:
Installs Nginx, a popular web server.
systemctl
starts and enables Nginx to run on boot.
5. Add and Manage Users
Purpose: Automate user creation with pre-defined privileges.
Script:
#!/bin/bash
USERNAME="newuser"
PASSWORD="password123"
GROUP="sudo"
echo "Creating user $USERNAME..."
sudo useradd -m -s /bin/bash "$USERNAME"
echo "$USERNAME:$PASSWORD" | sudo chpasswd
echo "Adding $USERNAME to $GROUP group..."
sudo usermod -aG "$GROUP" "$USERNAME"
echo "User $USERNAME created and added to $GROUP!"
Explanation:
useradd
creates a new user.chpasswd
sets the user password.usermod -aG
adds the user to a specified group (e.g.,sudo
).
6. Check Server Uptime
Purpose: Display how long the server has been running.
Script:
#!/bin/bash
echo "Server uptime:"
uptime
Explanation:
uptime
shows the server's uptime, load averages, and users logged in.
7. Check Active Processes
Purpose: Identify resource-intensive processes.
Script:
#!/bin/bash
echo "Top 5 processes by CPU usage:"
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head -6
Explanation:
ps
lists processes with details like PID, CPU, and memory usage.--sort=-%cpu
sorts processes by CPU usage in descending order.head -6
limits the output to the top 5 processes (plus the header).
8. Automate SSH Key Generation
Purpose: Generate SSH keys for secure remote access.
Script:
#!/bin/bash
KEY_NAME="id_rsa"
echo "Generating SSH key..."
ssh-keygen -t rsa -b 4096 -f "$HOME/.ssh/$KEY_NAME" -N ""
echo "SSH key generated at $HOME/.ssh/$KEY_NAME"
Explanation:
ssh-keygen
creates an RSA key pair with 4096 bits.-f
specifies the file name and location.-N ""
sets an empty passphrase.
9. Restart a Service
Purpose: Restart a service if it's not running.
Script:
#!/bin/bash
SERVICE="nginx"
if ! systemctl is-active --quiet "$SERVICE"; then
echo "$SERVICE is not running. Restarting..."
sudo systemctl restart "$SERVICE"
else
echo "$SERVICE is running."
fi
Explanation:
systemctl is-active
checks if the service is running.If not, the script restarts the service.
10. Schedule a Cron Job
Purpose: Automate task scheduling by adding a cron job.
Script:
#!/bin/bash
JOB="0 2 * * * /path/to/script.sh"
CRON_FILE="/tmp/mycron"
echo "Adding cron job..."
(crontab -l 2>/dev/null; echo "$JOB") | crontab -
echo "Cron job added successfully!"
Explanation:
Replace
/path/to/script.sh
with the script you want to schedule.(crontab -l; echo "$JOB")
appends the new job to existing ones.crontab -
loads the updated list of cron jobs.
Tips for Running the Scripts:
Save the Script: Save the script as a
.sh
file (e.g.,script.sh
).Make it Executable: Run
chmod +x script.sh
to make it executable.Run the Script: Execute it using
./script.sh
.Debugging: Use
bash -x script.sh
to debug if errors occur.
These scripts provide a solid foundation for automating repetitive tasks in DevOps workflows.