Managing disk space efficiently is an important step in maintaining the performance of your CWP (Control WebPanel) server. One common way to free up disk space is by clearing old or unnecessary log files.
In this tutorial, we’ll guide you through two solutions for clearing all log files on your CWP server without impacting its services. We’ll also provide instructions for automating the process using cron jobs.
Requirements
Before proceeding, ensure that you are logged in as the root user via SSH. This is necessary to execute the commands and scripts described in this guide.
Create a Log Clearing Script
To create a log clearing script and free up disk space, follow the steps below:
1. Create a Script File
Navigate to the /root directory and create a new file:
cd /root
nano clearlog.sh
2. Add the Bash Script
Open the file in a text editor and paste the following bash script:
#!/bin/bash
truncate -s 0 /usr/local/apache/logs/*bytes
truncate -s 0 /usr/local/apache/logs/*log
truncate -s 0 /usr/local/apache/domlogs/*bytes
truncate -s 0 /usr/local/apache/domlogs/*log
truncate -s 0 /var/log/messages
truncate -s 0 /var/log/maillog
truncate -s 0 /var/log/*log
truncate -s 0 /opt/alt/*/usr/var/log/php-fpm.log
truncate -s 0 /usr/local/cwpsrv/logs/access_log
truncate -s 0 /usr/local/cwpsrv/logs/error_log
truncate -s 0 /var/log/cron
truncate -s 0 /var/log/secure
truncate -s 0 /var/log/cwp/services_action.log
truncate -s 0 /var/log/cwp/cwp_sslmod.log
truncate -s 0 /var/log/cwp/cwp_cron.log
truncate -s 0 /var/log/cwp/cwp_backup.log
truncate -s 0 /var/log/cwp/activity.log
truncate -s 0 /usr/local/cwpsrv/var/services/roundcube/logs/errors
rm -rf /var/log/maillog-*
rm -rf /var/log/monit.log-*
rm -rf /var/log/spooler-*
rm -rf /var/log/messages-*
rm -rf /var/log/secure-*
rm -rf /var/log/pureftpd.log-*
rm -rf /var/log/yum.log-*
rm -rf /var/log/monit.log-*
rm -rf /var/log/cron-*
rm -rf /var/lib/clamav/tmp.*
3. Set Permissions
Ensure the script has executable permissions by running the following command:
chmod 755 /root/clearlog.sh
4. Execute the Script
Run the script to clear all logs:
sh /root/clearlog.sh
Once executed, the logs will be cleared. You can verify this by checking the log file locations.
Automating the Process with Cron Jobs
To regularly clear log files, you can set up a cron job. This will automate the execution of the log-clearing script according to a schedule that suits your needs.
1. Daily
To run the script daily, add this entry to your crontab:
0 0 * * * /usr/bin/sh /root/clearlog.sh
2. Weekly
To run the script weekly:
0 0 * * 0 /usr/bin/sh /root/clearlog.sh
3. Monthly
To run the script monthly:
0 0 1 * * /usr/bin/sh /root/clearlog.sh
Use the schedule that best fits your server’s log management requirements.
Summary
By following this guide, you can effectively manage log files on your CWP server and ensure optimal use of disk space. Whether you use a pre-written script or create your own, these solutions provide a straightforward way to keep your server running smoothly.
You May Also Like
- Go with a .co? Weighing the Pros and Cons of the Second Most Trusted TLD
- Maximize your social media reach with this simple blueprint
- How to Enable the WDDX Extension in PHP 7.4: Installation Guide
- Top Picks: 5 Best Linux Distros for Software Engineers
- Converting CentOS 8 to CentOS 8 Stream and Installing CWP Control Panel