From 1fd14f3871ea0b98bf8992397b39f8788e3d0eff Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 3 Apr 2026 22:29:49 +0200 Subject: [PATCH] feat: complete bash scripts --- scripts/backup.sh | 55 +++++++++++ .../backups/backup_2026-04-03_17-02-19.tar.gz | Bin 0 -> 113 bytes scripts/file1.txt | 0 scripts/file_1.txt | 0 scripts/file_manager.sh | 86 +++++++++++++++++ scripts/filenew.txt | 0 scripts/fille.txt | 0 scripts/fille_manager.sh | 25 +++++ scripts/logs/app.log | 11 +++ scripts/logs/file_manager.log | 8 ++ .../system_report_2026-03-30_22-26-54.log | 24 +++++ .../system_report_2026-03-30_22-32-31.log | 41 +++++++++ .../system_report_2026-04-03_15-40-51.log | 41 +++++++++ scripts/logs/user_info.log | 5 + scripts/logs/user_info.sh | 44 +++++++++ scripts/process_monitor.log | 61 ++++++++++++ scripts/process_monitor.sh | 56 +++++++++++ scripts/run_all.sh | 87 ++++++++++++++++++ scripts/system_check.sh | 60 ++++++++++++ scripts/user_info.sh | 44 +++++++++ 20 files changed, 648 insertions(+) create mode 100755 scripts/backup.sh create mode 100644 scripts/backups/backup_2026-04-03_17-02-19.tar.gz create mode 100644 scripts/file1.txt create mode 100644 scripts/file_1.txt create mode 100755 scripts/file_manager.sh create mode 100644 scripts/filenew.txt create mode 100644 scripts/fille.txt create mode 100755 scripts/fille_manager.sh create mode 100644 scripts/logs/app.log create mode 100644 scripts/logs/file_manager.log create mode 100644 scripts/logs/system_report_2026-03-30_22-26-54.log create mode 100644 scripts/logs/system_report_2026-03-30_22-32-31.log create mode 100644 scripts/logs/system_report_2026-04-03_15-40-51.log create mode 100644 scripts/logs/user_info.log create mode 100644 scripts/logs/user_info.sh create mode 100644 scripts/process_monitor.log create mode 100755 scripts/process_monitor.sh create mode 100755 scripts/run_all.sh create mode 100755 scripts/system_check.sh create mode 100755 scripts/user_info.sh diff --git a/scripts/backup.sh b/scripts/backup.sh new file mode 100755 index 0000000..0ba20cd --- /dev/null +++ b/scripts/backup.sh @@ -0,0 +1,55 @@ +#!/bin/bash +set -euo pipefail + +# ------------------------- +# Variables +# ------------------------- +BACKUP_DIR="backups" +LOG_FILE="backup.log" +TIMESTAMP=$(date +%F_%H-%M-%S) + +# ------------------------- +# Check input +# ------------------------- +if [ $# -ne 1 ]; then + echo "Usage: $0 " + exit 1 +fi + +SOURCE_DIR="$1" + +# ------------------------- +# Validate directory +# ------------------------- +if [ ! -d "$SOURCE_DIR" ]; then + echo "Error: Directory does not exist!" + exit 1 +fi + +# ------------------------- +# Create backup directory +# ------------------------- +mkdir -p "$BACKUP_DIR" + +# ------------------------- +# Create backup +# ------------------------- +BACKUP_FILE="$BACKUP_DIR/backup_$TIMESTAMP.tar.gz" + +tar -czf "$BACKUP_FILE" "$SOURCE_DIR" + +echo "$(date) - Backup created: $BACKUP_FILE" >> "$LOG_FILE" + +# ------------------------- +# Keep only last 5 backups +# ------------------------- +cd "$BACKUP_DIR" + +ls -t backup_*.tar.gz 2>/dev/null | tail -n +6 | while read -r old_backup; do + rm -f "$old_backup" + echo "$(date) - Deleted old backup: $old_backup" >> "../$LOG_FILE" +done + +cd - >/dev/null + +echo "Backup completed successfully!" diff --git a/scripts/backups/backup_2026-04-03_17-02-19.tar.gz b/scripts/backups/backup_2026-04-03_17-02-19.tar.gz new file mode 100644 index 0000000000000000000000000000000000000000..d4efc9e68d912239d9ecda7fc3f7007064bdfd4f GIT binary patch literal 113 zcmb2|=3oE==C|iI@*Xk}VF~De#QVc9rCN?zR>-O44Zq056$`koR-Y9uKPr3t<)(Yn zoNop0`4V+Mva0v|oh3ouv#Vz>n-M%EBQR)P^}JhaPAA_f_IqCVch~Q$%n*|q7!JtW OGVZ&txq(50fdK$t%`eUX literal 0 HcmV?d00001 diff --git a/scripts/file1.txt b/scripts/file1.txt new file mode 100644 index 0000000..e69de29 diff --git a/scripts/file_1.txt b/scripts/file_1.txt new file mode 100644 index 0000000..e69de29 diff --git a/scripts/file_manager.sh b/scripts/file_manager.sh new file mode 100755 index 0000000..9ed5da9 --- /dev/null +++ b/scripts/file_manager.sh @@ -0,0 +1,86 @@ +#!/bin/bash + +LOG_FILE="logs/file_manager.log" + +# Ensure logs directory exists +mkdir -p logs + +# Function to log actions +log_action() { + local message="$1" + echo "$(date) - $message" >> "$LOG_FILE" +} + +# Check for at least 2 arguments +if [ $# -lt 2 ]; then + echo "Usage: $0 {create|delete|list|rename} [newname]" + exit 1 +fi + +COMMAND=$1 +FILE=$2 + +case $COMMAND in + create) + # Auto-rename if file exists + base="$FILE" + ext="" + if [[ "$FILE" == *.* ]]; then + base="${FILE%.*}" + ext=".${FILE##*.}" + fi + + counter=1 + newfile="$FILE" + while [ -e "$newfile" ]; do + newfile="${base}_$counter$ext" + ((counter++)) + done + + touch "$newfile" + echo "File created: $newfile" + log_action "CREATE: $newfile" + ;; + + delete) + if [ ! -e "$FILE" ]; then + echo "Error: $FILE does not exist." + log_action "DELETE FAILED: $FILE does not exist" + else + rm -i "$FILE" + echo "File deleted: $FILE" + log_action "DELETE: $FILE" + fi + ;; + + list) + echo "Files in current directory:" + ls -lah + log_action "LIST directory" + ;; + + rename) + NEW_NAME=$3 + if [ -z "$NEW_NAME" ]; then + echo "Error: You must provide a new name for the file." + exit 1 + fi + if [ ! -e "$FILE" ]; then + echo "Error: $FILE does not exist." + log_action "RENAME FAILED: $FILE does not exist" + elif [ -e "$NEW_NAME" ]; then + echo "Error: $NEW_NAME already exists. Aborting." + log_action "RENAME FAILED: $NEW_NAME already exists" + else + mv "$FILE" "$NEW_NAME" + echo "File renamed from $FILE to $NEW_NAME" + log_action "RENAME: $FILE -> $NEW_NAME" + fi + ;; + + *) + echo "Unknown command: $COMMAND" + echo "Usage: $0 {create|delete|list|rename} [newname]" + exit 1 + ;; +esac diff --git a/scripts/filenew.txt b/scripts/filenew.txt new file mode 100644 index 0000000..e69de29 diff --git a/scripts/fille.txt b/scripts/fille.txt new file mode 100644 index 0000000..e69de29 diff --git a/scripts/fille_manager.sh b/scripts/fille_manager.sh new file mode 100755 index 0000000..2d5a155 --- /dev/null +++ b/scripts/fille_manager.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +echo "Before creating folder_1 and file_1" +ls + +# to create a directory and file +mkdir folder_1 +touch file_1 + +echo "After creating folder_1 and file_1" +ls + +# to rename directory and file +mv folder_1 tsfolder +mv file_1 tsfile + +echo "After renaming folder_1 and file_1" +ls + + +#deleting dir and file +rm -rf tsfolder +rm -f tsfile +echo "After removing tsfolder and tsfile" +ls diff --git a/scripts/logs/app.log b/scripts/logs/app.log new file mode 100644 index 0000000..87fb4ee --- /dev/null +++ b/scripts/logs/app.log @@ -0,0 +1,11 @@ +2026-04-03 18:39:03 - Running all scripts... +2026-04-03 18:39:03 - Running system check... +2026-04-03 18:39:03 - process_monitor.sh not found or not executable +2026-04-03 18:39:03 - Running backup... +2026-04-03 18:39:14 - backup.sh not found or not executable +2026-04-03 18:39:14 - All tasks completed +2026-04-03 18:39:18 - Running system check... +2026-04-03 18:39:18 - process_monitor.sh not found or not executable +2026-04-03 18:39:32 - Running backup... +2026-04-03 18:39:39 - backup.sh not found or not executable +2026-04-03 18:39:44 - Exiting application diff --git a/scripts/logs/file_manager.log b/scripts/logs/file_manager.log new file mode 100644 index 0000000..36e2ad0 --- /dev/null +++ b/scripts/logs/file_manager.log @@ -0,0 +1,8 @@ +Mon Mar 30 21:58:24 WAT 2026 - CREATE: file_1.txt +Mon Mar 30 21:59:26 WAT 2026 - RENAME: file.txt -> filenew.txt +Mon Mar 30 22:00:08 WAT 2026 - DELETE FAILED: file.txt does not exist +Mon Mar 30 22:00:47 WAT 2026 - CREATE: file1.txt +Fri Apr 3 15:57:21 WAT 2026 - CREATE: file.txt +Fri Apr 3 15:58:37 WAT 2026 - RENAME: file.txt -> fille.txt +Fri Apr 3 16:32:20 WAT 2026 - CREATE: file_2.txt +Fri Apr 3 16:32:51 WAT 2026 - DELETE: file_2.txt diff --git a/scripts/logs/system_report_2026-03-30_22-26-54.log b/scripts/logs/system_report_2026-03-30_22-26-54.log new file mode 100644 index 0000000..50dbe26 --- /dev/null +++ b/scripts/logs/system_report_2026-03-30_22-26-54.log @@ -0,0 +1,24 @@ +Filesystem Size Used Avail Use% Mounted on +none 1.9G 0 1.9G 0% /usr/lib/modules/6.6.87.2-microsoft-standard-WSL2 +none 1.9G 4.0K 1.9G 1% /mnt/wsl +drivers 476G 113G 364G 24% /usr/lib/wsl/drivers +/dev/sdd 1007G 1.4G 955G 1% / +none 1.9G 308K 1.9G 1% /mnt/wslg +none 1.9G 0 1.9G 0% /usr/lib/wsl/lib +rootfs 1.9G 2.7M 1.9G 1% /init +none 1.9G 516K 1.9G 1% /run +none 1.9G 0 1.9G 0% /run/lock +none 1.9G 0 1.9G 0% /run/shm +none 1.9G 76K 1.9G 1% /mnt/wslg/versions.txt +none 1.9G 76K 1.9G 1% /mnt/wslg/doc +C:\ 476G 113G 364G 24% /mnt/c +tmpfs 378M 20K 378M 1% /run/user/1000 + + total used free shared buff/cache available +Mem: 3771 413 3166 3 268 3357 +Swap: 1024 0 1024 + + 22:26:54 up 5:38, 1 user, load average: 0.00, 0.00, 0.00 + +=== Disk Usage Warnings === + diff --git a/scripts/logs/system_report_2026-03-30_22-32-31.log b/scripts/logs/system_report_2026-03-30_22-32-31.log new file mode 100644 index 0000000..9b46e4c --- /dev/null +++ b/scripts/logs/system_report_2026-03-30_22-32-31.log @@ -0,0 +1,41 @@ +=== System Check Report === +Date: Mon Mar 30 22:32:31 CEST 2026 + +=== Disk Usage === +Filesystem Size Used Avail Use% Mounted on +none 1.9G 0 1.9G 0% /usr/lib/modules/6.6.87.2-microsoft-standard-WSL2 +none 1.9G 4.0K 1.9G 1% /mnt/wsl +drivers 476G 113G 363G 24% /usr/lib/wsl/drivers +/dev/sdd 1007G 1.4G 955G 1% / +none 1.9G 308K 1.9G 1% /mnt/wslg +none 1.9G 0 1.9G 0% /usr/lib/wsl/lib +rootfs 1.9G 2.7M 1.9G 1% /init +none 1.9G 520K 1.9G 1% /run +none 1.9G 0 1.9G 0% /run/lock +none 1.9G 0 1.9G 0% /run/shm +none 1.9G 76K 1.9G 1% /mnt/wslg/versions.txt +none 1.9G 76K 1.9G 1% /mnt/wslg/doc +C:\ 476G 113G 363G 24% /mnt/c +tmpfs 378M 20K 378M 1% /run/user/1000 + +=== Disk Usage Warnings (>80%) === + +=== Memory Usage (MB) === + total used free shared buff/cache available +Mem: 3771 416 3162 3 269 3354 +Swap: 1024 0 1024 + +=== CPU Load === + 22:32:31 up 5:44, 1 user, load average: 0.00, 0.00, 0.00 + +=== Total Running Processes === +Total processes: 33 + +=== Top 5 Memory-Consuming Processes === + PID PPID CMD %MEM %CPU + 206 1 /usr/bin/python3 /usr/share 0.5 0.0 + 42 1 /usr/lib/systemd/systemd-jo 0.3 0.0 + 2281 1 /usr/libexec/wsl-pro-servic 0.3 0.1 + 1 0 /sbin/init 0.3 0.0 + 104 1 /usr/lib/systemd/systemd-re 0.3 0.0 + diff --git a/scripts/logs/system_report_2026-04-03_15-40-51.log b/scripts/logs/system_report_2026-04-03_15-40-51.log new file mode 100644 index 0000000..51e1a3a --- /dev/null +++ b/scripts/logs/system_report_2026-04-03_15-40-51.log @@ -0,0 +1,41 @@ +=== System Check Report === +Date: Fri Apr 3 15:40:51 WAT 2026 + +=== Disk Usage === +Filesystem Size Used Avail Use% Mounted on +none 1.9G 0 1.9G 0% /usr/lib/modules/6.6.87.2-microsoft-standard-WSL2 +none 1.9G 4.0K 1.9G 1% /mnt/wsl +drivers 476G 114G 363G 24% /usr/lib/wsl/drivers +/dev/sdd 1007G 1.4G 955G 1% / +none 1.9G 372K 1.9G 1% /mnt/wslg +none 1.9G 0 1.9G 0% /usr/lib/wsl/lib +rootfs 1.9G 2.7M 1.9G 1% /init +none 1.9G 520K 1.9G 1% /run +none 1.9G 0 1.9G 0% /run/lock +none 1.9G 0 1.9G 0% /run/shm +none 1.9G 76K 1.9G 1% /mnt/wslg/versions.txt +none 1.9G 76K 1.9G 1% /mnt/wslg/doc +C:\ 476G 114G 363G 24% /mnt/c +tmpfs 378M 20K 378M 1% /run/user/1000 + +=== Disk Usage Warnings (>80%) === + +=== Memory Usage (MB) === + total used free shared buff/cache available +Mem: 3771 415 3233 3 199 3355 +Swap: 1024 0 1024 + +=== CPU Load === + 15:40:51 up 7:30, 1 user, load average: 0.00, 0.00, 0.00 + +=== Total Running Processes === +Total processes: 35 + +=== Top 5 Memory-Consuming Processes === + PID PPID CMD %MEM %CPU + 206 1 /usr/bin/python3 /usr/share 0.5 0.0 + 42 1 /usr/lib/systemd/systemd-jo 0.3 0.0 + 1 0 /sbin/init 0.3 0.0 + 104 1 /usr/lib/systemd/systemd-re 0.3 0.0 + 345 1 /usr/lib/systemd/systemd -- 0.2 0.0 + diff --git a/scripts/logs/user_info.log b/scripts/logs/user_info.log new file mode 100644 index 0000000..610b3db --- /dev/null +++ b/scripts/logs/user_info.log @@ -0,0 +1,5 @@ +Mon Mar 30 20:49:55 WAT 2026 - Name: Daniel | Age: 45 | Country: Nigeria | Category: Adult +Fri Apr 3 15:23:12 WAT 2026 - Name: Daniel | Age: 3 | Country: Nigeria | Category: Minor +Fri Apr 3 15:24:46 WAT 2026 - Name: Aji | Age: 45 | Country: South Africa | Category: Adult +Fri Apr 3 15:27:05 WAT 2026 - Name: Aji | Age: 46 | Country: South Korea | Category: Adult +Fri Apr 3 15:31:09 WAT 2026 - Name: Aji | Age: 47 | Country: America | Category: Adult diff --git a/scripts/logs/user_info.sh b/scripts/logs/user_info.sh new file mode 100644 index 0000000..a7adeb8 --- /dev/null +++ b/scripts/logs/user_info.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +LOG_FILE="logs/user_info.log" + +# Ensure logs directory exists +mkdir -p logs + +echo "=== User Information Script ===" + +# Prompt user for input +read -p "Enter your name: " name +read -p "Enter your age: " age +read -p "Enter your country: " country + +# Validate inputs +if [[ -z "$name" || -z "$age" || -z "$country" ]]; then + echo "Error: All fields are required." + echo "$(date) - ERROR: Missing input" >> "$LOG_FILE" + exit 1 +fi + +# Check if age is numeric +if ! [[ "$age" =~ ^[0-9]+$ ]]; then + echo "Error: Age must be a number." + echo "$(date) - ERROR: Invalid age input ($age)" >> "$LOG_FILE" + exit 1 +fi + +# Determine age category +if (( age < 18 )); then + category="Minor" +elif (( age <= 65 )); then + category="Adult" +else + category="Senior" +fi + +# Output message +message="Hello $name from $country! You are an $category." + +echo "$message" + +# Save to log file +echo "$(date) - Name: $name | Age: $age | Country: $country | Category: $category" >> "$LOG_FILE" diff --git a/scripts/process_monitor.log b/scripts/process_monitor.log new file mode 100644 index 0000000..7ac8cf3 --- /dev/null +++ b/scripts/process_monitor.log @@ -0,0 +1,61 @@ +Fri Apr 3 17:10:36 WAT 2026 - nginx: Stopped +Fri Apr 3 17:10:45 WAT 2026 - nginx: Failed to restart +Fri Apr 3 17:10:45 WAT 2026 - ssh: Stopped +Fri Apr 3 17:10:45 WAT 2026 - ssh: Failed to restart +Fri Apr 3 17:10:45 WAT 2026 - docker: Stopped +Fri Apr 3 17:10:45 WAT 2026 - docker: Failed to restart +Fri Apr 3 17:11:13 WAT 2026 - nginx: Stopped +Fri Apr 3 17:11:13 WAT 2026 - nginx: Failed to restart +Fri Apr 3 17:11:39 WAT 2026 - nginx: Stopped +Fri Apr 3 17:11:39 WAT 2026 - nginx: Failed to restart +Fri Apr 3 17:11:39 WAT 2026 - ssh: Stopped +Fri Apr 3 17:11:39 WAT 2026 - ssh: Failed to restart +Fri Apr 3 17:11:39 WAT 2026 - docker: Stopped +Fri Apr 3 17:11:39 WAT 2026 - docker: Failed to restart +Fri Apr 3 17:14:43 WAT 2026 - nginx: Stopped +Fri Apr 3 17:14:50 WAT 2026 - nginx: Failed to restart +Fri Apr 3 17:14:51 WAT 2026 - ssh: Stopped +Fri Apr 3 17:14:51 WAT 2026 - ssh: Failed to restart +Fri Apr 3 17:14:51 WAT 2026 - docker: Stopped +Fri Apr 3 17:14:51 WAT 2026 - docker: Failed to restart +Fri Apr 3 17:21:51 WAT 2026 - nginx: Running +Fri Apr 3 17:21:51 WAT 2026 - ssh: Stopped +Fri Apr 3 17:21:51 WAT 2026 - ssh: Failed to restart +Fri Apr 3 17:21:51 WAT 2026 - docker: Stopped +Fri Apr 3 17:21:51 WAT 2026 - docker: Failed to restart +Fri Apr 3 17:39:59 WAT 2026 - nginx: Running +Fri Apr 3 17:39:59 WAT 2026 - ssh: Stopped +Fri Apr 3 17:39:59 WAT 2026 - ssh: Failed to restart +Fri Apr 3 17:39:59 WAT 2026 - docker: Stopped +Fri Apr 3 17:40:01 WAT 2026 - docker: Failed to restart +Fri Apr 3 17:41:25 WAT 2026 - nginx: Running +Fri Apr 3 17:41:25 WAT 2026 - ssh: Stopped +Fri Apr 3 17:41:25 WAT 2026 - ssh: Failed to restart +Fri Apr 3 17:41:25 WAT 2026 - docker: Stopped +Fri Apr 3 17:41:27 WAT 2026 - docker: Failed to restart +Fri Apr 3 17:43:35 WAT 2026 - nginx: Running +Fri Apr 3 17:43:35 WAT 2026 - ssh: Stopped +Fri Apr 3 17:43:36 WAT 2026 - ssh: Failed to restart +Fri Apr 3 17:43:36 WAT 2026 - docker: Stopped +Fri Apr 3 17:43:37 WAT 2026 - docker: Failed to restart +Fri Apr 3 17:48:11 WAT 2026 - nginx: Running +Fri Apr 3 17:48:11 WAT 2026 - ssh: Stopped +Fri Apr 3 17:48:11 WAT 2026 - ssh: Failed to restart +Fri Apr 3 17:48:11 WAT 2026 - docker: Stopped +Fri Apr 3 17:48:13 WAT 2026 - docker: Failed to restart +Fri Apr 3 17:53:49 WAT 2026 - nginx: Running +Fri Apr 3 17:53:49 WAT 2026 - sshd: Running +Fri Apr 3 17:53:49 WAT 2026 - docker: Stopped +Fri Apr 3 17:53:51 WAT 2026 - docker: Failed to restart +Fri Apr 3 17:57:10 WAT 2026 - nginx: Running +Fri Apr 3 17:57:10 WAT 2026 - sshd: Running +Fri Apr 3 17:57:10 WAT 2026 - docker: Stopped +Fri Apr 3 17:57:11 WAT 2026 - docker: Failed to restart +Fri Apr 3 18:05:23 WAT 2026 - nginx: Running +Fri Apr 3 18:05:23 WAT 2026 - sshd: Running +Fri Apr 3 18:05:23 WAT 2026 - dockered: Stopped +Fri Apr 3 18:05:23 WAT 2026 - dockered: Failed to restart +Fri Apr 3 18:30:05 WAT 2026 - nginx: Running +Fri Apr 3 18:30:05 WAT 2026 - sshd: Running +Fri Apr 3 18:30:05 WAT 2026 - dockered: Stopped +Fri Apr 3 18:30:05 WAT 2026 - dockered: Failed to restart diff --git a/scripts/process_monitor.sh b/scripts/process_monitor.sh new file mode 100755 index 0000000..b4a1f5f --- /dev/null +++ b/scripts/process_monitor.sh @@ -0,0 +1,56 @@ +#!/bin/bash +set -euo pipefail + +LOG_FILE="process_monitor.log" + +# ------------------------- +# Default services array +# ------------------------- +services=("nginx" "sshd" "dockered") + +# ------------------------- +# Accept process name input +# ------------------------- +if [ $# -ge 1 ]; then + services=("$@") # override with user input +fi + +# ------------------------- +# Function to check process +# ------------------------- +check_process() { + local service="$1" + + if pgrep -x "$service" >/dev/null 2>&1; then + echo "$service: Running" + echo "$(date) - $service: Running" >> "$LOG_FILE" + else + echo "$service: Stopped" + echo "$(date) - $service: Stopped" >> "$LOG_FILE" + + # Attempt restart (simulate or real) + echo "Attempting to restart $service..." + + if command -v systemctl >/dev/null 2>&1; then + sudo systemctl restart "$service" 2>/dev/null || true + else + echo "(Simulated restart for $service)" + fi + + # Check again + if pgrep -x "$service" >/dev/null 2>&1; then + echo "$service: Restarted" + echo "$(date) - $service: Restarted" >> "$LOG_FILE" + else + echo "$service: Failed to restart" + echo "$(date) - $service: Failed to restart" >> "$LOG_FILE" + fi + fi +} + +# ------------------------- +# Loop through services +# ------------------------- +for svc in "${services[@]}"; do + check_process "$svc" +done diff --git a/scripts/run_all.sh b/scripts/run_all.sh new file mode 100755 index 0000000..69bd614 --- /dev/null +++ b/scripts/run_all.sh @@ -0,0 +1,87 @@ +#!/bin/bash + +set -euo pipefail + +LOG_DIR="logs" +LOG_FILE="$LOG_DIR/app.log" +SCRIPTS_DIR="./scripts" + +mkdir -p "$LOG_DIR" + +# ------------------------- +# Logging function +# ------------------------- +log() { + echo "$(date '+%F %T') - $1" | tee -a "$LOG_FILE" +} + +# ------------------------- +# Error handler +# ------------------------- +handle_error() { + log "ERROR: Script failed at line $1" +} +trap 'handle_error $LINENO' ERR + +# ------------------------- +# Functions +# ------------------------- + +run_all() { + log "Running all scripts..." + + run_system_check + run_backup + + log "All tasks completed" +} + +run_system_check() { + log "Running system check..." + + if [[ -x "$SCRIPTS_DIR/process_monitor.sh" ]]; then + "$SCRIPTS_DIR/process_monitor.sh" nginx ssh docker \ + >> "$LOG_FILE" 2>&1 || log "process_monitor failed" + else + log "process_monitor.sh not found or not executable" + fi +} + +run_backup() { + log "Running backup..." + + read -p "Enter directory to backup: " dir + + if [[ -x "$SCRIPTS_DIR/backup.sh" ]]; then + "$SCRIPTS_DIR/backup.sh" "$dir" \ + >> "$LOG_FILE" 2>&1 || log "backup failed" + else + log "backup.sh not found or not executable" + fi +} + +# ------------------------- +# Menu +# ------------------------- +PS3="Choose an option: " + +select option in "Run All" "System Check" "Backup" "Exit"; do + case $REPLY in + 1) + run_all + ;; + 2) + run_system_check + ;; + 3) + run_backup + ;; + 4) + log "Exiting application" + break + ;; + *) + echo "Invalid option" + ;; + esac +done diff --git a/scripts/system_check.sh b/scripts/system_check.sh new file mode 100755 index 0000000..2cadf64 --- /dev/null +++ b/scripts/system_check.sh @@ -0,0 +1,60 @@ +#!/bin/bash + +# Create logs directory if it doesn't exist +mkdir -p logs + +DATE=$(date +%Y-%m-%d_%H-%M-%S) +LOG_FILE="logs/system_report_$DATE.log" + +echo "=== System Check Report ===" | tee -a "$LOG_FILE" +echo "Date: $(date)" | tee -a "$LOG_FILE" +echo "" | tee -a "$LOG_FILE" + +# Disk Usage +echo "=== Disk Usage ===" | tee -a "$LOG_FILE" +df -h | tee -a "$LOG_FILE" + +echo "" | tee -a "$LOG_FILE" + +# Warn if disk usage exceeds 80% +echo "=== Disk Usage Warnings (>80%) ===" | tee -a "$LOG_FILE" + +df -h | awk 'NR>1 {print $5 " " $1}' | while read output; +do + usage=$(echo $output | awk '{print $1}' | sed 's/%//g') + partition=$(echo $output | awk '{print $2}') + + if [ "$usage" -gt 80 ]; then + warning="WARNING: $partition is at ${usage}% usage!" + echo "$warning" | tee -a "$LOG_FILE" + fi +done + +echo "" | tee -a "$LOG_FILE" + +# Memory Usage +echo "=== Memory Usage (MB) ===" | tee -a "$LOG_FILE" +free -m | tee -a "$LOG_FILE" + +echo "" | tee -a "$LOG_FILE" + +# CPU Load +echo "=== CPU Load ===" | tee -a "$LOG_FILE" +uptime | tee -a "$LOG_FILE" + +echo "" | tee -a "$LOG_FILE" + +# Total running processes +echo "=== Total Running Processes ===" | tee -a "$LOG_FILE" +process_count=$(ps -e --no-headers | wc -l) +echo "Total processes: $process_count" | tee -a "$LOG_FILE" + +echo "" | tee -a "$LOG_FILE" + +# Top 5 memory-consuming processes +echo "=== Top 5 Memory-Consuming Processes ===" | tee -a "$LOG_FILE" +ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head -n 6 | tee -a "$LOG_FILE" + +echo "" | tee -a "$LOG_FILE" + +echo "Report saved to $LOG_FILE" diff --git a/scripts/user_info.sh b/scripts/user_info.sh new file mode 100755 index 0000000..da19642 --- /dev/null +++ b/scripts/user_info.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +LOG_FILE="logs/user_info.log" + +# Ensure logs directory exists +mkdir -p logs + +echo "=== User Information Script ===" + +# Prompt user for input +read -p "Enter your name: " name +read -p "Enter your age: " age +read -p "Enter your country: " country + +# Validate inputs +if [[ -z "$name" || -z "$age" || -z "$country" ]]; then + echo "Error: All fields are required." + echo "$(date) - ERROR: Missing input" >> "$LOG_FILE" + exit 1 +fi + +# Check if age is numeric +if ! [[ "$age" =~ ^[0-9]+$ ]]; then + echo "Error: Age must be a number." + echo "$(date) - ERROR: Invalid age input ($age)" >> "$LOG_FILE" + exit 1 +fi + +# Determine age category +if (( age < 18 )); then + category="Minor" +elif (( age <= 65 )); then + category="Adult" +else + category="Senior" +fi + +# Output message +message="Hello $name from $country! This age shows you are an $category." + +echo "$message" + +# Save to log file +echo "$(date) - Name: $name | Age: $age | Country: $country | Category: $category" >> "$LOG_FILE"