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

164
Views
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 answers
Answer question

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 Report

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 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!