From owner-freebsd-questions@FreeBSD.ORG Tue Sep 17 03:38:37 2013 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTP id 84B9CE29 for ; Tue, 17 Sep 2013 03:38:37 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mx01.qsc.de (mx01.qsc.de [213.148.129.14]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 48B9F2680 for ; Tue, 17 Sep 2013 03:38:37 +0000 (UTC) Received: from r56.edvax.de (port-92-195-74-65.dynamic.qsc.de [92.195.74.65]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx01.qsc.de (Postfix) with ESMTPS id 9FBC23E699; Tue, 17 Sep 2013 05:38:34 +0200 (CEST) Received: from r56.edvax.de (localhost [127.0.0.1]) by r56.edvax.de (8.14.5/8.14.5) with SMTP id r8H3cQZP003456; Tue, 17 Sep 2013 05:38:26 +0200 (CEST) (envelope-from freebsd@edvax.de) Date: Tue, 17 Sep 2013 05:38:26 +0200 From: Polytropon To: kpneal@pobox.com Subject: Re: test if script called by cron Message-Id: <20130917053826.5a5c7ed3.freebsd@edvax.de> In-Reply-To: <20130917032817.GA17509@neutralgood.org> References: <5236EB03.7040001@ifdnrg.com> <20130916140504.739fd28f.freebsd@edvax.de> <20130917032817.GA17509@neutralgood.org> 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 Cc: Paul Macdonald , freebsd-questions@freebsd.org X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: Polytropon List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 17 Sep 2013 03:38:37 -0000 On Mon, 16 Sep 2013 23:28:17 -0400, kpneal@pobox.com wrote: > On Mon, Sep 16, 2013 at 02:05:04PM +0200, Polytropon wrote: > > On Mon, 16 Sep 2013 12:26:59 +0100, Paul Macdonald wrote: > > > Is there a simple way of testing whether a given script was called via cron, > > > > > > I'd rather find a solution that would work from within the script rather > > > than setting an environment variable in the crontab. > > > > I'd suggest the script creates a file (lock file or, > > much easier, just a simple normal file) at its beginning: > > > > #!/bin/sh > > /usr/bin/touch /tmp/scriptrun > > # ... your script content here ... > > Wouldn't the lockf command be better than touch? That way you get the > condition code telling you whether or not the script is already running. Yes, it would probably be better in this case. This, in combination with the suggestion of "test-t 0" to check if the script has been interactively called or not, looks like a better solution. However, the intial question does not make fully sure (at least to me as a non-native speaker) if the intention is (a) to check _if_ the script has been run via cron, or (b) to check if the script has been run via _cron_. :-) > > Of course you would have to manually remove that file > > after you have verified its existence and content. > > If you use lockf as a drop-in replacement for touch then, yes, you'll > need to keep the lock file until removing it at the end of the script. Depends. Let's say the script is scheduled at 3:00 and will finish in about half an hour. The "evidence file" will only be visible from 3:00 to ca. 3:30, so removing the "evidence file" after the script has finished could lead to a false-negative result ("has not been run"). This is also true for the more simple solution using the touch command (no rm call at the end of the script). -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...