Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

166
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda