Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Lecture6/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## Bash scripts and Automation
1. Create bash with
2. backup a directory every day at 2 am.
2. Create cron job for the bash script.![SetUpCronJob.png](images/SetUpCronJob.png)
3. Create a bash script to monitor website health
4. Create cron job for the resource monitoring
5. Set up systemd timer to run a health-check script every 5 min.
6. Enable and start the timer.![SetHealthCheckTimer.png](images/SetHealthCheckTimer.png)
7. Log file for health check results.![HealthCheckLogFile.png](images/HealthCheckLogFile.png)
8. Create file rotation for acess_test.log. ![LogRotationConf.png](images/LogRotationConf.png)
9. Test force rotate logrotate -f /etc/logrotate.d/nginx-access-test
10. View rotated log files. ![RotatedLogFiles.png](images/RotatedLogFiles.png)
Binary file added Lecture6/images/LogRotationConf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lecture6/images/RotatedLogFiles.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lecture6/images/SetHealthCheckTimer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lecture6/images/SetUpCronJob.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions cron-backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
# A simple cron job script to back up a directory to a specified backup location everyday at 2 AM.
SOURCE_DIR="/media/sf_jenkins-project/gs-spring-boot"
BACKUP_DIR="/media/sf_jenkins-project/backups"

BACKUP_FILE="backup-$(date +'%Y-%m-%d').tar.gz"

if [ -f "$BACKUP_DIR/$BACKUP_FILE" ];then
echo "Backing file already exists for today. Skipping backup."
exit 0
else
echo "Start backing up $SOURCE_DIR to $BACKUP_DIR"
tar -czvf "$BACKUP_DIR/$BACKUP_FILE" -C "$SOURCE_DIR" .
fi


9 changes: 9 additions & 0 deletions health-check.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Unit]
Description=Check website availability and write result to a log file

[Service]
Type=oneshot
ExecStart=/media/sf_jenkins-project/gs-spring-boot/health-check.sh

[Install]
WantedBy=multi-user.target
24 changes: 24 additions & 0 deletions health-check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash
# A simple health check script to monitor system resources and log the status.
LOG_DIR="/media/sf_jenkins-project/gs-spring-boot/log"
LOG_FILE="/media/sf_jenkins-project/gs-spring-boot/log/system_health.log"
WEB_URL="http://localhost:8082"

# Create log directory if it doesn't exist
if [ ! -d "$LOG_DIR" ]; then
mkdir -p "$LOG_DIR"
fi

# Enable -e immediately exit in anny command with non-zero exit,
# -x print executed commands,
# -u send exception when use undefined variable,
# -o return exit status of last command in pipe that failed
set -exuo pipefail

if curl --request GET -fsSL --max-time 5 --url "$WEB_URL" >> "$LOG_FILE"; then

echo "$(date +'%Y-%m-%d %H:%M:%S') - Web service is up and running." >> "$LOG_FILE"
else
echo "failed to check $WEB_URL $(date +'%Y-%m-%d %H:%M:%S')" >> "$LOG_FILE"
fi

11 changes: 11 additions & 0 deletions health-check.timer
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Unit]
Description=Run health check script every 5 minutes

[Timer]
OnBootSec=5min
OnUnitActiveSec=5min
Unit=health-check.service

[Install]
WantedBy=timers.target

40 changes: 40 additions & 0 deletions resource-monitoring.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash
# A simple health check script to monitor system resources and log the status.
RESOURCE_LOG_DIR="/media/sf_jenkins-project/gs-spring-boot/resource-log"
RESOURCE_LOG_FILE="$RESOURCE_LOG_DIR/system_resource.log"

# Enable -e immediately exit in anny command with non-zero exit,
# -x print executed commands,
# -u send exception when use undefined variable,
# -o return exit status of last command in pipe that failed
set -exuo pipefail


CPU=$(top -bn1 | grep "Cpu(s)")
MEMORY=$(free -h | awk 'NR==2{printf "Used: %s / Total: %s (%.2f%%)\n", $3, $2, $3*100/$2 }')
DISK=$(df -h | awk 'NR=2{print $2, $5}')
UPTIME=$( uptime -p)
TOP_PROCESSORS=$( ps -eo pid,comm,%cpu --sort=-%cpu | head -n 6 )

LOG_TITLE="System Resource Status as of $(date +'%Y-%m-%d %H:%M:%S')"

if [ -f "$RESOURCE_LOG_FILE" ]; then
echo "Log file exists. Appending new resource status."
else
echo "Log file does not exist. Creating new log file."
mkdir -p "$RESOURCE_LOG_DIR"
touch "$RESOURCE_LOG_FILE"
fi

{
echo "=============================="
echo "$LOG_TITLE"
echo "=============================="
echo "CPU Usage: $CPU"
echo "Memory Usage: $MEMORY"
echo "Disk Usage: $DISK"
echo "System Uptime: $UPTIME"
echo "Top Processes by CPU Usage:"
echo "$TOP_PROCESSORS"
echo ""
} >> "$RESOURCE_LOG_FILE"
2 changes: 2 additions & 0 deletions test-crontab.cron
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Run script every day at 2 AM
0 2 * * * /media/sf_jenkins-project/gs-spring-boot/cron-backup.sh