You can use a shell script to check the status of a service. Here's an example to check if nginx
is running:
#!/bin/bash
SERVICE="nginx"
if systemctl is-active --quiet $SERVICE; then
echo "$SERVICE is running."
else
echo "$SERVICE is not running."
fi
This script monitors disk usage and sends an alert if any partition exceeds 80%.
#!/bin/bash
THRESHOLD=80
df -h | awk 'NR>1 {if ($5+0 > ENVIRON["THRESHOLD"]) print $0}' | while read line; do
echo "Alert: Disk usage exceeded threshold: $line"
done
Using Python and the requests
library, you can check the status of a web application.
import requests
url = "http://example.com"
try:
response = requests.get(url)
if response.status_code == 200:
print(f"Website {url} is up.")
else:
print(f"Website {url} is down. Status code: {response.status_code}")
except requests.exceptions.RequestException as e:
print(f"Error connecting to {url}: {e}")
This Dockerfile sets up a Flask web application.
# Use the official Python image
FROM python:3.9-slim
# Set the working directory
WORKDIR /app
# Copy the application files
COPY . /app
# Install dependencies
RUN pip install -r requirements.txt
# Expose the application port
EXPOSE 5000
# Run the application
CMD ["python", "app.py"]
Example app.py
:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello, DevOps!"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
Here’s an example of a shell script to back up a MySQL database:
#!/bin/bash
DB_NAME="my_database"
DB_USER="root"
DB_PASS="password"
BACKUP_DIR="/backup"
DATE=$(date +%F)
mkdir -p $BACKUP_DIR
mysqldump -u $DB_USER -p$DB_PASS $DB_NAME > $BACKUP_DIR/$DB_NAME-$DATE.sql
if [ $? -eq 0 ]; then
echo "Backup successful: $BACKUP_DIR/$DB_NAME-$DATE.sql"
else
echo "Backup failed!"
fi
This script continuously monitors and restarts a service if it stops running.
#!/bin/bash
SERVICE="nginx"
while true; do
if ! systemctl is-active --quiet $SERVICE; then
echo "$SERVICE is down. Restarting..."
systemctl restart $SERVICE
fi
sleep 30
done
This Ansible playbook installs Apache and ensures it’s running.
---
- name: Install and start Apache
hosts: all
become: yes
tasks:
- name: Install Apache
apt:
name: apache2
state: present
update_cache: yes
- name: Start Apache service
service:
name: apache2
state: started
enabled: yes
This Jenkinsfile defines a pipeline to build and deploy a Java app.
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building the application...'
sh './gradlew build'
}
}
stage('Test') {
steps {
echo 'Running tests...'
sh './gradlew test'
}
}
stage('Deploy') {
steps {
echo 'Deploying the application...'
sh 'scp build/libs/app.jar user@server:/deployments'
}
}
}
}
This Terraform configuration creates an AWS EC2 instance.
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0" # Replace with a valid AMI
instance_type = "t2.micro"
tags = {
Name = "DevOps-Instance"
}
}
This script uploads a file to an S3 bucket.
import boto3
s3 = boto3.client('s3')
bucket_name = "my-bucket"
file_name = "file.txt"
try:
s3.upload_file(file_name, bucket_name, file_name)
print(f"{file_name} uploaded to {bucket_name}.")
except Exception as e:
print(f"Error: {e}")