[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Protect Loop Execution with Traps
From: |
Roger |
Subject: |
Re: Protect Loop Execution with Traps |
Date: |
Tue, 28 Jan 2020 16:25:51 -0500 |
User-agent: |
Mutt/1.10.1 (2018-07-13) |
>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.
Yes. That's not desirable then, as sweet and short as it is.
>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
Wow, " trap 'trap INT; kill -INT $$' INT " not easily readable for me.
A trap calling kill inside of a trap.
I'm thinking, put "trap INT; kill -INT $$" inside of it's own function, named
something like exit_recurs_loop, so the first line reads "trap exit_recurs_loop
INT"
I'll put these additional trap ideas into practice here whenever I'm doing
loops and see what happens.
This trap stuff has me pulling out my hair, as I likely do not know the details
of how programs use signals and how Bash interacts with such activity.
Thanks for your time!
--
Roger
http://rogerx.sdf.org/
- Protect Loop Execution with Traps, Roger, 2020/01/28
- Re: Protect Loop Execution with Traps, Greg Wooledge, 2020/01/28
- Re: Protect Loop Execution with Traps, Roger, 2020/01/28
- Re: Protect Loop Execution with Traps, Greg Wooledge, 2020/01/28
- Re: Protect Loop Execution with Traps,
Roger <=
- Re: Protect Loop Execution with Traps, Robert Elz, 2020/01/29
- Re: Protect Loop Execution with Traps, Greg Wooledge, 2020/01/29
- Re: Protect Loop Execution with Traps, Roger, 2020/01/29
- Re: Protect Loop Execution with Traps, Roger, 2020/01/29
- Re: Protect Loop Execution with Traps, Greg Wooledge, 2020/01/29