[GRASSLIST:1467] Re: Initialising Grass Script From cron

coopl coopl at ippc2.orst.edu
Thu Feb 15 18:31:32 EST 2001


Izak,

Here is the "dograss" script we use for running scripts from cron
using GRASS 4.x - LenCoop

usage:
>dograss our_grass_cron_script params

dograss:
==================================================================================
! /bin/sh
trap '/bin/rm -f $lockfile' 0 1 2 3 5 9 15 # nevermind sig 9 can't be
caught...

########################################### setup general variables
user=`whoami`
home=/home/$user

export GISRC=$home/.grassrc
export GISBASE=/usr/local/grass42
export GISDBASE=/home2/data
export LOCATION_NAME=NWest_US
export MAPSET=$user
export PAINTER=preview
export LOCATION=$GISDBASE/$LOCATION_NAME
export GIS_LOCK=$$
export PATH=$GISBASE/bin:$GISBASE/scripts:$GISBASE/garden/bin:$PATH
ETC=$GISBASE/etc

########################################## file locking: get lock on
grass
lockfile=$home/.gislock

# grass's lock command tries to store a number (pid) in a file.
# It fails if the file exists and there's a live process with the stored
pid
# I can only assume it does the right thing with respect to race
conditions
if $ETC/lock $lockfile $GIS_LOCK
then    # nothing; it succeeded
        :
else    # #user is already running grass
        exit 1;
fi
########################################## 

g.gisenv GISDBASE=$GISDBASE
g.gisenv LOCATION_NAME=$LOCATION_NAME
g.gisenv LOCATION=$LOCATION
g.gisenv MAPSET=$MAPSET
eval `g.gisenv`

########################################### Run the GRASS command
# i.e. the entire command line
$*
########################################### Exit GRASS: clean up
monitor=`g.gisenv MONITOR`
if [ "$monitor" ]
then
        $ETC/mon.release -v $monitor
fi
g.gisenv MONITOR=

eval `g.gisenv`
LOCATION=${GISDBASE?}/${LOCATION_NAME?}/${MAPSET?}

rm -f $lockfile                 # release lock on grass

===================================================================================================


Scott O'Donnell wrote:
> 
> Hi Izak,
> 
> Marcus' instructions are nice to know about, but if you don't run GRASS-5.0
> yet I don't think it answers your question.
> 
> I'm still using GRASS-4.3 (similar to your version) and being a traditional
> UNIX user (Linux & HPUX), Iprefer to invoke GRASS functions from the
> command line and from scripts, rarely using interactive interface/s.
> 
> To guarantee a proper runtime environment for GRASS-4.3, I define my default
> GRASS environment variables in my startup  script, '.cshrc' (my default
> shell is tcsh).  I initialize my environment variables using the following
> commands (remember I use tcsh, so you may need to adapt these definitions
> for your preferred shell)...
> 
> setenv GISDBASE      ${HOME}/Grass/db
> setenv GISRC         ${HOME}/.grassrc
> setenv LOCATION_NAME  UTM
> setenv MAPSET         24K
> setenv LOCATION       ${GISDBASE}/${LOCATION_NAME}/${MAPSET}
> 
> (Of course, you must make the necessary directory substitutions)
> 
> When I need to access another dataset, I run scripts to redefine these
> variables as necessary.  Also, I have '.grassrc' files for each of my
> datasets and copy the relevant grass resource file to my home directory.
> 
> Having done this, my scripts and command line operations ALWAYS
> do the right things and I don't struggle with defining the "right"
> environment any more.
> 
> It's nice to know how to define my working environment when I get to
> GRASS-5.0, but until then, this method works well for me.
> 
> Hope this helps,
> Scott O'Donnell
> ---
> 
> Markus Neteler wrote:
> >
> > On Thu, Feb 15, 2001 at 03:56:26PM +0200, Izak Thomas Henry Deyzel wrote:
> > > Good Day Everyone,
> > >
> > > I am using Grass4.2.1. I am trying to automise my whole process of data
> > > manipulation and map production. I am using a bash script to do the
> > > grass stuff, but need to intialise this script from a cron. Grass and
> > > its dbase is running the whole time, but the cron runs in its own
> > > environment oblivious of Grass dbase. I test for the db being active and
> > > receive errors. Is there a method of exporting Grass' environmental
> > > variables so that my cron environment will be aware of the dbase or is
> > > there a method of intialising Grass and its dbase within the cron
> > > script:
> > >
> > > e.g. grass4.2 <<-script
> > > .....grass stuff....
> > > exit
> > > script
> > >

-- 
Leonard Coop
Research Associate
Entomology Dept & Integrated Plant Protection Center
2046 Cordley
Oregon State University
Corvallis OR 97331-2907
Phone 541-737-5523
Fax   541-737-3080
http://osu.orst.edu/dept/entomology/coopl




More information about the grass-user mailing list