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

165
Vistas
resource linux script, disable/flush extra input

Good afternoon everyone,

I have created a resource monitoring tool that works fairly well.

  • Pulls CPU average usage
  • Pulls average Memory usage
  • Even calculates % of NIC throughput (if you have a 1Gb NIC, it'll show percent that is being processed at a time)... and yes I know this is more of a rough estimate/theoretical max limit of a NIC.

I am having one issue with my script though. Portion of the code I am experiencing my issue with is below (I converted some to pseudo code for simplicity).

COUNT=1
read -rsp "When you are ready to begin, please press any key" -n1
echo "processing"
sleep 3
while [ ${COUNT} = "1" ; do
  read -t 1 -n 1
  if [$? = 0 ] ; then
    exit 0
  else
 `Resource command` > ${cpulog} file for future graphs
 `Resource command` > ${memlog} file for future graphs
 `Resource command` > ${network} log file for future graphs
 `etc`
 fi
done

Basically, you hit any key to start the program, and whenever you press any key on the keyboard after the program has started (While loop), the program stops recording information and moves on.

Now this script works and does everything I need it to do. The issue I have come across is when you "press any key".

Note that there are two points in the script waiting for a key press.

If I were to press any key more than once at the first point, the second key input would get processed by my read -t 1 -n 1 command (at the second point), and thus fail to run my resource pulls. Since that happens immediately, the script fails.

Basically, I am trying to figure out if there is a way I can shutdown input after that first key stroke for a limited time while I retrieve a limited amount of data, or flush any input that was given prior to hitting my read -t 1 command. Thank you.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

The script can call a small program, written in C, perl or similar, which calls the FIONREAD ioctl on stdin.

Then read the unexpected extra characters to be thrown away with a read call, see Perl Cookbook to determine unread bytes You can actually enter the perl code on command line with perl -e. to keep it all within the bash script.

over 4 years ago · Santiago Trujillo Denunciar

0

At the "processing line," add the following loop:

# Eat any remaining input
while read -t 1 -n 1
do
   # Do nothing here
   :
done

# Continue processing now that all input has been consumed...

It will add about 1 second delay to startup (more if the user is sitting there pressing keys), but otherwise does what you want.

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