# Shell Script Documentation – Delete Old `.log` Files

## Overview

This shell script is used to automatically delete `.log` files older than **15 days** from the `sslog` directory.  
The script is intended to run daily using a Linux cron job to help manage disk space and maintain server cleanliness.

---

# Script Name

`delete_sslog_files.sh`

---

# Purpose

The script performs the following tasks:

- Sets the system execution path
- Creates execution logs
- Deletes `.log` files older than 15 days
- Records execution start and completion timestamps

---

# Script Details

## 1. Define Shell Interpreter

```bash
#!/bin/bash
```

This line specifies that the script should run using the Bash shell.

---

## 2. Set System PATH

```bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
```

Defines executable paths to ensure Linux commands like `find`, `date`, and `echo` work properly when executed via cron.

---

## 3. Log File Configuration

```bash
LOG_FILE="/home/ubuntu/delete_sslog_files.log"
exec >> "$LOG_FILE" 2>&1
```

### Purpose

- Stores all script execution logs in:
`/home/ubuntu/delete_sslog_files.log`
- Redirects both:
  - Standard Output (`stdout`)
  - Error Output (`stderr`)

into the same log file.

---

## 4. Execution Start Log

```bash
echo "========================================="
echo "Delete sslog files started at $(date)"
echo "========================================="
```

Adds execution start information to the log file with the current date and time.

---

## 5. Backup Directory Configuration

```bash
BACKUP_DIR="/var/www/html/sslog"
```

Defines the directory where `.log` files are stored.

---

## 6. Delete Old Log Files

```bash
find "$BACKUP_DIR" -type f -name "*.log" -mtime +15 -delete
```

### Explanation


| Option          | Description                      |
| --------------- | -------------------------------- |
| `find`          | Searches files in the directory  |
| `"$BACKUP_DIR"` | Target directory                 |
| `-type f`       | Search only files                |
| `-name "*.log"` | Match `.log` files only          |
| `-mtime +15`     | Files older than 15 days          |
| `-delete`       | Permanently delete matched files |


---

## 7. Completion Message

```bash
echo "sslog files cleanup completed successfully at $(date)"
exit 0
```

Logs successful completion with timestamp and exits the script.

---

# Cron Job Setup

## Make Script Executable

Run the following command:

```bash
chmod +x /path/to/delete_sslog_files.sh
```

Example:

```bash
chmod +x /home/ubuntu/delete_sslog_files.sh
```

---

## Open Crontab

```bash
crontab -e
```

---

## Add Daily Cron Job

Example: Run every day at 1:00 AM

```bash
0 1 * * * /bin/bash /home/ubuntu/delete_sslog_files.sh >> /home/ubuntu/delete_sslog_files.log 2>&1
```

---

# Cron Schedule Format

```text
* * * * * command
| | | | |
| | | | +---- Day of Week (0-7)
| | | +------ Month
| | +-------- Day of Month
| +---------- Hour
+------------ Minute
```

---

# Log Verification

To verify script execution:

```bash
cat /home/ubuntu/delete_sslog_files.log
```

Or monitor logs live:

```bash
tail -f /home/ubuntu/delete_sslog_files.log
```

---

# Important Notes

- Deleted files cannot be recovered unless backups exist.
- Ensure correct directory permissions are set.
- Recommended to test without `-delete` first:

```bash
find "$BACKUP_DIR" -type f -name "*.log" -mtime +15
```

This command will only list files that would be deleted.

---

# Expected Outcome

After daily execution:

- `.log` files older than 15 days are automatically removed.
- Disk usage remains optimized.
- Cleanup activity is recorded in the log file.

