How to Calculate Script Run Time [Duplicate]

Consider this shell redirect: telnet towel.blinkenlights.nl >> starWarz when it ends, the session will break, detach from host or something like that. I want to know how long the connection lasts. How might I determine this?

1

Sounds like you're looking for the time builtin:

$ help time
time: time [-p] pipeline
    Report time consumed by pipeline's execution.

    Execute PIPELINE and print a summary of the real time, user CPU time,
    and system CPU time spent executing PIPELINE when it terminates.

    Options:
      -p    print the timing summary in the portable Posix format

    The value of the TIMEFORMAT variable is used as the output format.

    Exit Status:
    The return status is the return status of PIPELINE.

It will print a report of the time taken by a command. For example:

$ time sleep 3

real    0m3.027s
user    0m0.000s
sys     0m0.000s

The "real" field is the actual time that passes, the same value you would have measured with a watch. So, you can simply do:

time telnet towel.blinkenlights.nl >> starWarz
0

Most of the shells has a builtin named time, bash has it too. It will show you the actual human time (real) a process takes with the CPU time the process spends on user space (user) and kernel space (sys).

So run the command perpending time:

time telnet towel.blinkenlights.nl >> starWar

At the end bash will show the time on STDERR like:

real    XmX.XXXs
user    YmY.YYYs
sys     ZmZ.ZZZs

Even if you are on a shell that does not have the time builtin you have the external time command /usr/bin/time from the time package. The usage is same.

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest