bash 将进程运行所需的秒数保存在变量中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/858594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Save in a variable the number of seconds a process took to run
提问by flybywire
I want to run a process in bash and save in an env variable the number of seconds it took to run. How would I do such a thing?
我想在 bash 中运行一个进程并将运行所需的秒数保存在一个环境变量中。我怎么会做这样的事情?
回答by Bryan Oakley
Are you wanting to put this code in your script, or do it from the process that starts the script?
您是想将此代码放入脚本中,还是在启动脚本的过程中进行?
For the latter, you can use the "time" reserved word and then parse what it returns to get how much time a script takes.
对于后者,您可以使用“时间”保留字,然后解析它返回的内容以获取脚本所需的时间。
If you want to do this from within a script you can set the variable SECONDS to zero, and each time thereafter that you reference that variable it will be updated to be the number of elapsed seconds. So, you can put "SECONDS=0" at the very start of your script, and whenever you need the elapsed time it will be in the SECONDS variable.
如果您想从脚本中执行此操作,您可以将变量 SECONDS 设置为零,此后每次引用该变量时,它都会更新为经过的秒数。因此,您可以将“SECONDS=0”放在脚本的最开始处,并且只要您需要经过的时间,它就会在 SECONDS 变量中。
You can also use the $SECONDS trick on the command line as well, for example:
您也可以在命令行上使用 $SECONDS 技巧,例如:
$ SECONDS=0; sleep 5 ; echo "that took approximately $SECONDS seconds"
The time reserved word and the SECONDS variable are both documented in the bash man page.
时间保留字和 SECONDS 变量都记录在 bash 手册页中。
回答by Farzy
This works in Bash, and also Zsh:
这适用于 Bash,也适用于 Zsh:
# Set time format to seconds
TIMEFORMAT=%R
# Time a process
PROC_TIME=$(time (insert command here >/dev/null 2>&1) 2>&1)
echo $PROC_TIME
- The first two redirections hide your process's output ">/dev/null 2>&1"
- The last redirect is needed because "time" prints the time on stderr
- 前两个重定向隐藏了进程的输出 ">/dev/null 2>&1"
- 需要最后一次重定向,因为“时间”在 stderr 上打印时间
回答by ephemient
Using GNU time,
使用GNU 时间,
\time -p -o time.log $COMMAND
and then read time.log.
然后阅读time.log。
(Use either \timeor command time, otherwise you'll be using Bash's timebuilt-in, which doesn't support these options.)
(使用\time或command time,否则您将使用 Bash 的time内置功能,它不支持这些选项。)
This will work even when $COMMANDprints to stderr (which would confuse Oli's answer), and keeps stdout/stderr (which Farzy's answer doesn't).
即使$COMMAND打印到 stderr(这会混淆 Oli 的答案),这也会起作用,并保持 stdout/stderr(Farzy 的答案没有)。
-o ...tells timeto send its output to a file rather than to stderr (as is the default), and -pgenerates the traditional
-o ...告诉time将其输出发送到文件而不是 stderr(默认情况下),并-p生成传统的
real 0.00
user 0.00
sys 0.00
rather than GNU time's default of
而不是 GNU 时间的默认值
0.00user 0.00system 0:00.01elapsed 8%CPU (0avgtext+0avgdata 0maxresident)k
80inputs+0outputs (1major+188minor)pagefaults 0swaps
回答by Nathan
Using $SECONDSwasn't working for me in a script run by cron(though it worked when I ran it directly; I used cronfor the first time today, so there could be some user error too). @Farzy's answer using TIMEFORMATworked but I didn't want to redirect the output from the timed command. Here's an alternative to $SECONDSif you're in a similar situation:
$SECONDS在运行的脚本中使用对我不起作用cron(尽管当我直接运行它时它起作用;我cron今天第一次使用,所以也可能存在一些用户错误)。@Farzy 的回答使用TIMEFORMAT有效,但我不想重定向定时命令的输出。$SECONDS如果您处于类似情况,这里有一个替代方案:
start=$(date +%s)
your_command
seconds=$(($(date +%s) - $start))
回答by Oli
Use the time command. Note that the bash version of time is not the same as /usr/bin/time. So you would have something like:
使用时间命令。请注意,bash 版本的 time 与 /usr/bin/time 不同。所以你会有类似的东西:
TIME=`/usr/bin/time --format="%e" your_command_here >/dev/null`
The format just pulls the "real" time value out. You would need to convert that from a string if you wanted to do anything more than display it.
该格式只是拉出“真实”时间值。如果您想做的不仅仅是显示它,则需要将其从字符串转换。
If you just want to export the string, use export.
如果您只想导出字符串,请使用export.

