From owner-freebsd-questions@freebsd.org Sat Nov 11 08:32:36 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 166BAE64E92 for ; Sat, 11 Nov 2017 08:32:36 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mailrelay10.qsc.de (mailrelay10.qsc.de [212.99.163.152]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "*.antispameurope.com", Issuer "TeleSec ServerPass Class 2 CA" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 61E3779089 for ; Sat, 11 Nov 2017 08:32:34 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mx01.qsc.de ([213.148.129.14]) by mailrelay10.qsc.de; Sat, 11 Nov 2017 09:32:26 +0100 Received: from r56.edvax.de (port-92-195-23-159.dynamic.qsc.de [92.195.23.159]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx01.qsc.de (Postfix) with ESMTPS id E7FE83CBF9; Sat, 11 Nov 2017 09:32:24 +0100 (CET) Received: from r56.edvax.de (localhost [127.0.0.1]) by r56.edvax.de (8.14.5/8.14.5) with SMTP id vAB8WNNA001942; Sat, 11 Nov 2017 09:32:23 +0100 (CET) (envelope-from freebsd@edvax.de) Date: Sat, 11 Nov 2017 09:32:23 +0100 From: Polytropon To: Wayne Sierke Cc: Ernie Luzar , "freebsd-questions@freebsd.org" Subject: Re: how to code a timer loop in a sh script Message-Id: <20171111093223.ade0c610.freebsd@edvax.de> In-Reply-To: <1510386335.86602.2.camel@au.dyndns.ws> References: <5A00A826.2000501@gmail.com> <1510386335.86602.2.camel@au.dyndns.ws> Reply-To: Polytropon Organization: EDVAX X-Mailer: Sylpheed 3.1.1 (GTK+ 2.24.5; i386-portbld-freebsd8.2) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-cloud-security-sender: freebsd@edvax.de X-cloud-security-recipient: freebsd-questions@freebsd.org X-cloud-security-Virusscan: CLEAN X-cloud-security-disclaimer: This E-Mail was scanned by E-Mailservice on mailrelay10.qsc.de with 158CD683440 X-cloud-security-connect: mx01.qsc.de[213.148.129.14], TLS=1, IP=213.148.129.14 X-cloud-security: scantime:.1436 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 08:32:36 -0000 On Sat, 11 Nov 2017 18:15:35 +1030, Wayne Sierke wrote: > 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. An easy solution is the use of a lock file and [ -f for it, prior to starting the execution. If there is no such file, create it, then start executing, and finally remove it. In this branch, checking for the time interval will happen. Should another instance of the script be called during the execution of the already running one, [ -f will evaluate to true (as the file exists) and not enter the branch for execution. -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...