Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

609
Vistas
/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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda