Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Custom Cleanup Script

Discussion in 'Workarounds and Optimization' started by Bashed, Oct 24, 2017.

  1. Bashed

    Bashed Well-Known Member

    Joined:
    Dec 18, 2013
    Messages:
    106
    Likes Received:
    3
    Trophy Points:
    18
    cPanel Access Level:
    Root Administrator
    Hello,

    I run the below manually on occasion to do cleanup on my servers. I'd like to automatic this via cron into one single bash script. Would anyone know how to go about this? I've never done that before.

    Code:
    exim -bp | grep \< | awk '{print $3}' | xargs exim -Mrm
    find /home -type f -name error_log -delete
    for user in `/bin/ls -A /var/cpanel/users` ; do rm -fv /home/$user/backup-*$user.tar.gz ; done
    updatedb
    locate .pureftpd-upload | xargs rm -fv
    locate -r /core\.[0-9] | grep /home | egrep -v 'virtfs|php|sql|ini'
    rm -rf /usr/local/apache.backup*
    rm -fv /home/*/tmp/Cpanel_*
    rm -f /var/log/*.gz
    rm -f /var/log/exim_mainlog-*
    rm -f /var/log/exim_paniclog-*
    rm -f /var/log/exim_rejectlog-*
    rm -f /var/log/lve-stats.log-*
    rm /home/*.tar.gz -fv
    rm /home/*/public_html/error_log -fv
    rm /home/*/public_html/*/error_log -fv
    rm /home/*/public_html/*/*/error_log -fv
    rm /home/*/public_html/*/*/*/error_log -fv
    rm /home/*/public_html/*/*/*/*/error_log -fv
    cd /var/log
    rm -f cagefs.log-* cron-* maillog-* messages-* secure-* spooler-* up2date-* *.gz
    yum clean all
    
    grep cwd /var/log/exim_mainlog | grep -v /var/spool | awk -F"cwd=" '{print $2}' | awk '{print $1}' | sort | uniq -c | sort -n
    mysqlcheck --auto-repair --optimize --all-databases
    
    
    **********************
    
    MAIL SCRIPT CLEANUP
    
    **********************
    
    nano /root/delete_all_junk.sh
    
        #!/bin/bash
        MAILDIRS=$(find /home/*/mail/*/* -maxdepth 0 -type d)
        INBOXFOLDERS=(.Trash .Junk .Spam .Low\ Priority .cPanel\ Reports)
        for basedir in $MAILDIRS; do
        for ((i = 0; i < ${#INBOXFOLDERS[*]}; i++)); do
        for dir in cur new; do
        [ -e "$basedir/${INBOXFOLDERS[$i]}/$dir" ] && (
        echo "Processing $basedir/${INBOXFOLDERS[$i]}/$dir..."
        find "$basedir/${INBOXFOLDERS[$i]}/$dir/" -type f -mtime +30 -delete
        )
        done
        done
        done
        /scripts/generate_maildirsize -verbose -allaccounts -force -confirm
    
    chmod +x /root/delete_all_junk.sh
    sh /root/delete_all_junk.sh
     
    cesarlopes likes this.
  2. cesarlopes

    cesarlopes Registered

    Joined:
    Oct 18, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    51
    Location:
    Tiete, Sao Paulo, Brazil
    cPanel Access Level:
    Reseller Owner
    I liked your script ...

    If you create a shell file you can run it with cron, have you tried this?
     
  3. Bashed

    Bashed Well-Known Member

    Joined:
    Dec 18, 2013
    Messages:
    106
    Likes Received:
    3
    Trophy Points:
    18
    cPanel Access Level:
    Root Administrator
    Ok, did you actually read my post at all? It's not a "script". I specifically asked how to put all the commands into a script and run it automatically via cron. You completely ignored the very same point I asked about.
     
  4. Muhammed Fasal

    Muhammed Fasal Active Member

    Joined:
    Aug 9, 2017
    Messages:
    40
    Likes Received:
    3
    Trophy Points:
    8
    Location:
    India
    cPanel Access Level:
    Root Administrator
    Hi, :)

    I liked your way of cleaning server :rolleyes: I have created a bash script from your codes. You can download it directly from here
    Code:
    serverlogs.cf/cleaning/cleaning_server.sh
    ;)

    Thanks :-D
     
  5. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    42,721
    Likes Received:
    1,705
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Hello,

    Let us know if the previous post helped. Note that you will typically find better support for bash scripting on websites such as StackOverflow.

    Thank you.
     
Loading...

Share This Page