fatal error: cannot open file – does awk flush output when interrupted by SIGINT?
0 reputation · 23 Jan 2023, 21:52 UTC
0 reputation · 23 Jan 2023, 21:52 UTC
Investigate whether GNU Awk flushes partially written data to stdout or output files when it receives a cancellation signal such as SIGINT. The documented error string "fatal error: cannot open file" is used as a reliable anchor for parsing error logs, but it does not describe signal handling behavior.
Awk has no built‑in timeout mechanism; external utilities like timeout or shell traps are required to stop long‑running scripts. POSIX does not specify how awk should behave on signal delivery, and implementations differ: some terminate immediately without flushing, while others may ignore the signal unless explicitly handled. The lack of a documented graceful‑shutdown API leaves data integrity uncertain in pipelines that rely on complete output.
SIGINT, does it automatically flush any data remaining in its output buffers to stdout or open files?35275 reputation · 24 Jan 2023, 07:28 UTC
No. GNU Awk does not flush remaining output buffers when it receives SIGINT. The default signal disposition terminates the process immediately, so any data still in user-space stdio buffers—whether bound for stdout or a redirected file—is typically lost. The error string fatal error: cannot open file is unrelated to this behavior; it indicates a file-open failure, not a signal path.
Well-established: POSIX does not specify awk signal semantics, and gawk has no documented configuration or extension that installs a graceful SIGINT handler. The C runtime flushes stdio streams during normal exit(), but not when a signal kills the process with the default action.
Uncertain: whether a particular gawk release or non-GNU variant ever flushed on SIGINT, and whether the operating system performs any post-mortem cleanup. These details are platform-dependent. Because the underlying research flags current verification as required, treat version-specific claims as provisional until you test on your target system.
To ensure records survive interruption, flush explicitly after critical writes:
gawk '{ print > "out.txt"; fflush("out.txt") }'If you cannot modify the awk script, write to a temporary file and rename it from a shell trap. This only works when the shell—not the awk process directly—receives the signal:
tmp=out.tmp
trap 'mv -f "$tmp" out.txt' INT TERM
gawk '{ print > "'"$tmp"'" }'
mv -f "$tmp" out.txtConfirm the behavior on your system with a minimal test. Start a gawk process that writes without flushing, send SIGINT, then inspect the file:
printf 'a\nb\nc\n' | gawk '{ print > "out.txt"; system("sleep 5") }' &
pid=$!
sleep 1
kill -INT "$pid"
wait "$pid"; echo "exit=$?"If out.txt is empty or incomplete, buffers were not flushed. Repeat with fflush() after each print to verify the fix. Record gawk --version and your OS, because other awk implementations (mawk, nawk, BusyBox awk) may differ.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.