signals - bash shell: Can a control-c cause shell to write an empty file? -
i have bash shell script. writes out text file. of works find if stop script control-c @ command level. file that's been written such as
echo "hello world" >myfile.txt
will end being empty. it possible when hit control-c stop shell script running caught @ instance it's opening write file , before puts in it, doesn't chance , leaves empty?
if that's case. can in bash shell script exit gracefully after it's written file , before gets chance write file again, because it's doing in while loop. thanks!
yes, it's possible end empty file.
a solution trap signal that's caused ^c (sigint
), , set flag can check in loop:
triggered=0 trap "triggered=1" sigint while true if [ $triggered = 1 ] echo "quitting" exit fi ...do stuff... done
edit: didn't realize though shell's own sigint handling trapped, still pass sigint subprocesses, , they'll killed if don't handle sigint themselves.
since echo
shell builtin, might survive killing, i'm not entirely sure. quick test seems work okay (file written, whereas without trapping sigint, end empty file well).
as @spbnick suggests in comments, on linux can use setsid
command create new process group subprocesses start, prevent them being killed sigint sent shell.
Comments
Post a Comment