Here is a simple script which will perform Automatic Disk Log Cleanup for any Linux Environment. You just have to provide correct CRUNCHIFY_TMP_DIRS . When a disk has no free space left, all kinds of trouble can occur.
But before that let’s have some basic understanding of some important commands.
Step-1)
Check df -H command.
Use the df
command to display information about total space and available space on a file system.
The FileSystem parameter specifies the name of the device on which the file system resides, the directory on which the file system is mounted, or the relative path name of a file system.
If you do not specify the FileSystem parameter, the df command
displays information for all currently mounted file systems. If a file or directory is specified, then the df command displays information for the file system on which it resides.
Output:
Filesystem Size Used Avail Capacity iused ifree %iused Mounted on /dev/disk0s2 499G 114G 385G 23% 27868719 94059510 23% / devfs 189k 189k 0B 100% 640 0 100% /dev
Step-2)
Next filter out filesystem and find out the percentage of space
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }'
Output:
23% /dev/disk0s2 100% devfs
So, sometimes programmatically you may want to cleanup files from specific folders in case you ran out of space. In that case, you just need to execute below script and it will take care of cleaning all unused files based on filter criteria mentioned in script. It also send out an email to user specified in script.
Another must read:
Complete Linux DiskCleanup Script:
#!/bin/bash # Diskclean-Linux.sh - Remove unused files from /tmp directories # @author: Crunchify.com # ------------- Here are Default Configuration -------------------- # CRUNCHIFY_TMP_DIRS - List of directories to search # DEFAULT_FILE_AGE - # days ago (rounded up) that file was last accessed # DEFAULT_LINK_AGE - # days ago (rounded up) that symlink was last accessed # DEFAULT_SOCK_AGE - # days ago (rounded up) that socket was last accessed CRUNCHIFY_TMP_DIRS="/tmp /var/tmp /usr/src/tmp /mnt/tmp" DEFAULT_FILE_AGE=+2 DEFAULT_LINK_AGE=+2 DEFAULT_SOCK_AGE=+2 # Make EMPTYFILES true to delete zero-length files EMPTYFILES=false #EMPTYFILES=true cd /tmp/log "cleantmp.sh[$] - Begin cleaning tmp directories" echo "" echo "delete any tmp files that are more than 2 days old" /usr/bin/find $CRUNCHIFY_TMP_DIRS \ -depth \ -type f -a -ctime $DEFAULT_FILE_AGE \ -print -delete echo "" echo "delete any old tmp symlinks" /usr/bin/find $CRUNCHIFY_TMP_DIRS \ -depth \ -type l -a -ctime $DEFAULT_LINK_AGE \ -print -delete echo "" if /usr/bin/$EMPTYFILES ; then echo "delete any empty files" /usr/bin/find $CRUNCHIFY_TMP_DIRS \ -depth \ -type f -a -empty \ -print -delete fi echo "Delete any old Unix sockets" /usr/bin/find $CRUNCHIFY_TMP_DIRS \ -depth \ -type s -a -ctime $DEFAULT_SOCK_AGE -a -size 0 \ -print -delete echo"" echo "delete any empty directories (other than lost+found)" /usr/bin/find $CRUNCHIFY_TMP_DIRS \ -depth -mindepth 1 \ -type d -a -empty -a ! -name 'lost+found' \ -print -delete echo "" /usr/bin/logger "cleantmp.sh[$] - Done cleaning tmp directories" # send out an email about diskcleanup action mail -s "Disk cleanup has been performed successfully." you@email.com echo "" echo "Diskcleanup Script Successfully Executed" exit 0
Hope this helps. If you have any query on above script then let me know. Any suggestions are most welcome. The script works under Linux and Mac OS X.
Want to run above script every 3 days
? Just use below cron schedule 🙂
Detailed tutorial on Setting up CronJobs
is on it’s way. Please stay tuned.
0 0 */3 * * /opt/crunchify/crunchify_script.sh