LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-14-2007, 01:40 PM   #1
aubrey-calm2
LQ Newbie
 
Registered: Feb 2006
Location: Alabama
Distribution: Slackware 10.2/11.0
Posts: 23

Rep: Reputation: 15
time a process within bash script


I'm trying to write a bash script that will copy files from old_server to new_server. I need to copy certain directories first (please don't ask why) and certain directories last.
I want to time each of these copies. I am currently using this configuration....

# First we sync the clock and record the time
/etc/rc.d/rc.clock sync
BEGIN_TIME="$(date +%A%l:%M%P)"

# Next we set the clock to zero and make that our begin time. Used for report
date 010100002000 >/dev/null 2>&1

# Begin the actual copy-over process
echo -e "\n\n"
echo Began copying ACSS to /$DEST/APPS/ACSS/
BEGIN_ACSS="$(date +%s)"
cp -rf $ACSS/ /$DEST/APPS/
END_ACSS="$(date +%s)"
echo -e "\n\n\n"


echo Began copying APPS/* without ACSS to /$DEST/APPS/
BEGIN_APPS="$(date +%s)"
cp -rf $APPS/*[!ACSS] /$DEST/APPS/
END_APPS="$(date +%s)"
echo -e "\n\n\n"

echo Began copying SOURCE/* without APPS or ACS to /$DEST/
BEGIN_SOURCE="$(date +%s)"
cp -rf $SOURCE/*[!APPS][!ACS] /$DEST
END_SOURCE="$(date +%s)"
echo -e "\n\n\n"
mkdir /$DEST/ACS

# Resync clock to reality
/etc/rc.d/rc.clock sync >/dev/null 2>&1
FINISH_TIME="$(date +%A%l:%M%P)"

# --Show time spent--
# first build the variables
ACSS_TOTAL="$(expr $END_ACSS - $BEGIN_ACSS)"
APSS_TOTAL="$(expr $END_APPS - $BEGIN_APPS)"
SOURCE_TOTAL="$(expr $END_SOURCE - $BEGIN_SOURCE)"
ACSS_TOTAL_MIN="$(expr $ACSS_TOTAL / 60)"
APPS_TOTAL_MIN="$(expr $APSS_TOTAL / 60)"
SOURCE_TOTAT_MIN="$(expr $SOURCE_TOTAL / 60)"

echo -e "\n\n\n\n\n\n"
echo This job started $BEGIN_TIME; echo
echo This job ended $FINISH_TIME; echo
echo Copying $ACSS took $ACSS_TOTAL_MIN minutes.; echo
echo Copying $APPS took $APSS_TOTAL_MIN minutes.; echo
echo Copying $SOURCE took $SOURCE_TOTAL_MIN minutes.; echo -e "\n\n"
echo This script saved you "$(expr $ACSS_TOTAL_MIN + $APPS_TOTAL_MIN + $SOURCE_
TOTAT_MIN)" minutes!

The reason that I set the clock to 12:00am is because the copy-over has to start usually around 11:30pm, and takes around 3 hours to complete. The problem is that during this time the clock for the system is wrong.

Is there any way to do this without using the system clock?

-=Aubrey=-
 
Old 06-14-2007, 02:28 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
There are several ways to do this. I'm sure several people will be along to show you their favorite ways. Pay heed to each, and you'll learn some new stuff!

My favorite:

Code:
#!/bin/bash

# Instead of the sleep commands, substitute your cp commands.

cat > temp.sh <<EOD
/usr/bin/time -f '%e' -o temp1.dat sleep 5
/usr/bin/time -f '%e' -o temp2.dat sleep 10
/usr/bin/time -f '%e' -o temp3.dat sleep 15
EOD

chmod 700 temp.sh

/usr/bin/time -f '%e' -o temp0.dat ./temp.sh

echo first command took $(cat temp1.dat) seconds
echo second command took $(cat temp2.dat) seconds
echo third command took $(cat temp3.dat) seconds
echo the total is $(cat temp0.dat) seconds

rm temp.sh temp*.dat
Hope this helps.
 
Old 06-15-2007, 01:39 PM   #3
aubrey-calm2
LQ Newbie
 
Registered: Feb 2006
Location: Alabama
Distribution: Slackware 10.2/11.0
Posts: 23

Original Poster
Rep: Reputation: 15
That worked flawlessly. Thanks.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to check for dead process MaffooClock Other *NIX 12 05-18-2007 05:14 AM
How to stop a real time process in a script susee_sundar Linux - Software 4 05-30-2006 07:20 AM
How to create a bash script to automatically disown a process. jon_k Linux - Software 5 06-19-2005 05:53 AM
how do i kill a process from inside a bash script? mikaelo Programming 4 05-28-2004 08:51 AM
BASH script inform user and kill process mounters Programming 3 02-11-2002 04:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration