0% found this document useful (0 votes)
62 views2 pages

Shell Scripting Tasks for AWS/DevOps

The document outlines shell scripting tasks and examples for AWS and DevOps engineers with around 4 years of experience. It includes categories such as basic, intermediate, AWS & DevOps-oriented, CI/CD and Jenkins related scripts, advanced scenario-based questions, and common troubleshooting tasks. Each category features specific scripting challenges aimed at testing automation, Linux, CI/CD, and AWS command-line skills.

Uploaded by

anushamk479
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views2 pages

Shell Scripting Tasks for AWS/DevOps

The document outlines shell scripting tasks and examples for AWS and DevOps engineers with around 4 years of experience. It includes categories such as basic, intermediate, AWS & DevOps-oriented, CI/CD and Jenkins related scripts, advanced scenario-based questions, and common troubleshooting tasks. Each category features specific scripting challenges aimed at testing automation, Linux, CI/CD, and AWS command-line skills.

Uploaded by

anushamk479
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Shell Script Interview Tasks & Examples for

AWS/DevOps Engineers (4 Years Experience)


Below are real-world shell scripting questions and examples that are commonly asked in AWS &
DevOps interviews for engineers with around 4 years of experience. These test your automation,
Linux, CI/CD, and AWS command-line skills.

1. Basic Shell Script Questions

• Write a script to print 'Hello World' and display system date and time.
• Write a script to check if a file exists and print an appropriate message.
• Write a script to display disk usage, memory usage, and CPU load.
• Write a script to count the number of files in a directory.

2. Intermediate Shell Script Tasks

• Write a script to monitor disk usage and send an email alert if usage exceeds 80%.
• Create a script to back up a directory to /backup/ with date-based naming.
• Write a script to list all running services and their status.
• Write a script to check if a process (e.g., nginx) is running and restart it if not.

3. AWS & DevOps-Oriented Scripts

• Write a script using AWS CLI to list all EC2 instances with their states.
• Write a script to start or stop an EC2 instance based on input (start/stop).
• Write a script to upload a file to an S3 bucket and verify the upload.
• Write a script to take a backup of an RDS database using AWS CLI.

4. CI/CD and Jenkins Related Scripts

• Write a script to deploy a Docker container from an image.


• Create a script to check Jenkins service status and restart if down.
• Write a script to clean up Docker images and containers older than 7 days.
• Write a script to pull latest code from GitHub and build it.

5. Advanced and Scenario-Based Questions

• Write a script to parse a log file and count occurrences of ERROR and WARNING.
• Write a script to automate package installation (e.g., git, docker, nginx).
• Write a script to schedule a daily backup job using cron.
• Write a script to verify network connectivity between two servers.

6. Common Troubleshooting Shell Tasks

• Check exit status of last executed command and print success/failure.


• Redirect both stdout and stderr to a log file.
• Use trap command to handle Ctrl+C interrupts gracefully.
• Write a script to retry a failed command 3 times before exiting.

Example Shell Script: System Health Check


#!/bin/bash echo "===== System Health Check =====" echo "Hostname: $(hostname)" echo
"Uptime: $(uptime)" echo "Memory Usage:" free -h echo "Disk Usage:" df -h echo "Top
5 Memory-Consuming Processes:" ps aux --sort=-%mem | head -n 6

Example Shell Script: AWS EC2 Instance Status


#!/bin/bash echo "Fetching EC2 Instances and their states..." aws ec2
describe-instances --query
"Reservations[*].Instances[*].{ID:InstanceId,State:[Link]}" --output table

Common questions

Powered by AI

Effective log file parsing in a shell script uses 'grep' to filter for patterns like 'ERROR' or 'WARNING', possibly piping through 'awk' or 'sed' for advanced processing or formatting. Timestamps can aid in pattern correlation over time. Integrating with tools like 'awk' enhances statistical analysis, while 'sed' offers in-place editing for real-time modifications. Such scripts can alert admins on identified patterns, aiding faster troubleshooting.

A script to back up an RDS database involves: 1) Using 'aws rds create-db-snapshot' command to initiate the snapshot process. 2) Incorporating '--db-instance-identifier' to select the target database. 3) Implementing error handling by checking command outputs and interpreting any errors. 4) Using confirmations like '--dry-run' for testing. This ensures data protection and reliability, aligning with best database management practices.

The 'trap' command in shell scripting is significant for capturing and handling signals and interrupts, like 'Ctrl+C'. It allows the script to execute cleanup actions or display messages before termination, ensuring proper closure of files or resources in use. This is crucial for scripts that modify system states, helping maintain data integrity and avoiding potential system inconsistencies.

A script to clean up Docker images and containers older than 7 days involves: 1) Using 'docker ps' and 'docker images' with '--filter' to list and identify candidates for removal based on age. 2) Executing 'docker rm' for containers and 'docker rmi' for images. 3) Confirming clean-up actions with appropriate conditions to avoid unintended deletions. Automated clean-up helps manage storage resources efficiently, promoting server health.

To deploy a Docker container, the script should first check if Docker is installed and running. Use 'docker images' to verify the required image is present, pulling it if necessary. The script executes 'docker run' with appropriate options (detached mode, port mappings). Error handling is done by checking the exit statuses of Docker commands and logging errors for unexpected outputs. This robust approach ensures the container is correctly deployed.

A script to automate directory backup with date-based naming involves: 1) Defining the source directory PATH. 2) Creating a backup destination PATH, such as '/backup/'. 3) Generating the current date string using the 'date' command, formatted as needed. 4) Using the 'tar' command, archive the directory with a name that includes the date string. This archive is saved to the backup destination, ensuring systematic organization of backups by date.

Verifying network connectivity is crucial for diagnosing communication issues that could disrupt services or affect performance. A shell script to accomplish this uses 'ping' to test basic connectivity and 'nc' (netcat) for more detailed port reachability. The script repeatedly checks connectivity, logs results, and can notify on failures. This proactive monitoring helps prevent network issues, ensuring seamless server interaction.

To create a shell script that monitors disk usage and alerts if usage exceeds 80%, follow these steps: 1) Use the 'df' command to get current disk usage statistics. 2) Parse the output to identify the usage percentage. 3) Use a conditional statement to check if the usage is greater than 80%. 4) If usage exceeds 80%, employ the 'mail' command to send an email alert. The script should be scheduled via cron for periodic execution to automate the monitoring process.

Retrying a failed command in a shell script enhances resilience against transient system errors, particularly in networked applications. Implementation involves a loop that attempts the command up to a set number of times, using 'exit status' to check for failure. With each failure, the script might introduce a delay (e.g., using 'sleep'), logging errors. Upon exceeding attempts, the script exits or escalates the issue, maintaining operational stability.

Key considerations include: 1) Ensuring AWS CLI is installed and configured with access keys and default region. 2) Understanding EC2 instance states and appropriate commands for stopping, starting, or listing instances. 3) Properly handling JSON responses with '--query' option for clean output. 4) Implementing error handling to manage AWS CLI command failures. These considerations ensure the script functions reliably and securely, conforming to AWS best practices.

You might also like