Script to clear filesystem

Hi,

I have created a script to clear up the /var filesystem once it reaches > 90%.
This is part of the script :

#!/bin/bash

DIR = ./adm
DIR2=./adm/sw
DIR3 = ./spool/mqueue
DIR4 = ./adm/syslog
DIR5 = ./adm/sulog
DIR6 = ./tmp
F1 = ./tmp/dead.letter
F2 = ./adm/wtmps
file1 = dead.letter
file2 = wtmps

dir=`du -x | sort -rn | head -20`
cd /var
for i in $dir;
do
	if [ -d "$DIR1" ]; then
		cd "$DIR1"
			if [ -f "$file2" ]; then
			 cat /dev/null > "$file2"
	elif [ -d "$DIR2" ]; then
		tar -cvf /temp/admsw.arch "$DIR2";
		rm -rf "$DIR2"
	elif [ -d "$DIR3" ]; then
		find "$DIR3" -mtime +100 -exec rm {} \;			
	elif [ -d "$DIR4" ]; then

	elif [ -d "$DIR5" ]; then	
		
	elif [ -d "$DIR6" ]; then
		cd "$DIR6"
			if [ -f "$file1" ]; then
				rm -f "$file1"
 			fi
	elif [ -f "$F1" ]; then
		rm -f "$F1"
	elif [ -f "$F2" ]; then
		cat /dev/null > "$F2"
	fi
done

To test if this script works, I execute the command in the terminal (only to clear one of the filesystems above). However it seemed to run for an extremely long time. Therefore I sensed that something could be wrong with the statements I used - perhaps the for loop?

These are the steps I executed :

# cd /var
# dir=`du -x | sort -rn | head -20`
# DIR2=./adm/sw
#for i in $dir;
>do
>if [ -d "$DIR2" ]; then
>tar -cvf /temp/admsw.tar "$DIR2";
>fi
>done

Are the above steps correct? Why does the command run for a long time?

Have you tried removing all of your ELIF commands, and only work with each DIR one-by-one? That would allow you to se which of those eight commands is your culprit.

Is /temp part of root volume group or separated ?
Can you check sar -d 3 10 # <-- example 10 times every 3 sec
You can use -b options as well to check how the I/O is going on.

Do you have databases or other applications running on root vg disk(s) ?

Check out the process table using ps command and top
You are looking for 'runaway' processes, zombies or anything that actually is issuing intensive I/O.

Feel free to post those here, obfuscating information if any.

Omiting -v tar option could provide some performance benefit as well, again depending on the mostly CPU resources available.

Why do you run a for loop with variable i but then do nothing with $i ?

A suggestion for an alternate approach:

#!/bin/bash

DIRS=(./adm ./adm/sw ./tmp)
FILES=(dead.letter wtmps)

for CURRENT_DIR in "${DIRS[@]}" ; do
  if [ -d $CURRENT_DIR ] ; then
      for CURRENT_FILE in "${FILES[@]}" ; do
        if [ -f $CURRENT_DIR/$CURRENT_FILE ] ; then
           cat /dev/null >$CURRENT_DIR/$CURRENT_FILE
        fi
      done
  fi
done

Not exactly what you are doing. But maybe it helps with optimizing.

Your script does not work for several reasons.
The du -x ... is run in the current directory.
root's home directory is / so it takes very long to traverse all the files. Then, the du output consists of numbers and directories. The for loop cycles through all of them. The code in the loop is run over and over again.
Now you know why it takes so long.
--
Once upon a time I created a script that rotates HP-UX system log files.
Useful on systems that are not regularly rebooted.
It is run by the crontab entry:

10 3 1 * * /usr/lib/newsyslog

And the executable /usr/lib/newsyslog looks like this

#!/bin/sh
# idea from Sun's newsyslog script
# HP-UX version
#
MIN_BLOCKS=2048 # rotate if >1MB
MAX_HIST=3 # .0 through .3
#
# access rotated files with
# last -f /var/adm/wtmp.0
# last -Xf /var/adm/wtmps.0
# lastb -f /var/adm/btmp.0
# lastb -Xf /var/adm/btmps.0
#
pgrepx(){
UNIX95=1 ps -C "$1" -o pid=
}
#
testlog(){
 for _i do
  [ -s "$_i" ] &&
   [ -n "`find "$_i" -prune -type f -size +"$MIN_BLOCKS"`" ] &&
   return
 done
}
#
rotatelog(){
 log=$1
 method=${2:-mv}
 i=$MAX_HIST
 while [ $i -gt 0 ]
 do
  j=`expr $i - 1`
  test -f $log.$j && mv $log.$j $log.$i
  i=$j
 done
 $method $log $log.0
 > $log
}
#
umask 22
LOGS="
/var/adm/syslog/syslog.log
/var/adm/syslog/mail.log
"
action=""
for log in $LOGS
do
 if testlog $log
 then
  rotatelog $log "mv"
  action=1
 fi
done
[ -n "$action" ] &&
 chmod 644 /etc/syslog.pid &&
 kill -HUP `cat /etc/syslog.pid`
#
LOGS="
/var/opt/omni/log/inet.log
/var/opt/omni/log/debug.log
"
action=""
for log in $LOGS
do
 if testlog $log
 then
  rotatelog $log "mv"
  action=1
 fi
done
#
umask 2
LOGS="
/var/adm/wtmps
/var/adm/wtmp
/var/adm/btmps
/var/adm/btmp
/var/adm/sulog
/var/adm/lp/lpd.log
"
# rotate all non-zero files or none
action=""
if testlog $LOGS
then
 for log in $LOGS
 do
  if [ -s $log ]
  then
   rotatelog $log "cp -p"
   action=1
  fi
 done
fi
#
# finally rotate the cron logs
umask 22
LOGS="
/var/adm/cron/log
"
action=""
cronstart=/sbin/init.d/cron
[ -f $cronstart ] && {
for log in $LOGS
do
 if testlog $log
 then
  rotatelog $log "mv"
  action=1
 fi
done
# restart cron a bit time-shifted
[ -n "$action" ] &&
 sleep 1 &&
 sh $cronstart stop &&
 sleep 1 &&
 sh $cronstart start
}
#

If you have understood it, you can add more things to the LOGS= strings.