# Bash Script Expunge/Clear Logs and Journal Debian/Ubuntu Compatible using Cron

This documentation provides step-by-step instructions on creating and implementing a shell script named clean-logs.sh on an Ubuntu/Debian 12 server. The…

Canonical: https://www.ruhanirabin.com/code-snippet/bash-script-expunge-clear-logs-journal-debian-ubuntu-cron/

Aug 27, 2024Generic

This documentation provides step-by-step instructions on creating and implementing a shell script named `clean-logs.sh` on an Ubuntu/Debian 12 server. The purpose of this script is to reduce the **overhead of log files and journal entries** by truncating log files and clearing **systemd** journal logs older than one day.

## Additional Snippets

## 1. Logging into Your Server

### Step 1: Open Terminal

* On your local machine, open the terminal application.

### Step 2: Connect to Your Server

* Use SSH to connect to your Ubuntu/Debian server. Replace `your_server_ip` with your server's IP address:

  `ssh root@your_server_ip`
* If prompted, enter your root password.

**Note**: It is recommended that SSH keys be used for more secure authentication.

## 2. Creating the Clean-Logs Script

### Step 1: Navigate to the Appropriate Directory

* Once logged in as root, navigate to the directory where you want to store your script. Typically, scripts are stored in `/usr/local/bin`:

  `cd /usr/local/bin`

### Step 2: Create the Script File

* Use a text editor like `nano` to create the `clean-logs.sh` script:

  `nano clean-logs.sh`

### Step 3: Write the Script

* Copy and paste the **following script** into the editor:
* Save and close the file by pressing `CTRL + X`, then `Y`, and `Enter`.

### Step 4: Make the Script Executable

* Change the script’s permissions to make it executable:

  `chmod +x clean-logs.sh`

## 3. Understanding the Script

This script performs the following tasks:

1. **Truncates Log Files**: The script targets log files (`*.log`) in the `/var/log` directory and truncate them to 0 bytes. This means it empties the log files without deleting them, which helps maintain the structure while reducing disk usage.
2. **Clears Systemd Journal Logs**: It uses the `journalctl --vacuum-time=1d` command to remove systemd journal logs older than one day. This helps in freeing up space without losing recent logs.
3. **Success Message**: After performing the tasks, the script prints a message indicating successful execution.

## 4. Setting Up a Cron Job

### Step 1: Open the Cron Table

* To schedule this script to run automatically, you can add it to the cron table:

  `crontab -e`

### Step 2: Add the Cron Job

* Scroll to the bottom of the file and add the following line to schedule the script to run daily at 2 AM:

  `0 2 * * * /usr/local/bin/clean-logs.sh > /dev/null 2>&1`
* Save and exit the editor.

### Recommended Cron Times

* **Daily Cleanup**: 2 AM is a typical time to perform maintenance tasks since server load is generally low.
* **Weekly Cleanup**: If you prefer a weekly cleanup, you can adjust the cron schedule accordingly, e.g., every Sunday at 3 AM:

  `0 3 * * 0 /usr/local/bin/clean-logs.sh > /dev/null 2>&1`

## 5. Testing the Script

### Step 1: Run the Script Manually

* To ensure the script works as expected, you can run it manually:

  `/usr/local/bin/clean-logs.sh`
* You should see the message: `Log files cleared successfully!`

### Step 2: Check Log Files

* After running the script, you can verify that the log files in `/var/log` have been truncated by checking their sizes:

  `ls -lh /var/log/*.log`
* The sizes should show `0` bytes if the truncation was successful.

## 6. Troubleshooting

* If the script does not run as expected, ensure the paths are correct and the script has executable permissions.
* Check the cron logs to verify that the job is being executed:

  `grep CRON /var/log/syslog`

## Conclusion

By following this documentation, you’ve set up a `clean-logs.sh` Script to manage your server's log files efficiently. Regularly clearing logs helps reduce disk usage and ensures your server runs smoothly without unnecessary overhead.

```
#!/bin/bash

# List of directories containing log files
LOG_DIRS=(
    "/var/log"
)

# Loop through each directory and clear the log files
for DIR in "${LOG_DIRS[@]}"; do
    find "$DIR" -type f -name "*.log" -exec truncate -s 0 {} \;
done

# Optionally, you can clear systemd journal logs
journalctl --vacuum-time=1d

# Print a message
echo "Log files cleared successfully!"
```
