问题
I know my script is going to take more than 10 hours to run. Is there a way to log the time it starts and the time it ends ?
Does the time command just time the process or do I get the output of the process that I'm timing ?
回答1:
Use the time command (details):
time your_prog
If time does not fit for you, I would try to log the output of date (details) before and after the execution of your program, e.g.
date > log.txt; your_prog; date >> log.txt
Finally, you can also add some formatting (NOTE: inspired by Raze2dust's answer):
echo "started at: $(date)" > log.txt; your_prog; echo "ended at: $(date)" >> log.txt
回答2:
The time command shows how long your process runs:
$ time sleep 2
real 0m2.002s
user 0m0.000s
sys 0m0.000s
$
sleep 2 is just a simple process that takes 2 seconds.
To log the current time, use the date command.
回答3:
I am not sure I get your question. time <command> will give the time taken by <command>. If you want the actual start and end times to be printed as well, you can do:
echo "start time = $(date)"
time <command>
echo "end time = $(date)"
回答4:
At the beginning and ending your script you just need to have date commands which will log the information.
var1=`date`
echo "Starting of the script $var1" > timing_log.txt
<your code>
var2=`date`
echo "Ending of the script $var2" >> timing_log.txt
来源:https://stackoverflow.com/questions/18781264/how-to-log-the-time-taken-for-a-unix-command