zsh-users
 help / color / mirror / code / Atom feed
* infinite loop that is possible to quit
@ 2020-12-01  1:50 Emanuel Berg
  2020-12-01  7:36 ` Alex Satrapa
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Emanuel Berg @ 2020-12-01  1:50 UTC (permalink / raw)
  To: zsh-users

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



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: infinite loop that is possible to quit
  2020-12-01  1:50 infinite loop that is possible to quit Emanuel Berg
@ 2020-12-01  7:36 ` Alex Satrapa
  2020-12-01  9:53 ` Lewis Butler
  2020-12-01 17:17 ` Daniel Shahaf
  2 siblings, 0 replies; 4+ messages in thread
From: Alex Satrapa @ 2020-12-01  7:36 UTC (permalink / raw)
  To: zsh-users

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
> 
> 



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: infinite loop that is possible to quit
  2020-12-01  1:50 infinite loop that is possible to quit Emanuel Berg
  2020-12-01  7:36 ` Alex Satrapa
@ 2020-12-01  9:53 ` Lewis Butler
  2020-12-01 17:17 ` Daniel Shahaf
  2 siblings, 0 replies; 4+ messages in thread
From: Lewis Butler @ 2020-12-01  9:53 UTC (permalink / raw)
  To: Zsh Users

On 30 Nov 2020, at 18:50, Emanuel Berg <moasenwood@zoho.eu> wrote:
> while true; do
>  # run program
>  sleep 1
> done

Since I tend to use loops like this just to do simple things like check status on some other process, that is the method I mostly use (though usually with a sleep 5 or 30 or 300, very rarely a sleep 1). When you get to more complicated scripts though, it is better to have some brains in the script for terminating cleanly.

How you do that depends very much on what you are doing.

I like grail@goldweb.com.au's example of trapping TERM QUIT and HUP, but that is not always the best solution either.

For example, I used to have a pair of scripts. One checked that a network mount was enabled and if it vanished, it remounted it, and restarted the second script. The other synced files to the network mount. When the second script was done, it wrote a file to the network with the output of the sync (how many files, how much data was transferred). When the first script saw that file, it exited.

This was necessary when I had a somewhat unreliable network connection to the remote server. Or at least it was the simplest way I had at the time to ensure that the network sync happened as quickly as possible and that I didn't end up with an incomplete sync because the network had gone offline for 40 seconds at 03:19.

Basically, when you have an infinite loop, you have to have an idea of what is going to end that loop, and what makes sense depends on what the loop is doing. It may be a file, or a time, or user intervention, or a particular condition on the machine. All are valid, and which is best depends on what you're trying to achieve.

-- 
ɹןʇnqן
<mailto:lbutler@covisp.net>
tel:+1.303.219.0564





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: infinite loop that is possible to quit
  2020-12-01  1:50 infinite loop that is possible to quit Emanuel Berg
  2020-12-01  7:36 ` Alex Satrapa
  2020-12-01  9:53 ` Lewis Butler
@ 2020-12-01 17:17 ` Daniel Shahaf
  2 siblings, 0 replies; 4+ messages in thread
From: Daniel Shahaf @ 2020-12-01 17:17 UTC (permalink / raw)
  To: zsh-users

Emanuel Berg wrote on Tue, Dec 01, 2020 at 02:50:10 +0100:
> 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?

I'd spell it like this:

    while run program && sleep 1; do done

Or use the Linux version of watch(1) (not to be confused with the FreeBSD tool
of the same name).




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-12-01 17:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-01  1:50 infinite loop that is possible to quit Emanuel Berg
2020-12-01  7:36 ` Alex Satrapa
2020-12-01  9:53 ` Lewis Butler
2020-12-01 17:17 ` Daniel Shahaf

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).