Sandeep B. Posted June 1, 2023 Share Posted June 1, 2023 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 1 Link to comment Share on other sites More sharing options...
narin Posted June 1, 2023 Share Posted June 1, 2023 Good luck with your new site, Sandeep. Is there anything I can help you with? 1 Link to comment Share on other sites More sharing options...
Sandeep B. Posted June 1, 2023 Author Share Posted June 1, 2023 1 hour ago, narin said: Good luck with your new site, Sandeep. Is there anything I can help you with? keep visiting 🙂 and share as far as you can, thank you Link to comment Share on other sites More sharing options...
torettos Posted June 12, 2023 Share Posted June 12, 2023 Thank you, Sandeep! P.S.: Quote rm -rf /var/log/monit.log-* This remove is call it 2 times, it has to be like this or ... ? 1 Link to comment Share on other sites More sharing options...
Sandeep B. Posted June 12, 2023 Author Share Posted June 12, 2023 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 More sharing options...
torettos Posted June 15, 2023 Share Posted June 15, 2023 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 More sharing options...
Sandeep B. Posted June 15, 2023 Author Share Posted June 15, 2023 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 More sharing options...
torettos Posted June 21, 2023 Share Posted June 21, 2023 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 More sharing options...
Sandeep B. Posted June 21, 2023 Author Share Posted June 21, 2023 find /dir/to/search/ -type f -name "FILE-TO-FIND" -exec rm -rf {} \; try this Link to comment Share on other sites More sharing options...
torettos Posted June 21, 2023 Share Posted June 21, 2023 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 More sharing options...
torettos Posted June 21, 2023 Share Posted June 21, 2023 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 More sharing options...
Sandeep B. Posted June 21, 2023 Author Share Posted June 21, 2023 On 6/21/2023 at 3:10 PM, torettos said: /root/clearsession.sh: line 2: $'\r': command not found this happens because of windows editor you need to change the format with dos2unix Link to comment Share on other sites More sharing options...
torettos Posted June 22, 2023 Share Posted June 22, 2023 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 More sharing options...
Sandeep B. Posted June 22, 2023 Author Share Posted June 22, 2023 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 More sharing options...
torettos Posted June 22, 2023 Share Posted June 22, 2023 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. 1 Link to comment Share on other sites More sharing options...
torettos Posted June 23, 2023 Share Posted June 23, 2023 Hello Sandeep, I report after dos2unix conversion of bash script everything works fine, without errors, warn or notice. You are awesome, thank you so much! 1 Link to comment Share on other sites More sharing options...
Navid Posted January 3 Share Posted January 3 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 More sharing options...
Navid Posted January 3 Share Posted January 3 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 More sharing options...
Sandeep B. Posted January 4 Author Share Posted January 4 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 More sharing options...
Sandeep B. Posted January 4 Author Share Posted January 4 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 More sharing options...
torettos Posted January 4 Share Posted January 4 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! 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now