Save in a Variable The Number of Seconds a Process Took to Run

Save in a variable the number of seconds a process took to run

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.

You can also use the $SECONDS trick on the command line as well, for example:

$ 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.

Why does adding a sleep to a bash script seem to break variable decrementing for certain named variables?

SECONDS is reserved variable in shell. That's why you must always use lowercase or mixed case variables in your script and avoid using all uppercase variable names.

#!/bin/bash
let secs=5

until [[ "$secs" -le "1" ]]; do
echo SECONDS $secs
(( secs -= 1 ))

sleep 1
done

That gives expected output:

SECONDS 5
SECONDS 4
SECONDS 3
SECONDS 2

Doumentation:

SECONDS

Each time this parameter is referenced, the number of seconds since shell invocation is returned. If a value is assigned
to SECONDS, the value returned upon subsequent references is the number of seconds since the assignment plus the value
assigned. If SECONDS is unset, it loses its special properties, even if it is subsequently reset.

How do I measure duration in seconds in a shell script?

Using the time command, as others have suggested, is a good idea.

Another option is to use the magic built-in variable $SECONDS, which contains the number of seconds since the script started executing. You can say:

START_TIME=$SECONDS
dosomething
ELAPSED_TIME=$(($SECONDS - $START_TIME))

I think this is bash-specific, but since you're on Linux, I assume you're using bash.

Get time process takes to complete in seconds?

Stopwatch watch = new Stopwatch();
watch.Start();
//Do things
watch.Stop();
Text = watch.Elapsed.TotalSeconds.ToString();

How do I get time of a Python program's execution?

The simplest way in Python:

import time
start_time = time.time()
main()
print("--- %s seconds ---" % (time.time() - start_time))

This assumes that your program takes at least a tenth of second to run.

Prints:

--- 0.764891862869 seconds ---

Get program execution time in the shell

Use the built-in time keyword:


$ help time

time: time [-p] PIPELINE
Execute PIPELINE and print a summary of the real time, user CPU time,
and system CPU time spent executing PIPELINE when it terminates.
The return status is the return status of PIPELINE. The `-p' option
prints the timing summary in a slightly different format. This uses
the value of the TIMEFORMAT variable as the output format.

Example:

$ time sleep 2

real 0m2.009s
user 0m0.000s
sys 0m0.004s

How to wait in bash for several subprocesses to finish, and return exit code !=0 when any subprocess ends with code !=0?

wait also (optionally) takes the PID of the process to wait for, and with $! you get the PID of the last command launched in the background.
Modify the loop to store the PID of each spawned sub-process into an array, and then loop again waiting on each PID.

# run processes and store pids in array
for i in $n_procs; do
./procs[${i}] &
pids[${i}]=$!
done

# wait for all pids
for pid in ${pids[*]}; do
wait $pid
done

Bash script to calculate time elapsed

Either $(()) or $[] will work for computing the result of an arithmetic operation. You're using $() which is simply taking the string and evaluating it as a command. It's a bit of a subtle distinction. Hope this helps.

As tink pointed out in the comments on this answer, $[] is deprecated, and $(()) should be favored.

How to calculate time elapsed in bash script?

Bash has a handy SECONDS builtin variable that tracks the number of seconds that have passed since the shell was started. This variable retains its properties when assigned to, and the value returned after the assignment is the number of seconds since the assignment plus the assigned value.

Thus, you can just set SECONDS to 0 before starting the timed event, simply read SECONDS after the event, and do the time arithmetic before displaying.

#!/usr/bin/env bash

SECONDS=0
# do some work
duration=$SECONDS
echo "$(($duration / 60)) minutes and $(($duration % 60)) seconds elapsed."

As this solution doesn't depend on date +%s (which is a GNU extension), it's portable to all systems supported by Bash.



Related Topics



Leave a reply



Submit