Bash: close if IO pipe is not working

How to close a program if a thread is idle for a certain period of time?

Say for example:

someprogram | closeidlepipe -t 500 | otherprogram

Is there some kind of program closeidlepipethat can close if for a period of time ( -t 500)?)

timeout may close after a period, but not with an "unoccupied" difference.

UPDATE

It is important to note that it someprogramoutputs an endless stream of binary data. Data may contain a null character \0and must be transmitted over the protocol.

+4
source share
2 answers

Here is a common heart shape program that does this.

while(1) {
    struct timeval tv;
    tv.m_sec = 0;
    tv.m_usec = 500000;
    int marker = 1;
    select(1, &marker, NULL, NULL, &tv);
    if (marker == 0) exit(1);
    char buf[8192];
    int n = read(0, buf, 8192);
    if (n < 0) exit(2);
    char *b = buf;
    while (n)
    {
        int l = write(1, b, n);
        if (l <= 0) exit(3);
        b += l;
        n -= l;
     }
}
+1
source

read - -t.

someprogram |
  while :; do
    IFS= read -d'' -r -t 500 line
    res=$?
    if [[ $res -eq 0 || )); then
      # Normal read up to delimiter
      printf '%s\0' "$line"
    else
      # Either read timed out without reading another null
      # byte, or there was some other failure meaning we
      # should break. In neither case did we read a trailing null byte
      # that read discarded.
      [[ -n $line ]] && printf '%s' "$line"
      break
    fi
  done | 
  otherprogram

read 500 , while , . someprogram SIGCHLD , , .

+1

Source: https://habr.com/ru/post/1660840/


All Articles