Panda Guru LogoPanda
Guru

coding-related questions commonly asked in DevOps interviews

Round 1

Questions:

  1. Write a Bash script to check if a service is running.

    service_name="nginx" if systemctl is-active --quiet $service_name; then echo "$service_name is running" else echo "$service_name is not running" fi
  2. Write a script to automate log file rotation.

    • Use logrotate or write a script to move files and compress older logs using gzip.
  3. Write a Python script to monitor CPU usage.

    import psutil print(f"CPU Usage: {psutil.cpu_percent(interval=1)}%")
  4. How do you parse a log file to extract errors using Python?

    with open("logfile.log", "r") as file: errors = [line for line in file if "ERROR" in line] print(errors)

Round 2

Questions: 5. Write a Git command to undo the last commit but keep the changes in the working directory.

git reset --soft HEAD~1
  1. Write a script to automate Git pull for multiple repositories.
    for dir in /path/to/repos/*; do if [ -d "$dir/.git" ]; then echo "Updating $dir..." cd "$dir" && git pull fi done

Round 3

Questions: 7. Write a Dockerfile to set up a Python application.

FROM python:3.9-slim WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY . . CMD ["python", "app.py"]
  1. How do you remove all stopped containers and unused images in Docker using a single command?

    docker system prune -a
  2. Write a script to restart a Docker container if it stops.

    docker run -d --restart unless-stopped container_name

Round 4

Questions: 10. Write a Kubernetes YAML file to deploy an Nginx application. yaml apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80

  1. How do you scale a deployment to 5 replicas in Kubernetes?

    kubectl scale deployment nginx-deployment --replicas=5
  2. Write a command to fetch logs from a specific pod in Kubernetes.

    kubectl logs pod-name

Round 5

Questions: 13. Write a Jenkins pipeline script to build and deploy a Java application. groovy pipeline { agent any stages { stage('Build') { steps { sh './gradlew build' } } stage('Deploy') { steps { sh 'scp build/libs/app.jar user@server:/path/to/deploy' } } } }

  1. How do you write a GitHub Actions workflow to build a Node.js project?
    name: Node.js CI on: [push] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Install dependencies run: npm install - name: Run tests run: npm test

Round 6

Questions: 15. Write a script to fetch system memory usage on Linux. bash free -h

  1. How do you monitor a log file in real-time?

    tail -f logfile.log
  2. Write a Python script to send an alert if disk usage exceeds 80%.

    import shutil total, used, free = shutil.disk_usage("/") if (used / total) * 100 > 80: print("Disk usage exceeds 80%!")

Round 7

Questions: 18. Write a Terraform script to create an EC2 instance on AWS. hcl provider "aws" { region = "us-west-2" } resource "aws_instance" "example" { ami = "ami-12345678" instance_type = "t2.micro" }

  1. How do you initialize a Terraform project?

    terraform init
  2. How do you check what changes Terraform will make without applying them?

    terraform plan

Round 8

Questions: 21. Write an Ansible playbook to install Nginx on a server. yaml - name: Install Nginx hosts: all become: true tasks: - name: Install Nginx apt: name: nginx state: present

  1. How do you run an Ansible playbook?

    ansible-playbook playbook.yml -i inventory
  2. Write an Ansible ad-hoc command to check uptime of all servers.

    ansible all -m command -a "uptime"

Round 9

Questions: 24. Write a script to monitor if a website is up. bash if curl -s --head http://example.com | grep "200 OK" > /dev/null; then echo "Website is up" else echo "Website is down" fi

  1. Write a script to delete files older than 30 days.

    find /path/to/files -type f -mtime +30 -delete
  2. How do you compress a directory using tar?

    tar -czvf archive.tar.gz /path/to/directory

Round 10

Questions: 27. Write a command to check the status of all services on Linux. bash systemctl list-units --type=service

  1. Write a cron job to back up a directory daily at midnight.

    0 0 * * * tar -czvf backup.tar.gz /path/to/directory
  2. How do you test the availability of a port?

    nc -zv hostname port
  3. Write a script to restart a service if it is not active.

    service_name="nginx" if ! systemctl is-active --quiet $service_name; then systemctl restart $service_name fi

image