Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

606
Views
/usr/bin/time of a bash script containing ampersands

lets say we have a bash script containing commands, some of which can have ampersand (&) symbols. I would like to measure the execution time of such a script using the /usr/bin/time, but because of the ampersands the actual work gets done in the background, while the script returns command to the shell, resulting in measurements equal to zero.

Is there a way to suppress the ampersand behaviour (i.e. not letting the commands be run in background)?

The reason why I'm trying to do this is because I want to do a large-scale benchmark (large amount of data sets, and more than several tools (some of which exhibit such behaviour)).

A concrete, very simple example would be as follows.

Create a script test.sh containing:

#! /bin/sh
sleep 2 &

And try running it with:

/usr/bin/time ./test.sh

The result is:

0.00user 0.00system 0:00.00elapsed 100%CPU (0avgtext+0avgdata 668maxresident)k
0inputs+0outputs (0major+219minor)pagefaults 0swaps

The expected result would be around 2 seconds (either cpu or elapsed time).

Please note that the script above is only a toy example. Also, suppose that it cannot be modified (look at it as a black box).

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Yes. Add a wait statement at the end of your script. This will cause that script to "wait" for the background tasks to finish.

over 4 years ago · Santiago Trujillo Report

0

This is a combination of answers from Elliott Frisch and Gilles (in the comments), and is intended to sum things up.

The answer is to create a wrapper script wrapper.sh that looks like:

#! /bin/sh
. $*; wait

The . (source command) will force the job to be executed in the same shell and not in the background, and wait will wait for all subprocesses to finish.

This can now be run as:

/usr/bin/time ./wrapper.sh ./test.sh

Please note that using the source command prevents you from changing the interpreter in test.sh's shebang line.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!