0

I have this line:

timeout -s SIGKILL 55s minicom -C $file.txt

It works fine when I run it directly from the terminal (even with the timeout) but when I run it from the bash script, it creates the files, but they're all empty.

aynber
  • 22,380
  • 8
  • 50
  • 63
Ahmed
  • 184
  • 1
  • 2
  • 14
  • 1
    SIGKILL is a hard enough shutdown that it stops a program from flushing its output -- anything that's still in the memory buffer is lost. Use SIGTERM instead. – Charles Duffy Sep 26 '22 at 14:58
  • 1
    The reason this doesn't happen from a terminal is liable to be a difference in behavior based on `isatty()` -- terminal stdout is line-buffered by default, whereas stdout going to regular files is fully buffered (so it's written in larger chunks to reduce round-trips into kernelspace). – Charles Duffy Sep 26 '22 at 14:59
  • Didn't work with SIGTERM either. Is there a way I can change the buffering behavior so it writes as it goes? – Ahmed Sep 26 '22 at 15:28
  • If a program doesn't override libc's defaults, and the local platform is Linux, you can use `stdbuf` to configure the defaults it uses. That said, if switching to SIGTERM doesn't help, it may not in fact be a buffering problem. – Charles Duffy Sep 26 '22 at 15:32
  • That said, one other thing you can try to test whether it's a problem related to where stdin and stdout are attached is to use `unbuffer` or other tools that emulate a TTY. – Charles Duffy Sep 26 '22 at 15:33
  • ...where I'd be going next, though, is using syscall-tracing tools (strace, sysdig, etc) to log how the program executes and see where it's getting stuck -- including whether it's _trying_ to write output at all. – Charles Duffy Sep 26 '22 at 15:34
  • After debugging I realized that the kill isn't the problem. When not running from the script, the contents are already in the file before it gets killed. Not sure how to proceed. – Ahmed Sep 27 '22 at 15:49
  • I still recommend using sysdig or similar to trace execution. – Charles Duffy Sep 27 '22 at 15:54

0 Answers0