Bashed

Well-Known Member
Dec 18, 2013
146
4
68
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
 
  • Like
Reactions: cesarlopes

Bashed

Well-Known Member
Dec 18, 2013
146
4
68
cPanel Access Level
Root Administrator
I liked your script ...

If you create a shell file you can run it with cron, have you tried this?
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.
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,909
2,228
463
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.
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,909
2,228
463
Hello @Bashed,

I've removed the post you are referencing because the URL no longer works. If you're looking for help on how to create a bash script, you'll likely find more information on a website such as StackOverflow where programming discussing occur. Here's a third-party URL you may find helpful:

How to Create a Simple Shell Script on Linux

Thank you.