[OT] Re: Automated... CRON

Поиск
Список
Период
Сортировка
От Tim Ellis
Тема [OT] Re: Automated... CRON
Дата
Msg-id 20020808101729.67a633eb.Tim.Ellis@gamet.com
обсуждение исходный текст
Ответ на Re: Automated database backups and authentication  (Lee Harr <missive@frontiernet.net>)
Список 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.

Вложения

В списке pgsql-admin по дате отправления:

Предыдущее
От: Brickley Jeff-RA9607
Дата:
Сообщение: Rookie - Performance Tests
Следующее
От: Tom Lane
Дата:
Сообщение: Re: Rookie - Performance Tests