Path: csiph.com!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail From: Greg Wooledge Newsgroups: gnu.bash.bug Subject: Re: Protect Loop Execution with Traps Date: Tue, 28 Jan 2020 16:07:21 -0500 Lines: 59 Approved: bug-bash@gnu.org Message-ID: References: <20200128020322.GA31704@localhost4.local> <20200128134935.GM1350@eeg.ccf.org> <20200128204932.GB12574@localhost4.local> <20200128210721.GU1350@eeg.ccf.org> NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: usenet.stanford.edu 1580245673 11007 209.51.188.17 (28 Jan 2020 21:07:53 GMT) X-Complaints-To: action@cs.stanford.edu Cc: bug-bash@gnu.org To: Roger Envelope-to: bug-bash@gnu.org Mail-Followup-To: Roger , bug-bash@gnu.org Content-Disposition: inline In-Reply-To: <20200128204932.GB12574@localhost4.local> User-Agent: Mutt/1.10.1 (2018-07-13) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 139.137.100.1 X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <20200128210721.GU1350@eeg.ccf.org> X-Mailman-Original-References: <20200128020322.GA31704@localhost4.local> <20200128134935.GM1350@eeg.ccf.org> <20200128204932.GB12574@localhost4.local> Xref: csiph.com gnu.bash.bug:15830 On Tue, Jan 28, 2020 at 03:49:32PM -0500, Roger wrote: > As I slept on this, I realized the likeliness some programs are also trapping > CTRL-C as you just explained. > > The programs I'm using within a loop were ffmpeg && mv (rename) after verifying > ffmpeg created a file >0 bytes. I'm not familiar with ffmpeg in detail. An ffmpeg mailing list might be able to offer more focused advice. > But as I re-think think this, should be good Bash scripting practice to > integrate a trap within each loop? That doesn't sound right. Let me go back to my previous example using ping. Let's say that for reasons outside the scope of bug-bash, you're forced to perform a ping in a loop. And that you will almost certainly want to abandon the loop prematurely using Ctrl-C. And that you aren't allowed to fix the misbehaving ping command at the source code level. Here's a simple fix, that involves setting up ONE trap within the shell script, to override the shell's default SIGINT handling heuristic. #!/bin/bash trap exit INT while true; do ping -c 3 8.8.8.8 done There. Now, when I hit Ctrl-C, the whole script exits, not just one instance of ping. (Switching from -c 1 to -c 3 made it a *lot* less spammy, and also much harder to kill using the "press Ctrl-C twice really fast" approach. The INT trap works around it beautifully for me, though.) Whether this same stuff applies to ffmpeg, I have no idea. I also don't know why you're Ctrl-C'ing out of an ffmpeg loop often enough that this has become a concern. But, the "simple fix" that I used here has the same issue that ping itself has -- we're catching SIGINT and handling it and exiting, instead of letting SIGINT kill us. If something runs *us* in a loop, it'll have the same problem that *we* had when we tried to run ping in a loop. So, in the interest of not causing problems for other programs, here's the more correct fix: #!/bin/bash trap 'trap INT; kill -INT $$' INT while true; do ping -c 3 8.8.8.8 done