Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am trying to fix this error I keep seeing, and I'm not sure why I'm seeing them. The script generates a list of files older than 7 days, then tars/gzips them into a backup directory. Finally it removes the files once the backup has completed successfully. I am encountering a weird error. The script is simple, most of it is to prevent the script from running twice or hanging, or it handles logging. The important code is between the asterisks.

#!/bin/bash
#
#
#       Title:          fwdCleanup.sh
#       Author:         Matthew Sarro
#       Date:           03/07/2012
#       Desc:           Backs up all files down the tree which are
#                       older than 7 days
#
#
#
#
DATE=`date +%F-%H%M`

LOGFILE=/root/testing/fwdcleanuplogs/$DATE.log
exec 3<>$LOGFILE
exec >&3 2>&3

#define the name of the lockfile
LOCKFILE='/root/testing/fwdcleanup.pid'

# check for existing lockfile
if [ -e "$LOCKFILE" ]; then
# lockfile exists
        #check if file is readable
   if [ ! -r "$LOCKFILE" ]; then
      echo error: lockfile is not readable
      exit 1
   fi

#define PID as the process number in lockfile
   PID=`cat "$LOCKFILE"`
# check if process is signal-able and redirect stderr to /dev/null
   kill -0 "$PID" 2>/dev/null
#if process was signal-able echo an error
   if [ $? == 0 ]; then
      echo error: existing instance of this task is already running
      exit 1
   fi

# process that created lockfile is no longer running - delete lockfile
   rm -f "$LOCKFILE"
#check if lockfile deleted properly
   if [ $? != 0 ]; then
      echo error: failed to delete lockfile
      exit 1
   fi
fi

# create lockfile containing current process ID
echo $$ >"$LOCKFILE"
if [ $? != 0 ]; then
   echo error: failed to create lockfile
   exit 1
fi

#************************
#EDIT IN HERE
#find all files older than 7 days and create a file list
find /root/testing/{MSP,CEMP} -mtime +7 -type f > /root/testing/fwdcleanuplogs/$DATE.list

#add all files from the file list into a tar.gz file
tar -czf /root/testing/backups/$DATE.tar.gz --files-from /root/testing/fwdcleanuplogs/$DATE.list

#delete all files from the file list off disk
rm -f `cat /root/testing/fwdcleanuplogs/$DATE.list`
*************************

# delete lockfile
rm -f "$LOCKFILE"
if [ $? != 0 ]; then
   echo error: failed to delete lockfile
   exit 1
fi

exec 3>&-
exit 0

When I run the script, the following shows up in the generated log:

./fwdCleanup.sh: line 67: backups: command not found

I have no idea why this is showing up - I can't seem to figure it out. The code still executes brilliantly, but the error is driving me batty. I can't see anything in line 67 that would even remotely tell the OS that backups is a command.

Any advice would really be appreciated.

Edit: Before anyone notices: this is being done on a test VM, hence the usage of the root account :)

share|improve this question

2 Answers 2

up vote 4 down vote accepted

Your line 67 is ************************* which should really have a # at the beginning. The first * is being expanded by your shell and is picking up the file called backups in the current directory.

share|improve this answer
    
GAH! thank you so much, I can't believe I missed that. Very much appreciated –  Matthew Mar 7 '12 at 20:44
1  
+1 because I can relate –  rlduffy Mar 7 '12 at 21:08

Use the -x option to bash to get a trace of what the script is doing. That'll give you a pretty good idea of where things are going bad.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.