From owner-freebsd-questions@freebsd.org Sat Nov 11 07:50:53 2017 Return-Path: Delivered-To: freebsd-questions@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 68F37E638F6 for ; Sat, 11 Nov 2017 07:50:53 +0000 (UTC) (envelope-from ws@au.dyndns.ws) Received: from ipmail07.adl2.internode.on.net (ipmail07.adl2.internode.on.net [150.101.137.131]) by mx1.freebsd.org (Postfix) with ESMTP id ED3E377941 for ; Sat, 11 Nov 2017 07:50:52 +0000 (UTC) (envelope-from ws@au.dyndns.ws) Received: from ppp103-111.static.internode.on.net (HELO lillith-iv.ovirt.dyndns.ws) ([150.101.103.111]) by ipmail07.adl2.internode.on.net with ESMTP; 11 Nov 2017 18:15:42 +1030 X-Envelope-From: ws@au.dyndns.ws X-Envelope-To: freebsd-questions@freebsd.org Received: from predator-ii.buffyverse (predator-ii.buffyverse [172.17.17.136]) by lillith-iv.ovirt.dyndns.ws (8.14.9/8.14.9) with ESMTP id vAB7jZ6j036478; Sat, 11 Nov 2017 18:15:36 +1030 (ACDT) (envelope-from ws@au.dyndns.ws) Message-ID: <1510386335.86602.2.camel@au.dyndns.ws> Subject: Re: how to code a timer loop in a sh script From: Wayne Sierke To: Ernie Luzar , "freebsd-questions@freebsd.org" Date: Sat, 11 Nov 2017 18:15:35 +1030 In-Reply-To: <5A00A826.2000501@gmail.com> References: <5A00A826.2000501@gmail.com> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.24.2 FreeBSD GNOME Team Mime-Version: 1.0 Content-Transfer-Encoding: 7bit X-Greylist: inspected by milter-greylist-4.6.2 (lillith-iv.ovirt.dyndns.ws [172.17.17.142]); Sat, 11 Nov 2017 18:15:36 +1030 (ACDT) for IP:'172.17.17.136' DOMAIN:'predator-ii.buffyverse' HELO:'predator-ii.buffyverse' FROM:'ws@au.dyndns.ws' RCPT:'' X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.6.2 (lillith-iv.ovirt.dyndns.ws [172.17.17.142]); Sat, 11 Nov 2017 18:15:36 +1030 (ACDT) X-Scanned-By: MIMEDefang 2.78 on 172.17.17.142 X-Scanned-By: SpamAssassin 3.004001(2015-04-28) X-Scanned-By: ClamAV X-Spam-Score: -1 () ALL_TRUSTED X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 11 Nov 2017 07:50:53 -0000 On Mon, 2017-11-06 at 13:21 -0500, Ernie Luzar wrote: > Trying to write a sh script that will run continually and every 10 > minutes issue a group of commands. Been trying to use the wait > command > and the while loop command to achieve the desired effect with no > joy. > Would like an example of a wait loop code to see how its done. > > Thanks for any help. Other answers have covered the "sleep X" inside the loop approach. One potential shortcoming is that it accumulates the execution time of the script. That is, if the time taken to execute the code in the loop is 1 minute, then the next execution will start 11 minutes after the time that the previous execution started. The following script records a timestamp and uses it to determine when to execute the scheduled part. #!/bin/sh # see man date(1), -v option description INTERVAL=600S ; # suffix is one of: y, m, w, d, H, M or S # Set the initial scheduled time, delayed by $INTERVAL. # To adjust the initial delay, replace $INTERVAL with alternate value nextruntime=$(date -v +$INTERVAL +%s) while : ; do echo "executing main code (pre-scheduled)..." if [ $(date +%s) -ge $nextruntime ] ; then echo executing scheduled code... ; # scheduled code here nextruntime=$(date -r $nextruntime -v +$INTERVAL +%s) fi echo "executing main code (post-scheduled)..." ; sleep 1 done One potential shortcoming of this is if the loop execution takes longer than $INTERVAL, the scheduled code will get executed repeatedly until it "catches up". When that is not desirable, a more sophisticated handling of the timestamp can be used.