Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #15828

Re: Protect Loop Execution with Traps

Path csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail
From Roger <rogerx.oss@gmail.com>
Newsgroups gnu.bash.bug
Subject Re: Protect Loop Execution with Traps
Date Tue, 28 Jan 2020 15:49:32 -0500
Lines 81
Approved bug-bash@gnu.org
Message-ID <mailman.262.1580244577.2185.bug-bash@gnu.org> (permalink)
References <20200128020322.GA31704@localhost4.local> <20200128134935.GM1350@eeg.ccf.org> <20200128204932.GB12574@localhost4.local>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="a8Wt8u1KmwUX3Y2C"
X-Trace usenet.stanford.edu 1580244578 10571 209.51.188.17 (28 Jan 2020 20:49:38 GMT)
X-Complaints-To action@cs.stanford.edu
To Greg Wooledge <wooledg@eeg.ccf.org>, bug-bash@gnu.org
Envelope-to bug-bash@gnu.org
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=xRAk2017zMiCDXflpGM4Wkh5penlXlXzuVkuz4V+6bU=; b=CSVEj47sGf+Ydeu/viNQHyDUVxq5F0t8qaG8l5UMfC3T0zbS7+hnoFSz3fNKEjwX+t Cpa5/E5YhW7wN6BYktOaDa7yanWIYMTGXOvWYs6X+kIT88u4DmZAG2R5xreY2AeLErZf fCwMUwdN+xR7hqRb66A3tV1/+p3WZyxx3Ufpm11bN8gdAw4cHO/a+3eePYZOPNTnAjzN yfovMlW0iHOsJTKCC/RLP5YSvaCL4Ua3mYqKqFf8wiRomVS/cimR73Mjoe8/m3x/ITdP 8LmdCxgGrqzSz4y8C97A7lqqyMD9cUHW+lhxnFbMeAhv14ZlsX/eS72DmZst20Wkvzp4 1ceg==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=xRAk2017zMiCDXflpGM4Wkh5penlXlXzuVkuz4V+6bU=; b=XfbZsjjsG2bX7OC6bZ+NY7xFhruH3LVwIoYTuolqR26ig+tlQ+0PH1u0xDDe/Q9iil nQ/B4I6DqewfIX7FnisJ1qupLeYvc48S4IgcbjA/YMHhHZV6g0pwlU2s5glh9UNfybcN 2+f3OGOZ49aX0Ko3bwkG8RLaNa6V6tONrr+qTDxc3kSlkO4KLuFaW6TtBJ34s5PXUmL4 xwOGMcnARk9+aAWUJU+IlkPHldFLmHvGOlJrOxIVj/hyvJ0aq5zJSrzwnvs+9nguPHyR cBwBsbbTVKRpeZ23ZHiqdDAMA/+2xbjbA8Dx4IgECH3e7labrzIXd91cOIqoqg1V6Njm ZxKQ==
X-Gm-Message-State APjAAAUg5b80IdkpEtz8jFuGIS5rmCHUuRaJl7KvzvUSZ5o4sGwhQC6A esS086xOwgG69qMy9ZAAuUtpV9+otGE=
X-Google-Smtp-Source APXvYqzl6ixcVFOesQx+0ogoFHZBtFRhghLtShGkwJW9LdJU1mnqO8b2nfe659M0duihed+nohWflg==
X-Received by 2002:ac8:1196:: with SMTP id d22mr23641188qtj.344.1580244573837; Tue, 28 Jan 2020 12:49:33 -0800 (PST)
Content-Disposition inline
In-Reply-To <20200128134935.GM1350@eeg.ccf.org>
X-PGP-Key http://rogerx.sdf.org/about/armor-F515AC1F231FFC4EF0BCDE7606CEA0ECE2887C75-20170104.asc
User-Agent Mutt/1.10.1 (2018-07-13)
X-detected-operating-system by eggs.gnu.org: Genre and OS details not recognized.
X-Received-From 2607:f8b0:4864:20::82b
X-BeenThere bug-bash@gnu.org
X-Mailman-Version 2.1.23
Precedence list
List-Id Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org>
List-Unsubscribe <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe>
List-Archive <https://lists.gnu.org/archive/html/bug-bash>
List-Post <mailto:bug-bash@gnu.org>
List-Help <mailto:bug-bash-request@gnu.org?subject=help>
List-Subscribe <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe>
X-Mailman-Original-Message-ID <20200128204932.GB12574@localhost4.local>
X-Mailman-Original-References <20200128020322.GA31704@localhost4.local> <20200128134935.GM1350@eeg.ccf.org>
Xref csiph.com gnu.bash.bug:15828

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

>If you run these, and try to kill them with Ctrl-C, you may find that
>the first one behaves perfectly (stops when you ask), and the second
>one does not.  It may take several tries to kill the second one.  You
>might have better luck suspending it with Ctrl-Z first, then killing
>the shell only, then fg'ing the job.
>
>See <https://www.cons.org/cracauer/sigint.html> for details.
>
>In a nutshell, ping(8) is a misbehaving program.  It sets a SIGINT
>trap (so that it can report statistics when you interrupt it), but
>it doesn't exit as a result of the signal.  The shell sees that the
>program didn't exit due to SIGINT, and decides that it (the shell)
>should not exit either.

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.  As I speculated, I was likely doing too much 
within one loop and should likely isolate each task to it's own specific loop, 
mitigating such behaviour.  (The check should have also likely checked for a 
ffmpeg created file >= to the original file before renaming/moving.)

In other words, it's safer to

1) Have ffmpeg process all files within it's own loop, then

2) Perform a second loop renaming files using mv.

But as I re-think think this, should be good Bash scripting practice to 
integrate a trap within each loop? 


>Whatever program you're using inside your loop may have the same
>issues.  It's extremely common.

Another thought occurred to me last night, no wonder they call it Bash. When 
you execute a Bash loop, you'll likely be sure to find yourself pounding 
"CTRL-C" on your keyboard!

As far as the design/intigration of the trap within a loop, I'm guessing my 
initial post contained the most simplistic, readable, proper form of a trap for 
creating a safe quick exiting loop?


--
Roger
http://rogerx.sdf.org/

Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread


Thread

Re: Protect Loop Execution with Traps Roger <rogerx.oss@gmail.com> - 2020-01-28 15:49 -0500

csiph-web