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


Groups > linux.debian.user > #227322

Deterministic delays in POSIX shell scripts (Was: Re: notify via virtual terminal available packages)

From Andy Smith <andy@strugglers.net>
Newsgroups linux.debian.user
Subject Deterministic delays in POSIX shell scripts (Was: Re: notify via virtual terminal available packages)
Date 2020-09-25 09:50 +0200
Message-ID <ASQEp-5FR-5@gated-at.bofh.it> (permalink)
References (1 earlier) <ASzNf-3Kv-7@gated-at.bofh.it> <ASzWW-3NY-3@gated-at.bofh.it> <ASAzE-4fD-11@gated-at.bofh.it> <ASAzE-4fD-9@gated-at.bofh.it> <ASAJk-4iX-9@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


Hello,

On Thu, Sep 24, 2020 at 08:49:07AM -0600, Charles Curley wrote:
> On Thu, 24 Sep 2020 10:38:55 -0400
> Greg Wooledge <wooledg@eeg.ccf.org> wrote:
> > So you're just doing "sleep 1" every time.
> 
> Ah, thank you. Yup. Which is weird, because it worked when I first
> wrote that many years ago.

In cron scripts where I want a "random" delay, I actually tend to
not really want it to be random, but just different for that host as
opposed to other hosts, otherwise deterministic. I like it if the
delay is the same every time on that host as long as it is a
different delay on different hosts.

So what I tend to do is something like:

    sleep $(( $(printf %d "0x$(hostid)") % 60 ))m; /some/command

which will sleep for some amount of time between 0 and 59 minutes,
the same amount every time, but different on different hosts.

(Obviously change the "60" and the "m" to different values for
different things, like you might want "1440" and "m" for minutes in
a day.)

Note that in a file parsed by cron you do need to escape both the
'%' (like '\%').

The printf is needed to turn the hexadecimal value from the "hostid"
command into a decimal number. Is there a way to do that with pure
shell internals that isn't very verbose?

"hostid" tends to return a hexadecimal representation of the first
IPv4 address (but isn't guaranteed to). On a systemd system one
could instead use /etc/machine-id. On Linux there is also
/proc/sys/kernel/random/boot_id (but needs dashes removed).

Systemd timers can do this sort of thing themselves, so no need
there for this sort of scripting.

> But I will move toward more use of unattended-upgrades, which
> handles the original problem differently.

Yup, I use apticron and unattended-upgrades for solving these
problems these days.

Cheers,
Andy

-- 
https://bitfolk.com/ -- No-nonsense VPS hosting

Please consider the environment before reading this e-mail.
 — John Levine

Back to linux.debian.user | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

notify via virtual terminal available packages Pòl Hallen <deben@fuckaround.org> - 2020-09-23 22:50 +0200
  Re: notify via virtual terminal available packages john doe <johndoe65534@mail.com> - 2020-09-24 10:40 +0200
  Re: notify via virtual terminal available packages Charles Curley <charlescurley@charlescurley.com> - 2020-09-24 15:50 +0200
    Re: notify via virtual terminal available packages Greg Wooledge <wooledg@eeg.ccf.org> - 2020-09-24 16:00 +0200
      Re: notify via virtual terminal available packages Greg Wooledge <wooledg@eeg.ccf.org> - 2020-09-24 16:40 +0200
        Re: notify via virtual terminal available packages Charles Curley <charlescurley@charlescurley.com> - 2020-09-24 16:50 +0200
          Re: notify via virtual terminal available packages Greg Wooledge <wooledg@eeg.ccf.org> - 2020-09-24 17:00 +0200
          Deterministic delays in POSIX shell scripts (Was: Re: notify via  virtual terminal available packages) Andy Smith <andy@strugglers.net> - 2020-09-25 09:50 +0200
            Re: Deterministic delays in POSIX shell scripts (Was: Re: notify via  virtual terminal available packages) Greg Wooledge <wooledg@eeg.ccf.org> - 2020-09-25 13:50 +0200
              Re: Deterministic delays in POSIX shell scripts (Was: Re: notify via  virtual terminal available packages) Andy Smith <andy@strugglers.net> - 2020-09-25 14:30 +0200
                Re: Deterministic delays in POSIX shell scripts (Was: Re: notify via  virtual terminal available packages) David Wright <deblis@lionunicorn.co.uk> - 2020-09-26 04:50 +0200
      Re: notify via virtual terminal available packages Charles Curley <charlescurley@charlescurley.com> - 2020-09-24 16:40 +0200

csiph-web