Jump to content

Clean all log files and Free DISK Space in CWP – Control WebPanel


Recommended Posts

In this tutorial I’ll explain and provide solution script upon how you can clear all log files including old logs from CWP server. This tutorial is specially dedicated to the blog visitors who have emailed me to provide such solution.

This script will clean all possible logs without affecting any service. Ensure you’ve logged in as root ssh user

Solution 1 :

Run CWP script to clean logs :

/scripts/clean_all_server_logs

Solution 2:

To clear the log instantly you can run this bash script as i already created for your to make the task faster or else if you want to create by your own go to solution 3.

curl -s -L https://www.alphagnu.com/upload/clear-sh.sh | bash


Solution 3:

First create a file in /root dir :

cd /root
nano clearlog.sh


Then paste this Bash script and save it:

#!/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/cwpsecure/reject_003.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
truncate -s 0  /var/spool/amavisd/.razor/razor-agent.log
truncate -s 0  /usr/local/cwp/php71/var/log/php-fpm.log
truncate -s 0  /root/.acme.sh/cwp_certs/acme.sh.log
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/cron-*
rm -rf /var/lib/clamav/tmp.*
find /tmp/ -type f -name "sess_*" -exec rm {} \;
rm -f /tmp/202*request_body-*
echo "" > /var/spool/mail/root


Change the permission :

chmod 755 /root/clearlog.sh


Now run this command to run the clear log script:

sh /root/clearlog.sh


Thats it the logs will be cleared you can go and check to the respected locations of the log.

Cron Job :

You can also create cron job like below by following solution 2 and create this cron job task according to your needs :

TO run DAILY :

0 0 * * * /usr/bin/sh /root/clearlog.sh


TO run Weekly :

0 0 * * 0 /usr/bin/sh /root/clearlog.sh


TO run Monthly:

0 0 1 * * /usr/bin/sh /root/clearlog.sh

 

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

Hello Sandeep,

i maked process automate with your example for cron job:

0 0 * * * /usr/bin/sh /root/clearlog.sh

... but e-mail notification return me this error:
 

Quote

/bin/sh: 0: command not found

Why and how can i fix it?

Link to comment
Share on other sites

1 hour ago, torettos said:

Hello Sandeep,

i maked process automate with your example for cron job:

0 0 * * * /usr/bin/sh /root/clearlog.sh

... but e-mail notification return me this error:
 

Why and how can i fix it?

hi
search the `sh` binary location by typing :

which sh

it will output the binary path location replace with /usr/bin/sh 

 

by the way sh location is always in /usr/bin/sh in centos

Link to comment
Share on other sites

On 6/15/2023 at 12:24 PM, Sandeep B. said:

hi
search the `sh` binary location by typing :

which sh

it will output the binary path location replace with /usr/bin/sh 

 

by the way sh location is always in /usr/bin/sh in centos

I finded the problem - was in Cron Job error ... thank you so much!

One анотхер question for optimay the cleaning file (on my case). How i can to add this command:

find /tmp -name "sess_*" -exec rm -f {} \;

... to the shell script file?

I tryed directly but without results 😞

Link to comment
Share on other sites

32 minutes ago, Sandeep B. said:
find /dir/to/search/ -type f -name "FILE-TO-FIND" -exec rm -rf {} \;

try this

I try it:

find /tmp/ -type f -name "sess_*" -exec rm -rf {} \;

but the result is:

Quote

/root/clearsession.sh: line 2: $'\r': command not found

 

Link to comment
Share on other sites

But after check on command line:

Quote

find /tmp -name "sess_*" -print | wc -l

... the sessions is removed 🙂

Maybe if it's fixed "command not found" and will star to work without problems and will be possible to set cron job.
 

Link to comment
Share on other sites

On 6/21/2023 at 3:24 PM, Sandeep B. said:

this happens because of windows editor you need to change the format with dos2unix

OK after your comment i tried to leave in cron job and this is the result form this night:

Quote
/root/clearlog.sh: line 2: $'\r': command not found
truncate: cannot open ‘/opt/alt/*/usr/var/log/php-fpm.log\r’ for writing: No such file or directory
/root/clearlog.sh: line 24: $'\r': command not found
/root/clearlog.sh: line 35: $'\r': command not found

Any idea how to change it?

Link to comment
Share on other sites

8 hours ago, torettos said:

OK after your comment i tried to leave in cron job and this is the result form this night:

Any idea how to change it?

Hi did you converted your bash script with dos2unix?

yum install dos2unix -y  
dos2unix /root/clearlog.sh  

 

Link to comment
Share on other sites

  • 3 months later...

HI, kindly guide me on how can fix this error: 

truncate: cannot open ‘/var/log/cwpsecure/reject_003.log’ for writing: No such file or directory

 

thank you

 

Link to comment
Share on other sites

HI, as well the cron is not script is not working that you guided below

0 0 * * 0 /usr/bin/sh /root/clearlog.sh

I set it from cwp admin, it will work from your point of view??

 

thanks.

Link to comment
Share on other sites

5 hours ago, Navid said:

HI, kindly guide me on how can fix this error: 

truncate: cannot open ‘/var/log/cwpsecure/reject_003.log’ for writing: No such file or directory

 

thank you

 

This is not an issue if you've secure kernel installed the log file will be there.

Link to comment
Share on other sites

5 hours ago, Navid said:

HI, as well the cron is not script is not working that you guided below

0 0 * * 0 /usr/bin/sh /root/clearlog.sh

I set it from cwp admin, it will work from your point of view??

 

thanks.

Its works fine just tested it

Link to comment
Share on other sites

9 hours ago, Navid said:

HI, as well the cron is not script is not working that you guided below

0 0 * * 0 /usr/bin/sh /root/clearlog.sh

I set it from cwp admin, it will work from your point of view??

 

thanks.

Hello Navid,

the cron work fine - tested from me and confirm this!

Have only one problem if you make it daily and wаnt to monitoring mail logs (for reason of SPAM and etc.) in this case is neccesere to remove this lines:

truncate -s 0 /var/log/maillog
truncate -s 0  /usr/local/cwpsrv/var/services/roundcube/logs/errors
rm -rf /var/log/maillog-*


Have a nice day!

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...