Determine amount of time to process


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Determine amount of time to process
# 1  
Old 10-10-2010
Determine amount of time to process

Hello all,

Hopefully someone can point me in the right direction...
I have a script written in bash which is pretty basic and just stop/starts various services based on particular conditions. What I am trying to build is a reporting type function which will send out an email with various stats and one of which I would like to include is the amount of time it took for the various condition to complete..

My initial thoughts were to assign the current system date to a variable at the beginning of the condition and then once complete assign the date to a new variable, compare the difference and then I would have my answer...

Does anyone have any sample script that would work in this situation or know of a better way to attempt this?

Thanks
# 2  
Old 10-10-2010
Here is a PERL example, from one of my scripts:

perl code:
  1. $mtime = microtime();
  2. $mtime = explode(' ', $mtime);
  3. $mtime = $mtime[1] + $mtime[0];
  4. $starttime = $mtime;
  5. // your system commands / work here
  6. $mtime = microtime();
  7. $mtime = explode(" ", $mtime);
  8. $mtime = $mtime[1] + $mtime[0];
  9. $endtime = $mtime;
  10. $totaltime = ($endtime - $starttime);
  11. //
  12. // do some more stuff
  13. //
  14. $to = "me@whereiam.com";
  15. $subject = "My Subject";
  16. $body = "Process executed in " .$totaltime. " seconds.";
  17. $header = "From: me @ whereiwas . com]\r\n";
  18. $header .= "Return-Path: me @ whereiwas . com\r\n";
  19. $header .= "Reply-To: me @ whereiwas . com\r\n";
  20. $success = mail($to, $subject, $body, $header);
  21. //
  22. // email addresses with spaces added
# 3  
Old 10-10-2010
Hello, systrex:

Perhaps the `time` command may be of use.

Regards,
Alister
# 4  
Old 10-11-2010
How about the bash/ksh env variable $SECONDS?

Here is some code to get into human readable form, just call duration_str with number of seconds eg:

Code:
START=$SECONDS
...My command to time....
duration_str $(($SECONDS-$START))

Code:
plural()
{
 # usage: plural <value> <word>
 [ $1 -eq 1 ] && echo $1 $2 || echo $1 ${2}s
}

duration_sub()
{
  # Input <duration> <A-div> <A-desc> <B-div> <B-desc>
  let DUR_A=$1/$2
  let DUR_B=$1-DUR_A*$2
  let DUR_B/=$4
  echo $(plural $DUR_A $3) $(plural $DUR_B $5)
}

duration_str()
{
 # Input param1 is duration in seconds
 # Outputs human readable string to stdout

 DUR=$1
 [ $DUR -le 60      ] && echo $(plural $DUR second) && return
 [ $DUR -le 3600   ] && duration_sub $DUR    60 minute    1 second && return
 [ $DUR -le 86400  ] && duration_sub $DUR  3600 hour     60 minute && return
 [ $DUR -le 604800 ] && duration_sub $DUR 86400 day    3600 hour   && return
 # Older than a week
 duration_sub $DUR 604800 week 86400 day
}


Last edited by Chubler_XL; 10-11-2010 at 12:41 AM.. Reason: typo
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run a command for specific amount of time with an auto key press

Hi, I have been trying to do a small fun project for myself. I want to run a command for 45 seconds. And to get the final output of this command, the script requires I push the "q" key on my keyboard and then the final output file becomes available. I tried the following script. But it... (12 Replies)
Discussion started by: jacobs.smith
12 Replies

2. Shell Programming and Scripting

How to determine the completion of a background process to trigger something else?

I've been thinking about a peculiar problem, and so far haven't been able to find out a convincing solution to that. To put it simply, I have a shell script (assume it to be parent_script.sh), calling another shell script (child_script.sh) 5 times, in nohup mode to be executed in the background.... (3 Replies)
Discussion started by: Aviktheory11
3 Replies

3. UNIX for Dummies Questions & Answers

How to determine nfs mount time

Hi There are numerous mounts on my local system from various other remote machines. I was looking for some way to determine the time stamp when the drives are mounted. like say if there is a mount name backup on local system. I want to determine the timestamp when it was mounted. any... (3 Replies)
Discussion started by: rakeshkumar
3 Replies

4. UNIX for Advanced & Expert Users

Kill a process after a certain amount of time

I would like to kill a process after a certain amount of time. Can I please get some ideas on how to do this? (9 Replies)
Discussion started by: cokedude
9 Replies

5. Shell Programming and Scripting

Determine previous time in minutes

I have several logs with where the time stamp in the logs are "YYYYMMDDHHMM". I would like to check the last line in each file to make sure the entry is less than 5 minutes old. My timezone is EST5EDT so the following will work for 1 hour. But I need something easy for 5 minutes ago.... (5 Replies)
Discussion started by: oldman2
5 Replies

6. UNIX for Dummies Questions & Answers

Script to determine logged in time

Hi all So I am thinking my inability to cope with math is bogging me down here so Im asking for help. I want to determine how long a user has been logged on for by using the date and who commands to determine the time they have been logge don. My problem is that I keep getting the wrong... (2 Replies)
Discussion started by: losingit
2 Replies

7. UNIX for Dummies Questions & Answers

determine total memory used by some user/process

HI guys, :confused:i would like to know how can i determine the total/approx memory used by a single user. Example Top output is below =========================================================================== top - 20:00:50 up 24 days, 2:48, 2 users, load average: 0.43, 0.40, 0.37... (3 Replies)
Discussion started by: cromohawk
3 Replies

8. AIX

How to determine what process is actively using my harddisk?

Hi, Is there any command that I can issue to check who is actively using my harddisk? I notice that yesterday the hdisk0 and hdisk1 is really actively being used and is reaching almost 100%. I realized that this is because of paging which is climbing up to 70%. However, just this morning... (3 Replies)
Discussion started by: depam
3 Replies

9. UNIX for Dummies Questions & Answers

Determine which file is invoking process?

Hey Guys, I am trying to figure out what is chewing up a bunch of CPU on our SunFire V120 Boxes and I am having a little trouble drilling down the source. When I check the CPU usage it displays tail & cat as the top two processes PID USERNAME SIZE RSS STATE PRI NICE TIME CPU... (1 Reply)
Discussion started by: Jerrad
1 Replies

10. Shell Programming and Scripting

executing a script for a certain amount of time

I am writing a script that takes two parameters: the name of another script and an integer that represents a number of seconds. The script must execute the second script (first parameter) for the specified number of seconds (second parameter), suspend it for the same number of seconds, and continue... (9 Replies)
Discussion started by: ponchorage
9 Replies
Login or Register to Ask a Question