Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 17 Feb 2006 10:41:48 -0600
From:      Tim Daneliuk <tundra@tundraware.com>
To:        Dan Nelson <dnelson@allantgroup.com>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: /bin/sh Madness
Message-ID:  <43F5FCCC.5050409@tundraware.com>
In-Reply-To: <20060217011923.GA87403@dan.emsphone.com>
References:  <43F50074.8060205@tundraware.com> <20060217011923.GA87403@dan.emsphone.com>

next in thread | previous in thread | raw e-mail | index | archive | help
Dan Nelson wrote:
> In the last episode (Feb 16), Tim Daneliuk said:
> 
>>Here is a shell function that behaves quite strangely:
>>
>>#!/bin/sh
>>#####
>># Execute A Command, Noting Start/Stop Time, & Logging Output
>># Args:
>>#       $1 Command Name
>>#       $2 Log Directory
>>#       $3 Command String To Execute
>>#####
>>
>>runupd()
>>{
>>      log=$2/$1.log
>>      timestamp $log
>>      touch $2/.$1-begin && eval $3 2>&1 >> $log && touch $2/.$1-end &
>>}
>># End of 'runupd()'
>>
>>So, you might do something like:
>>
>>   runupd freespace /var/log/ "df -k"
>>
>>Now, for the weirdness.  This function works fine in my script
>>so long as one of two conditions is met:
>>
>>   1) I run it interactively from the command line (bash)
>>                      OR
>>   2) I run it from 'cron' AND $3 is *not* another script
>>
>>If I try to run it from 'cron' and point $3 to a script, everything gets
>>run as planned, however, the ending timestamp (touch $2/.$1-end) never
>>runs. That is, the initial time stamp (.$1-begin) and the command itself
>>are executed, and output is properly written to the logfile,
>>but the final timestamp never happens.
> 
> 
> Could your $3 command be returning a nonzero exit code?  You probably
> want something more like
> 
> touch $2/.$1-begin && { eval $3 2>&1 >> $log ; touch $2/.$1-end } &

I am looking into this.

> 
> so your end timestamp always gets created whether or not $3 succeeds.
> Also note that in your original script, you only backgrounded "touch
> $2/.$1-end", which is probably not what you wanted.


Hmmmm - I have invoked the script that uses this function a variety
of ways and the command being evaled *is* being backgrounded in
every case with the syntax shown above ...


-- 
----------------------------------------------------------------------------
Tim Daneliuk     tundra@tundraware.com
PGP Key:         http://www.tundraware.com/PGP/




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?43F5FCCC.5050409>