-2

I want to write a script in bash that will save to file how long it have been executed

I want output to look like this: 1 minute 2 minute ...

jajoś
  • 1
  • The goal is that you add some code of your own to show at least the research effort you made to solve this yourself. – Cyrus Aug 20 '17 at 16:24
  • You will need to describe in more detail your environment as well as the work you have done so far as suggested above. You may also look to search for the solution using Google as this is a VERY easy task to complete with just a bit of research. – DropHit Aug 20 '17 at 16:40
  • This link will help you [https://stackoverflow.com/questions/385408/get-program-execution-time-in-the-shell] – Guardian Aug 20 '17 at 16:51
  • 1
    Possible duplicate of [Get program execution time in the shell](https://stackoverflow.com/questions/385408/get-program-execution-time-in-the-shell) – Samy Aug 20 '17 at 20:17
  • Use command 'time [your_command]', it will count exactly the executation time of command – Bui Anh Tuan Aug 21 '17 at 00:13

1 Answers1

0

Requirements

You need to install time (not the shell built-in).

To validate that you have the right one: $ which time

This is the expected output:

$ which time
/usr/bin/time

Solution

Assuming that you have a function called main, in which your main scripting code is included

function main() {
  echo "Sleeping .."
  sleep(5)
  echo "This is the first arg: ${1}"
  echo "This is the second arg: ${2}"
}

To time this function call, do as follows (arguments for reference):

main "HELLO" "WORLD" | $(which time) -o "OUTPUT_FILENAME_FOR_TIME" -f "%e" $(which bash)

Explanation

We are piping /usr/bin/time to the function call to time it. We are calling time using $(which time) because we do not want the shell built-in. After that we are passing the -o argument to define our output file and then the -f argument is used to define the time format. In my example, I used seconds. In the end, we pass which shell we are using, and in our case we are using bash, so $(which bash).

man time to read about other formats and of course the proper usage of the program

I always use seconds because it is easier to convert them to anything.

EDIT #1

You can use the GNU tool command instead of using the absolute path of time

 $ command time

instead of

 $ $(which time)
Community
  • 1
  • 1
xtonousou
  • 569
  • 5
  • 16
  • What is the advantage of using /usr/bin/time over the shell builtin? – user1934428 Aug 21 '17 at 09:26
  • @user1934428 You can read [this](https://unix.stackexchange.com/questions/70653/increase-e-precision-with-usr-bin-time-shell-command/70655#answer-70655) – xtonousou Aug 21 '17 at 15:57
  • Hmmm.... while the article is interesting, I somehow miss the point. It is true that the article says in one sentence *The GNU time, /usr/bin/time, is usually more useful than the built-in*, but it does not give any explanation, in what respect it is more useful. Instead it explains in detail (and very well), that the built-in (a.k.a. *bash time*) gives a **higher** precision than gnu time. This would rather be an argument in favor to bash time (but doesn't apply to the problem here, because the OP needs only a resolution to the minute). – user1934428 Aug 22 '17 at 05:16
  • 1
    @user1934428 From my experience on `time` commands usage: to write time log files with the builtin `time` you have to redirect the stdout and stderr to the file (`somecommand &> time.log` ). In this case you have precision but if `somecommand` produces any error, this will be written to your time.log something you don't want to happen. On the other hand, the `/usr/bin/time` is less precise but you can control the output by arguments, format output using a format string that can be specified using the -f option or the TIME environment variable and many more. – xtonousou Aug 22 '17 at 17:34
  • 1
    @user1934428 I am commenting once more (no characters left). *NOTE: less precise, about `0.0X` seconds, where `X` is an integer.* Moreover, to convert seconds to minutes, one can use **BASH maths: `VAR=$(( X - Y ))`**, the `bc` command, or something like `python`. – xtonousou Aug 22 '17 at 17:42
  • [More info on how to do integer and float calculations](https://unix.stackexchange.com/questions/40786/how-to-do-integer-float-calculations-in-bash-or-other-languages-frameworks#answer-40787) – xtonousou Aug 22 '17 at 18:06