Moumou
July 29, 2009, 4:50am
1
Hi,
I have a simple question. How can I get the execution time of a script and maybe put it in a variable?
Another question. How can I get only time and not date and put it in a variable? I tried something with "date" command but with no success...
If someone could help me...
aigles
July 29, 2009, 5:51am
2
$ script_times=$(/usr/bin/time -p my_script 3>&2 2>&1 1>&3 3>&-)
$ echo "$script_times
real 0.01
user 0.00
sys 0.01
$
$ current_time=$(date +%T)
$ echo $current_time
11:26:20
$ current_time=$(date +'%H.%M.%S')
$ echo $current_time
11.27.50
$
Jean-Pierre.
Moumou
July 29, 2009, 6:16am
3
ok, thx a lot for your answer.
Another question : what is the difference between real time, user time and system time?
And I just realized that if forgot a part of my question previously so I ask it now :
In fact i run a script which is calling other scripts. And what i'd like to do is to print at the end of each children script something like
Thx for your answers
aigles
July 29, 2009, 7:52am
4
You can do something like that :
elapsed()
{
(( seconds = SECONDS ))
"$@"
(( seconds = SECONDS - seconds ))
(( etime_seconds = seconds % 60 ))
(( etime_minuts = ( seconds - etime_seconds ) / 60 % 60 ))
(( etime_hours = seconds / 3600 ))
(( verif = etime_seconds + (etime_minuts * 60) + (etime_hours * 3600) ))
echo "elapsed time: ${etime_hours}h${etime_minuts}m${etime_seconds}s"
}
elapsed sleep 67
Output:
elapsed time: 0h1m7s
Jean-Pierre.
Moumou
July 29, 2009, 10:31am
5
Thx a lot Jean Pierre,
I tested it and it works.... in bash shell. And i need it for a csh script and here it doesn't work (snif..)
When I just copying the script it send me this error :
and when I remove the first (), it worst... :
I'll try to find out something by myself, but if you have any suggestion, i'll appreciate it!
And sorry not to tell what kind of shell i was writting at the beginning.... o
Regards
Aswex
July 30, 2009, 4:54am
6
Hi,
I don't know if I can ask, but I found this function very interesting..
elapsed()
{
(( seconds = SECONDS ))
"$@"
(( seconds = SECONDS - seconds ))
(( etime_seconds = seconds % 60 ))
(( etime_minuts = ( seconds - etime_seconds ) / 60 % 60 ))
(( etime_hours = seconds / 3600 ))
(( verif = etime_seconds + (etime_minuts * 60) + (etime_hours * 3600) ))
echo "elapsed time: ${etime_hours}h${etime_minuts}m${etime_seconds}s"
}
At first time I didn't try to make it work, but I was wrong. In fact each time I am using this function on my script I get
elapsed time: 0h0m0s
Do you mind to tell me how to make it work correctly ? can you please help me ?
Thanks
Moumou
July 30, 2009, 5:44am
7
How are you using this function?
if you are doing something like this :
code code
code code
code code
elapsed
I understand why it doesn't works. You have to specify on which script you want to apply elapsed.
e.g :
elapsed yourscript.sh
Jean-Pierre used "sleep 67" as an example.
Regards
Aswex
July 30, 2009, 6:09am
8
Dear Moumou,
You're right. I didn't see this fonction on that way. As I am aleady using a script that needs an input file, I can't using like this :
elapsed myscript.ksh input_file
do you know a way to do the same thing on my script ?
I am looking to monitor the execution time of myscript.ksh
Thanks
Moumou
July 30, 2009, 6:27am
9
You can use this function even with a script who needs parameters.
Indeed, in the example of "sleep 67", "67" is a parameter.
I use it withe a script who has 10 parameters and it works very well!
Regards