12 March 2012

How to clean-up JBoss temporary directories using bash scripting

Repetitive and annoying file system tasks are the natural field of automation through shell scripting - so, here's a script  that can be used to clean up temporary directories created by JBoss Application Server.

You can provide as parameter the name of the node that you want clean up - if launched without parameters, the script cleans up temporary directories in each node under current installation.

Tested on JBoss 4.0.5.GA and JBoss 5.1.0.GA installations.

#!/bin/bash
# Commonly available under GPL 3 license
# Copyleft Pietro Martinelli - javapeanuts.blogspot.com

function cleanTmpDir {
        echo "  Cleaning ${1}/${2}"
        rm -rf "${1}/${2}"

}

function cleanNode {
        echo "Cleaning \"${1}\" jboss node"
        for tmpDir in data log tmp work
        do
                cleanTmpDir ${1} ${tmpDir}
        done
}


if [ $# -eq 0 ]
then
        for dir in $(find server -maxdepth 1 -mindepth 1 -type d)
        do
                cleanNode ${dir}
        done
else
        if [ -e "server/${1}" ]
        then
                cleanNode "server/${1}"
        else
                echo "${1} is not a subdir of server dir"
        fi
fi

Updated: this script is now available on bitbucket.org:
https://bitbucket.org/thecleancoder/javapeanuts-shell-utils/src