Previous Topic: Configure the Agent to Clear Spool Files AutomaticallyNext Topic: Log File Maintenance


Clear UNIX Spool Files Using Scripts

You can clear UNIX spool files periodically using the clearspool and deldirifempty (delete directory if empty) scripts.

  1. Create the clearspool and deldirifempty scripts.
  2. Schedule the clearspool script using CA WA server or run it manually.

    The clearspool script deletes files that meet certain modification time criteria. If the spool files are completely cleared, the clearspool script calls deldirifempty. The deldirifempty script deletes empty directories within the spool directory.

    If you run clearspool from a Telnet session, ensure you switch to the directory containing the spool files.

    If you used the defaults when installing the agent, the spool directory is called spool.

    The clearspool script assumes the spool directory is called spool in the current directory. If not, supply the full directory path name in the environment variable SPOOL.

Example: Create the clearspool and deldirifempty Scripts

The following are sample scripts. You can have other file maintenance procedures.

  1. Create a script called clearspool that contains the following code:
    #! /bin/ksh
    
      if [[ -z $SPOOL ]]
    
        then
    
          SPOOL=./spool
    
      fi
    
    find $SPOOL -type f -mtime +n -exec rm {} \;
    
    find $SPOOL -depth -type d -exec /bin/ksh /script_path/deldirifempty {} \;
    
    mtime n

    Specifies the age of the files to be deleted.

    • +n — deletes files last modified more than n days.
    • n — deletes files last modified exactly n days ago.
    • -n — deletes files last modified less than n days ago.

    Note: Put this script in the same directory as the cybAgent binary. Otherwise, specify the full path for SPOOL. You cannot specify a symbolic-linked directory for the SPOOL path.

  2. Create the script called deldirifempty that contains the following code:
    #!/bin/ksh
    
    Dir=$(ls -A $1)
    
     if [[ -z $Dir ]]
    
      then
    
       echo  "deleting directory $1"
    
       rmdir $1
    
      else
    
       echo "$1 is not empty"
    
     fi
    

Example: Deleting Files Modified Yesterday or Earlier

In the following example, mtime is specified as +1 to delete files that were last modified at least one day ago. The clearspool script then calls the deldirifempty script, which deletes any empty spool subdirectories.

#!/bin/ksh
  if [[ -z $SPOOL ]]
    then
     SPOOL=/AgentDirectory/spool
  fi
find $SPOOL -type f -mtime +1 -exec rm {} \;
find $SPOOL -depth -type d -exec /bin/ksh  /script_path/
deldirifempty {} \;

Note: $SPOOL cannot be symbolic-linked directories.