Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to timeout a Bash command and count the number of lines emitted to stdout?

Tags:

bash

I'd like to do something like timeout 12s tail -f access.log | wc -l but I'm not seeing the output from wc. What needs to be done to be able to do this?

like image 211
Noel Yap Avatar asked Apr 28 '17 20:04

Noel Yap


People also ask

How do I count the number of lines in a bash output?

wc. The wc command is used to find the number of lines, characters, words, and bytes of a file. To find the number of lines using wc, we add the -l option. This will give us the total number of lines and the name of the file.

How do I make bash script wait?

How to Use the Bash Sleep Command. Sleep is a very versatile command with a very simple syntax. It is as easy as typing sleep N . This will pause your script for N seconds, with N being either a positive integer or a floating point number.

What is timeout in shell script?

timeout is a command-line utility that runs a specified command and terminates it if it is still running after a given period of time. In other words, timeout allows you to run a command with a time limit.

How do you count words in bash?

Using wc command. wc command is used to know the number of lines, word count, byte and characters count etc. Count the number of words using wc -w. Here, “-w” indicates that we are counting words.


1 Answers

Use --foreground option with timeout:

timeout --foreground 12s tail -f access.log | wc -l

As per man timeout:

--foreground   when not running timeout directly from a shell prompt,
               allow COMMAND to read from the TTY and get TTY signals;
               in this mode, children of COMMAND will not be timed out
like image 153
anubhava Avatar answered Sep 29 '22 00:09

anubhava