Reading with cat: Stop when not receiving data

linux read data from ttys0
linux flush serial port command line
bash serial port read/write
cat /dev/ttyusb0
how to write to ttys0
bash write to serial
linux read serial port command line

Is there any way to tell the cat command to stop reading when not receiving any data? maybe with some "timeout" that specifies for how long no data is incoming.

Any ideas?

There is a timeout(1) command. Example:

timeout 5s cat /dev/random

Is cat the only reliable command that can read data from serial , But the problem with cat is that, as it doesn't receive an EOF , it continues retrieving data. I've also tried head , read and tail . head -1 /dev/ttyUSB0  Category: Stop getting I.T. wrong! Top Ten Starter Points for Digital Learning. Posted on February 5, 2016 March 3, 2020 by lessonhacker_ug6qjw. 1. There is a time

cat itself, no. It reads the input stream until told it's the end of the file, blocking for input if necessary.

There's nothing to stop you writing your own cat equivalent which will use select on standard input to timeout if nothing is forthcoming fast enough, and exit under those conditions.

In fact, I once wrote a snail program (because a snail is slower than a cat) which took an extra argument of characters per second to slowly output a file (a).

So snail 10 myprog.c would output myprog.c at ten characters per second. For the life of me, I can't remember why I did this - I suspect I was just mucking about, waiting for some real work to show up.

Since you're having troubles with it, here's a version of dog.c (based on my afore-mentioned snail program) that will do what you want:

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/select.h>

static int dofile (FILE *fin) {
    int ch = ~EOF, rc;
    fd_set fds;
    struct timeval tv;

    while (ch != EOF) {
        // Set up for fin file, 5 second timeout.

        FD_ZERO (&fds); FD_SET (fileno (fin), &fds);
        tv.tv_sec = 5; tv.tv_usec = 0;
        rc = select (fileno(fin)+1, &fds, NULL, NULL, &tv);
        if (rc < 0) {
            fprintf (stderr, "*** Error on select (%d)\n", errno);
            return 1;
        if (rc == 0) {
            fprintf (stderr, "*** Timeout on select\n");

        // Data available, so it will not block.

        if ((ch = fgetc (fin)) != EOF) putchar (ch);

    return 0;


int main (int argc, char *argv[]) {
    int argp, rc;
    FILE *fin;

    if (argc == 1)
        rc = dofile (stdin);
    else {
        argp = 1;
        while (argp < argc) {
            if ((fin = fopen (argv[argp], "rb")) == NULL) {
                fprintf (stderr, "*** Cannot open input file [%s] (%d)\n",
                    argv[argp], errno);
                return 1;
            rc = dofile (fin);
            fclose (fin);
            if (rc != 0)

    return rc;

Then, you can simply run dog without arguments (so it will use standard input) and, after five seconds with no activity, it will output:

*** Timeout on select

(a) Actually, it was called slowcat but snail is much nicer and I'm not above a bit of minor revisionism if it makes the story sound better :-)

Why does "cat ttyUSB0" not produce output?, I think for serial devices you have to set the baud rate before they do anything. I'm not sure how to do that from the command line in order to get cat to work, but  A prospective study was therefore organized comparing patients with CHD to normal subjects. Both groups were free of symptoms suggesting arrhythmia, were not receiving cardiac medications, and showed no ectopic activity during a 3-min period of supine monitoring. A total of 331 males were selected, of these, 168 had CHD and 163 were normal.

Here is the code for timeout-cat:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

void timeout(int sig) {

int main(int argc, char* argv[]) {
        int sec = 0; /* seconds to timeout (0 = no timeout) */
        int c;

        if (argc > 1) {
                sec = atoi(argv[1]);
                signal(SIGALRM, timeout);
        while((c = getchar()) != EOF) {
        return EXIT_SUCCESS;

It does basically the same as paxdiablo's dog. It works as a cat without an argument - catting the stdin. As a first argument provide timeout seconds.

One limitation (applies to dog as well) - lines are line-buffered, so you have n-seconds to provide a line (not any character) to reset the timeout alarm. This is because of readline.


instead of potentially endless:

cat < some_input > some_output

you can do compile code above to timeout_cat and:

./timeout_cat 5 < some_input > some_output

Monitoring and Tuning the Linux Networking Stack: Receiving Data , Preparing to receive data from the network; Enable NAPI; Register an interrupt Tuning: Adjusting netdev_max_backlog to prevent drops; Tuning: Adjust the NAPI weight Driver calls into NAPI to start a poll loop if one was not running already. This device can read and write to 64bit memory addresses,  Congratulations to the winners of the 2016 Betty and Edgar Sweren Student Book Collecting contest! The annual competition, which is sponsored by the Friends of the Libraries and was endowed in 2007 by Betty and Edgar Sweren, recognizes the love of books and the art of shaping a thoughtful and focused book collection.

Try to consider tail -f --pid I am assuming that you are reading some file and when the producer is finished (gone?) you stop.

Example that will process /var/log/messages until finishes.

tail -f /var/log/messages --pid $! | ... do something with the output

Using cat with a serial port on Linux, I can run "echo 'a' > /dev/ttyUSB0" on (1) and receive it just fine on (2). I would think that would continuously read/write whatever data comes a crappy USB-​serial dongle on one end; one that does not actually wire one of  Telstra believes a new frontline consumer and small business support system will help resolve issues faster and prevent customers from “falling through the cracks like they may

Atlanta, First, children destined to be poor readers at the end of elementary school almost receiving intervention may not be among the most seriously disabled readers, Measures of sensitivity to rhyme ("Which word rhymes with cat: leg or mat? In some embodiments, the user data can also include, for purposes of delivering the breaking news content to the user, user device information (associated with the connected devices, as discussed below), including, but not limited to, device identifying information, voice/data carrier information, Internet Protocol (IP) address, applications

Identification and Assessment to Prevent , Kafka only provides a total order over records within a partition, not between different It isn't enough to just read, write, and store streams of data, the purpose is to Instead of stopping the Kafka Streams client with a fatal exception​, Kafka When receiving compressed messages, 0.10.0 brokers avoid recompressing the  Embodiments provide a method for recognizing a traffic sign from an input image. The method may include extracting image features from the input image; modifying the image features based on a predetermined image saliency map to determine modified image features; determining a plurality of traffic sign candidates by applying an adaptive boosting algorithm on the modified image features

Documentation - Apache Kafka, The return code of the read command is zero, unless an end-of-file character is This option has no effect if read is not reading input from the terminal or from a pipe. michel ~/test> cat #!/bin/bash # This script will test if you have Redirection can also occur in a script, so that it can receive input from a file, for​  8 Jul. Nursery World headline: “Schools bribed to use phonics, M.P’s report says”. Phonics test for five and six year olds. Posted by Learning about life and counselling together in commercialisation of childhood, Early Day Motion Reading Tests for six year olds, Early Day Motion Stop School League Tables for Five Year Olds, League tables for five year olds, Phonics testing for five and

Catching user input, And, often, when reading other tutorials and hookup guides, you will be Data, Stop, and Parity Bits - Each packet of data sent to and from the terminal has a that the sender is not sending data faster than the receiver can receive the data. This state of crisis is consistently blamed on the lack of funding for indigent defense nationwide. Although there is a need for more data on this issue, the GAO reported on how federal Department of Justice funds were being used in 2012: only 2.45 percent of $13 billion, disbursed to states between 2005 and 2010, was used for indigent defense.