[GRASSLIST:1461] Initialising Grass Script From cron

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

Thank you.

Izak

^^^^^^^^^^^^^^^^
Izak T.H. Deyzel
Meteorologist
METSYS
South Africa

Izak,

You should be able to run your script from within GRASS by initializing
it form the command prompt from *within* GRASS. We don something
similar with SWAT/GRASS. I'm a bit unclear on exactly what exactly
you are starting to do. However, if you have a script similar to the one I
have attached it should work. This makes all the calls to a set of binary
programs. Pay close attention to the front of the script.

Let me know if this helps.

Bruce

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

Thank you.

Izak

^^^^^^^^^^^^^^^^
Izak T.H. Deyzel
Meteorologist
METSYS
South Africa

(attachments)

SWATGRASS.sh (3.14 KB)

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

Thank you.

Hi Izak,

you may have a look here:

http://www.geog.uni-hannover.de/grass/faq/index.html#problems
->
    How to use GRASS in batch mode (fully script based)?:

This has been simplified in GRASS 5:

grass5 -help
Usage:
    grass5 [-h | -help | --help] [-text | -tcltk] [[[<GISDBASE>/]<LOCATION_NAME>/]<MAPSET>]

Flags:
    -h or -help or --help print this help message
    -text use text based interface
    -tcltk use Tcl/Tk based graphical interface

Parameters:
    GISDBASE initial database
    LOCATION_NAME initial location
    MAPSET initial mapset

    GISDBASE/LOCATION_NAME/MAPSET fully qualified initial LOCATION directory

Environment variables:
    GRASS_TCLSH set tclsh shell name to override 'tclsh'
    GRASS_WISH set wish shell name to override 'wish'

Hope this helps,

Markus Neteler

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
>
> Thank you.
>
Hi Izak,

you may have a look here:

http://www.geog.uni-hannover.de/grass/faq/index.html#problems
->
    How to use GRASS in batch mode (fully script based)?:

This has been simplified in GRASS 5:

grass5 -help
Usage:
    grass5 [-h | -help | --help] [-text | -tcltk] [[[<GISDBASE>/]<LOCATION_NAME>/]<MAPSET>]

Flags:
    -h or -help or --help print this help message
    -text use text based interface
    -tcltk use Tcl/Tk based graphical interface

Parameters:
    GISDBASE initial database
    LOCATION_NAME initial location
    MAPSET initial mapset

    GISDBASE/LOCATION_NAME/MAPSET fully qualified initial LOCATION directory

Environment variables:
    GRASS_TCLSH set tclsh shell name to override 'tclsh'
    GRASS_WISH set wish shell name to override 'wish'

Hope this helps,

Markus Neteler

--
"A successful tool is one that was used | Scott O'Donnell
to do something undreamed of by its author."| odonnell@fsl.noaa.gov
                   -- S. C. Johnson | 303 497 6562

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