euglug.org

[Eug-lug] Unix ‘time’ command

작성자

카테고리:



Allen BrownWed Mar 30 15:01:20 PST 2005

The following does more than you are asking.  It is designed to
make rather large updates to half a dozen sites around the
world.  But I've trimmed out most of the sites.

It tracks the size of the database it is syncing.  That info
goes into a log file that is then rsynched to the various sites.

It keeps track of how long it has been running and aborts the
later updates if it has run to long.  The idea is that those
sites will be updated the next day.  This works for me since
I get significant updates once or twice a week.  In between the
updates are rather small.

As Larry pointed out, you need to use the non-builtin time
command.  In general you should ensure that your programs are
not replaced by functions or aliases without you intending this.
You can ensure this by quoting the name of the command.  I have
done this in the example below.


# file="/home/abrown/etc/virage"
# Allen Brown  date="20050208 13:54:44"

progname=${0}
who=<Email address deleted for posting>
dateformat="+%Y%m%d %H:%M:%S"
(
   echo "==================== LOG SIZE OF DATABASES. ==================="
   src=/usr/iclib/Virage
   tempfile=/tmp/viraged$$
   diskusagelog="${src}/DISKUSAGE.log"
   echo "$(\du -s "${src}"):     $(\date "${dateformat}")" > "${tempfile}"
   \cat "${tempfile}" >> "${diskusagelog}"
   \cat "${tempfile}"
   typeset -i timeacc=0 timelimit=3600
   \rm "${tempfile}"; echo ${timeacc} > "${tempfile}"
   timelimit=${timelimit}*14;    # 14 hours
   timeopts="--quiet -f %e -o ${tempfile}"

   function controlrsync
   {
     site="${1}"
     act="${2}"

     case "${act}" in
       run | RUN)
         timeacc=${timeacc}+$(\sed -e 's/\..*//' < ${tempfile})
         if [ ${timeacc} -gt ${timelimit} ]; then
           echo "This has run too long. ${timeacc}>${timelimit}"
           returns=1
         else
           echo "=============== ${site} $(\date "${dateformat}") ${timeacc}" \
                 "================"
           returns=0
         fi
         ;;
       skip | SKIP)
         echo "============ SKIPPING ${site} $(\date "${dateformat}")" \
                 "=============="
         returns=1
         ;;
       *)
         echo "${progname}: internal error." 1>&2
         returns=1
         exit 1
         ;;
     esac
     return ${returns}
   }; # controlrsync

   # ==========================================================================
   echo "===================== RSYNC THE VIRAGE DATABASES. ===================="
   # ==========================================================================

   exclude="--exclude=*~"
   rsopts="--delete -vrlHtpcz ${exclude}"
   # rdopts='-c -oremove -t 60'
   # rsopts="--dry-run ${rsopts}"; rdopts="-n ${rdopts}"; echo "*** Dummy Run ***"

   # --------------------------------------------------------------------------
   if controlrsync "Boise" RUN; then
     target="boi.agilent.com:/va/Libraries/Virage"
     rsyncpath="--rsync-path=/usr/local/bin/rsync"
     \time ${timeopts} \
       \rsync ${rsyncpath-} ${rsopts} ${src}/. ${target}
   fi

   # --------------------------------------------------------------------------
   if controlrsync "Another system" RUN; then
     target="anothersystem.agilent.com:/Libraries/Virage"
     rsyncpath="--rsync-path=/usr/local/bin/rsync"
     \time ${timeopts} \
       \rsync ${rsyncpath-} ${rsopts} ${src}/. ${target}
   fi

   timeacc=${timeacc}+$(\sed -e 's/\..*//' < ${tempfile})
   rm -f "${tempfile}"

   echo "============ done $(\date "${dateformat}") - ${timeacc} =============="
) 2>&1 \
   | \mail -s "${progname}: cron update $(\date "${dateformat}")" ${who}
-- 
Allen Brown
   work: Agilent Technologies      non-work: http://www.peak.org/~abrown/
         allen_brown at agilent.com	            abrown at peak.org
   If all economists were laid end to end,
   they would not reach a conclusion. ---George Bernard Shaw


Jim Beard wrote:
> Howdy folks,
> 
>     It's time for my bi-weekly simple(for some!) unix/linux question!  
> I'm working on a shell script to automate some back ups using rsync.  I 
> would like to know how long the rsync process takes to complete.  In 
> order to do this I was hoping to execute the command with 'time' and 
> then capture time's output.  The problem is that any output redirection 
> I append to the command (like ' > logfile.txt' gets applied to the 
> command, and not to time.  Time supposedly has an option for this, but I 
> have likewise not been able to get this to execute correctly.
> 
> I've tried:
> 
> time -o test.txt ls
> bash: -o: command not found
> 
> real    0m0.002s
> user    0m0.000s
> sys     0m0.000s
> 
> and
> 
> time ls -o test.txt
> ls: test.txt: No such file or directory
> 
> real    0m0.009s
> user    0m0.000s
> sys     0m0.010s
> 
> along with similar variations including >'s and 2>'s and 2>&1's all 
> sprinkled about.  Does anyone have any experience with this?
> 
> Jim Beard