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 :

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 2.

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


Solution 2 :

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/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.*


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...
2 hours ago, torettos said:

Thank you, Sandeep!

 

P.S.:

This remove is call it 2 times, it has to be like this or ... ?

thanks for the info it is fixed in the script, if you run this 2 times there will be no issues.

Link to comment
Share on other sites

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

30 minutes ago, Sandeep B. said:

Hi did you converted your bash script with dos2unix?

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

 

Thank you so much, i make it and after cron job we see it.

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...