From: Alex Satrapa <grail@goldweb.com.au>
To: zsh-users@zsh.org
Subject: Re: infinite loop that is possible to quit
Date: Tue, 1 Dec 2020 18:36:36 +1100 [thread overview]
Message-ID: <A2B8B6A2-AB4D-4B52-AB72-B06CCC13F800@goldweb.com.au> (raw)
In-Reply-To: <875z5m9twt.fsf@zoho.eu>
There's no real "good" or "bad" decision here until you start doing stuff inside that loop. How will immediately halting processing of that script cause trouble later? Will you corrupt data? Leave a peripheral in an unknown state? If there's no concern about data corruption, allowing Ctrl+C (or SIGTERM) terminate the processing of the script is fine.
Other options include:
1. Reading user input and looking for eg "q" being pressed
2. Looking for a specific file on the file system, eg ".stop" and stopping when that file appears
3. Looking for a specific file on the file system, eg "config" and stopping when that file disappears
4. Using a settings file which you watch for changes, reread the settings, and get the stop/go from a config settings
5. Catching Ctrl+C (SIGTERM) and handling it (more about that later)
#!/bin/zsh
CONTINUE=1
wrap_up() {
echo "Wrapping up."
CONTINUE=0
}
trap 'wrap_up' HUP INT QUIT TERM
while [ $CONTINUE -eq 1 ] ; do
echo "I'm still here."
sleep 5
done
Which will produce something like:
[user@host]$ ./traptest
I'm still here.
I'm still here.
^CWrapping up.
The traps I'm catching there are the ones usually associated with a soft shutdown.
HTH
HAND
> On 1 Dec 2020, at 12:50, Emanuel Berg <moasenwood@zoho.eu> wrote:
>
> How do I do an infinite loop that runs something, but it will
> still be possible to quit the whole thing?
>
> The best thing I've come up with is
>
> while true; do
> # run program
> sleep 1
> done
>
> Then do, say, q to exit the program, and C-c during the sleep
> period to quit the loop!
>
> Is this good or bad?
>
> TIA
>
> --
> underground experts united
> http://user.it.uu.se/~embe8573
> https://dataswamp.org/~incal
>
>
next prev parent reply other threads:[~2020-12-01 7:37 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-01 1:50 Emanuel Berg
2020-12-01 7:36 ` Alex Satrapa [this message]
2020-12-01 9:53 ` Lewis Butler
2020-12-01 17:17 ` Daniel Shahaf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=A2B8B6A2-AB4D-4B52-AB72-B06CCC13F800@goldweb.com.au \
--to=grail@goldweb.com.au \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).