home *** CD-ROM | disk | FTP | other *** search
/ ftp.jcu.edu.au / 2014.06.ftp.jcu.edu.au.tar / ftp.jcu.edu.au / v6.3.2b / SWBD63 / fabos-6.3.2b-10.ppc.rpm / fabos-6.3.2b.10.cpio.gz / fabos-6.3.2b.10.cpio / sbin / core_tidy.sh < prev    next >
Linux/UNIX/POSIX Shell Script  |  2010-11-10  |  3KB  |  135 lines

  1. #!/bin/bash
  2. # Function to reduce the number of core files in a directory to
  3. # a maximum of $KEEP. Delete from the oldest to the newest
  4.  
  5. # Defaults, can be overridden using command parameters
  6.  
  7. DESIRED_SPACE=20000
  8. DESIRED_PERCENT=80
  9. DO_GZIP=0
  10. GZIP=/bin/gzip
  11. RM=/bin/rm
  12.  
  13. shopt -s extglob nullglob
  14.  
  15. # A newline, used to help read difficult filenames
  16. nl='
  17. '
  18.  
  19. # Remove files from directory $1 (get back by going to $2) that are either
  20. # core or core.[0-9][0-9]*. However keep at least $KEEP of them, which are
  21. # the newest.
  22. # Make use of the GNU ls program, which can output filenames
  23. # like "fred" or "bill\"", including quotes
  24.  
  25. trimdir(){
  26.     cd "$1"
  27.     olddir="$2"
  28.     # Do a quick check before we bother calling ls
  29.     set -- core?(.+([0-9]))?(.gz)
  30.     if [[ $# > $KEEP ]] ; then
  31.         # Read in the core file names, dealing with weird characters
  32.         OIFS="$IFS"; IFS="$nl"
  33.         set -- $(ls --quoting-style=c -t1d "$@" 2>/dev/null)
  34.         IFS="$OIFS"
  35.         if [[ $# > $KEEP ]] ; then
  36.             shift $KEEP
  37.             for filename ; do
  38.                 # Strip off the " that the ls put on.
  39.                 j=${filename#\"}; j=${j%\"}; j=${j//\\\"/\"}
  40.                 # Append full pathname to the togo array
  41.                 togo[$C]="$(printf %s/%b "$PWD" "$j" )"
  42.                 let C+=1
  43.             done
  44.         fi
  45.     fi
  46.     cd "$olddir"
  47. }
  48.  
  49. compress_in_dir(){
  50.     cd "$1"
  51.     olddir="$2"
  52.     set -- core?(.+([0-9]))
  53.   [ $# -gt 0 ] && $GZIP "$@"
  54.   cd "$olddir"
  55. }
  56.  
  57. # loop over directories except panic and trim each one.
  58.  
  59. tidy(){
  60.     let C=0
  61.     unset togo
  62.     for i in !(panic)/.
  63.     do
  64.         trimdir "$i" ..
  65.     done
  66.     if [[ $C > 0 ]] ; then
  67.         $RM -- "${togo[@]}"
  68.     fi
  69. }
  70.  
  71. compress_all_core_files(){
  72.     for i in !(panic)/.
  73.     do
  74.         compress_in_dir "$i" ..
  75.     done
  76. }
  77.  
  78.  
  79. # Return 0 when then is enough space, 1 otherwise
  80. space(){
  81.     set -- $(df .)
  82.     # Skip header ("filesystem 1k-blocks used available use% mounted on")
  83.     shift 7
  84.     # $1=where, $2=size, $3=used $4=free $5=free% $6=mnt_point
  85.     # Can either use DESIRED_SPACE as a number of blocks
  86.     if [[ $4 < $DESIRED_SPACE ]] ; then return 1 ; fi
  87.     # Or as a percentage
  88.     percent=${5%\%}
  89.     if [[ $percent > $DESIRED_PERCENT ]] ; then return 1 ; else return 0 ; fi
  90. }
  91.  
  92. # Loop, with smaller and smaller values of KEEP until we have enough space
  93. # Returns 0 is there is enough space, 1 otherwise
  94. tidy_till_space(){
  95.     for KEEP in 4 3 2 ; do
  96.         if space ; then return  0; fi
  97.         tidy
  98.     done
  99.     # Do it one more time to set return code
  100.     space
  101. }
  102.  
  103. usage(){
  104.     u="Usage: $0 [-c] [-p percent_space] [-n desired_free_blocks] [-P rm_program]" 
  105.     echo "$u" >&2
  106.     echo "Usage: $0 -h [this help]" >&2
  107.     exit ${1:-2}
  108. }
  109.  
  110. # Main program.
  111. # Decode arguements
  112. while getopts "hcp:n:P:" option ;do
  113.     case "$option" in
  114.     c)  DO_GZIP=1 ;;
  115.     p)    DESIRED_PERCENT="$OPTARG" ;;
  116.     n)    DESIRED_SPACE="$OPTARG" ;;
  117.     P)    RM="$OPTARG" ;;
  118.     h)    usage 0;;
  119.     \?)    usage ;;
  120.     esac
  121. done
  122. shift $(($OPTIND - 1))
  123.  
  124. if [ $# -ne 0 ] ; then
  125.     usage
  126. fi
  127.  
  128. # First attempt to compress files that can be compressed
  129. if [ $DO_GZIP -eq 1 ]; then 
  130.     compress_all_core_files
  131. fi
  132.  
  133. # clean up additional files 
  134. tidy_till_space
  135.