Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: infinite loop that is possible to quit

On 30 Nov 2020, at 18:50, Emanuel Berg <moasenwood@xxxxxxx> 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@xxxxxxxxxxxxxx'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.


Messages sorted by: Reverse Date, Date, Thread, Author