peschkaj has asked for the wisdom of the Perl Monks concerning the following question:

I am working on creating a script that will run through the .profile on several servers. Normally this would be an easy task, but we have several k shell scripts which run through cron. In order for these scripts to run correctly, they need to run .profile so they can be aware of $ORACLE_HOME and other variables. My script, however, does not need this info.

The solution we are thinking of pursuing right now is to have the perl script detect when it is being called by cron, rather than a standard login, and then gracefully die so that cron does not hang waiting for input.

My question, in a nutshell, is: Is there any way to detect if cron is running .profile, rather than a 'standard' user logon?

Replies are listed 'Best First'.
Re: Detecting cron
by tommyw (Hermit) on Aug 08, 2002 at 14:55 UTC

    From my cron manpage:
    The shell is invoked from your $HOME directory with an arg0 of sh. Users who desire to have their .profile executed must explicitly do so in the crontab file. cron supplies a default environment for every shell, defining HOME, LOGNAME, SHELL(=/bin/sh), TZ, and PATH. The default PATH for user cron jobs is /usr/bin; while root cron jobs default to /usr/sbin:/usr/bin. The default PATH can be set in /etc/default/cron; see cron(1M).

    So the usual mechanism is to look for the existance of some environment variable, other than those mentioned. And make sure that it's set nice and early for an interactive login.

    Or there's the perlfaq. Or just the fact that cron jobs have their STDIN closed, so any attempt to read from it will simply return EOF, and not hang.

    --
    Tommy
    Too stupid to live.
    Too stubborn to die.

Re: Detecting cron
by peschkaj (Pilgrim) on Aug 08, 2002 at 16:48 UTC
    AH AH! Thank you Tommy.

    Regardless of what is run from cron, the $SHELL will be set to /usr/bin/sh. So, all I need to do is look at %ENV{SHELL} and compare and away I go!
Re: Detecting cron
by peschkaj (Pilgrim) on Aug 08, 2002 at 15:04 UTC
    What would happen, though, if cron ran a k-shell script and the k-shell script ran .profile? Would I still be able to detect using the methods you describe?

      That depends: if the .profile sets the environment variable you're testing for, then after it's been run, the variable exists. So you need to test first :-)

      OTOH, the .profile isn't going to have any effect on the arrangement of STDIN and STDOUT, so they'll still be safe to test (or safe to rely on the fact that STDIN just returns EOF...)

      --
      Tommy
      Too stupid to live.
      Too stubborn to die.