Essential Shell Scripting for DevOps Interview Preparation
TLDR: This blog post covers essential shell scripting tasks that candidates can prepare for DevOps interviews, including scripts for memory checks, user creation, file management, package updates, log counting, and service checks. It emphasizes the importance of explaining thought processes during interviews.
In the realm of DevOps, shell scripting is a crucial skill that candidates must demonstrate during interviews. When you inform an interviewer about your experience with shell scripting and automation, they may inquire about specific scripts you have written and the tasks you have automated. This blog post will outline several common shell scripting tasks that you can discuss in your interviews, along with their solutions.
Common Shell Scripting Tasks
Here are six essential shell scripting tasks that are frequently asked in DevOps interviews:
Check Available Free Memory
Create a script to check the available free memory on the system and alert the user if it falls below a threshold (e.g., 10%).Automate User Creation
Write a script to automate the creation of a new user with specific permissions and a designated home directory.Find and Move Large Files
Develop a script to find all files larger than 1 GB in a directory and move them to another directory.Update Installed Packages
Create a script that automatically updates all installed packages on a system and reboots if necessary.Count Lines in Log Files
Write a script to count the number of lines in all.log
files in a specified directory.Check and Install Software
Write a script that checks for the presence of specific software (e.g., Docker) on the system and installs it if it is missing.
Detailed Solutions
1. Check Available Free Memory
To create a script that checks available memory, follow these steps:
Define the shebang line:
#!/bin/bash
Set a threshold for memory.
Use the
free
command to calculate total and available memory. For example:total_memory=$(free -m | awk 'NR==2{print $2}') available_memory=$(free -m | awk 'NR==2{print $7}') percent_free=$((available_memory * 100 / total_memory)) if [ $percent_free -lt 10 ]; then echo "Free memory is below threshold" else echo "Memory usage is safe" fi
2. Automate User Creation
To automate user creation:
Ensure the script runs as the root user.
Use
read
to get input for the username and home directory.Use
useradd
to create the user andpasswd
to set the password. Example:if [ $(id -u) -ne 0 ]; then echo "This script must be run as root" exit 1 fi read -p "Enter username: " username read -p "Enter home directory: " home_dir useradd -m -d $home_dir $username passwd $username
3. Find and Move Large Files
To find and move files larger than 1 GB:
Use the
find
command to locate files and move them accordingly. Example:find /path/to/directory -type f -size +1G -exec mv {} /path/to/destination/ \;
4. Update Installed Packages
To update packages and reboot if necessary:
Use
apt-get
for package management. Example:apt-get update && apt-get upgrade -y if [ $? -eq 0 ]; then reboot fi
5. Count Lines in Log Files
To count lines in .log
files:
Use
find
andwc
to count lines. Example:find /path/to/directory -name "*.log" -exec wc -l {} +
6. Check and Install Software
To check for software and install if missing:
Use
systemctl
to check the status of the service. Example:if ! systemctl is-active --quiet docker; then apt-get install docker -y systemctl start docker systemctl enable docker fi
Best Practices for Interviews
When discussing your scripts during an interview, it is essential to communicate your thought process clearly. Here are some tips:
Explain each line of your script as you write it.
Share your reasoning behind using specific commands or structures.
Maintain a dialogue with the interviewer to demonstrate your problem-solving skills.
By following these practices, you can leave a positive impression on your interviewer, showcasing not only your technical skills but also your ability to communicate effectively.
Conclusion
Shell scripting is a vital skill for anyone pursuing a career in DevOps. By preparing these common tasks and understanding their implementations, you can confidently demonstrate your capabilities in interviews. Remember to practice writing these scripts and explaining your thought process to enhance your interview performance.