[OT] Re: Automated... CRON - Mailing list pgsql-admin
From | Tim Ellis |
---|---|
Subject | [OT] Re: Automated... CRON |
Date | |
Msg-id | 20020808101729.67a633eb.Tim.Ellis@gamet.com Whole thread Raw |
In response to | Re: Automated database backups and authentication (Lee Harr <missive@frontiernet.net>) |
List | pgsql-admin |
On Tue, 6 Aug 2002 23:32:42 +0000 (UTC) Lee Harr <missive@frontiernet.net> wrote: > Some issues I ran into: > [snip] > * Expect scripts which run fine from the command line will > mysteriously fail from cron (and just hang around not > doing much of anything) I found that using absolute > paths to *everything* solved most of those problems. > [snip] > I had never used cron, and it was tough to get this > working, but now it seems pretty good. cron is an interesting beast. Because it doesn't source your .profile (or whatever shell equivalent, .bashrc, .cshrc, .kshrc, whatever), then often the shell environment of a cron job isn't quite the same as an interactive session. Usually, in fact, you don't *WANT* it to do most of those variables. Who cares what your prompt looks like during a cron job, anyway? But then PATH isn't right, ORACLE_HOME isn't right, or SYBASE isn't set. Thus, scripts FAIL through cron that worked interactively. After struggling with this problem for a few years (hey, I'm not bright, but eventually I get it!), I settled on this: Once the script is working in an interactive session, I get a list of environment variables ("env" or "set" -- usually one of those will spew forth every variable you've got set) then, instead of 00 04 * * * $HOME/dir/script.sh I do 00 04 * * * $HOME/dir/wrapper.sh script.sh wrapper.sh is just a shellscript that #1 sets up the environment as it was during the interactive shell run, takes a single argument (that is, the script to run). So wrapper.sh looks something like: ----------------------------------------------------------------- #!/bin/bash export ENV1=/usr/bin export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin export PGHOME=/usr/local/pgsql $1 > /var/log/databasescripts/$1.log ----------------------------------------------------------------- The "export XYZ=abc" bit are the lines from the "set" or "env" command that I deemed necessary. PATH, for me, is always one of those. With a little experimentation, this method should make setting up cron jobs a *LOT* easier for you. These days, my cron jobs work on the first time with no errors about 95% of the time, and I'd say that's completely because of this method of putting the ENV variables into a wrapper script. Just make sure you handpick which environment variables to stick into that script. You don't need/want PWD or _ or SHLVL or anything, but LC_* and PATH and OSTYPE and HOSTNAME might actually be useful. You will get pretty good at determining what you need and what you don't after a few iterations. -- Tim Ellis Senior Database Architect Gamet, Inc. ps -- Once you have this done, you suddenly realise wrapper.sh is a perfect place to put some log rotation, log analysis, and email notification. I've attached doSQLScript.sh for calling SQL scripts in an Oracle-specific way, and runScript.sh which is for calling shell scripts (again, with an Oracle bent). Both of these should be pretty easy to repurpose to Postgres.
Attachment
pgsql-admin by date: