2

I am trying to write a bash script that launches n commands in the background and then waits for each command to finish. I can easily get the time it took to execute all the commands using time sh myscript.sh.
But I want to get the execution time for each command.

So lets say I have two commands and I launch them in my script like so:

echo "1" &
echo "2" &
wait

Now to time each command I have tried the following:

time echo "1" 1> /dev/null &
time echo "2" 1> /dev/null &
wait

But that seems to only print the overall time of the script (or something similar to it).

Am I going about this the wrong way?

  • That should work and you should get two outputs of time when starting your script with myscript.sh. Test with sleep 5s and sleep 10s instead of echo.... – Thomas Jan 11 '17 at 17:54
  • 1
    "that seems to only print the overall time" => Give us the output in your question, and tell us why you are not satisfied with it – xhienne Jan 11 '17 at 17:55
  • You can use your own wrapper command for timing command execution -- unix.stackexchange.com/a/334152/64411 – Rabin Jan 11 '17 at 19:43
  • @Thomas you are right. Sorry maybe I was looking at something weird. Thanks. – Zaid Jan 11 '17 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.