shell functions not working in 'at' or 'cron' - SCO

This is a discussion on shell functions not working in 'at' or 'cron' - SCO ; I've got a script that runs fine from the command line that's bombing out in 'at'. I build a little test case to show what's happening. The script: # Testcase to figure out why # functions are not working in ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: shell functions not working in 'at' or 'cron'

  1. shell functions not working in 'at' or 'cron'

    I've got a script that runs fine from the command line that's bombing
    out in 'at'. I build a little test case to show what's happening.

    The script:
    # Testcase to figure out why
    # functions are not working in 'at'

    function greet {
    echo "Hello, Dave"
    }

    echo "script starting"
    greet
    echo "script ending"

    Run from the command line:
    # echo $SHELL
    /bin/sh
    # ./func-testcase.sh
    script starting
    Hello, Dave
    script ending
    #

    Now here's the output of the job from 'at':
    To: root
    Date: Fri, 11 Jul 2008 11:05:00 CDT
    Status: R

    /upgrade_work/func-testcase.sh: function: not found
    Hello, Dave
    /upgrade_work/func-testcase.sh: syntax error at line 7: `}'
    unexpected.

    When I invoke 'at' it warns me that my job will be run with /bin/sh,
    but I'm using /bin/sh on the command line without a problem. What am
    I missing?

    TIA Dave




  2. Re: shell functions not working in 'at' or 'cron'

    dthacker typed (on Fri, Jul 11, 2008 at 09:12:25AM -0700):
    | I've got a script that runs fine from the command line that's bombing
    | out in 'at'. I build a little test case to show what's happening.
    |
    | The script:
    | # Testcase to figure out why
    | # functions are not working in 'at'
    |
    | function greet {
    | echo "Hello, Dave"
    | }
    |
    | echo "script starting"
    | greet
    | echo "script ending"
    |
    | Run from the command line:
    | # echo $SHELL
    | /bin/sh
    | # ./func-testcase.sh
    | script starting
    | Hello, Dave
    | script ending
    | #
    |
    | Now here's the output of the job from 'at':
    | To: root
    | Date: Fri, 11 Jul 2008 11:05:00 CDT
    | Status: R
    |
    | /upgrade_work/func-testcase.sh: function: not found
    | Hello, Dave
    | /upgrade_work/func-testcase.sh: syntax error at line 7: `}'
    | unexpected.
    |
    | When I invoke 'at' it warns me that my job will be run with /bin/sh,
    | but I'm using /bin/sh on the command line without a problem. What am
    | I missing?
    |
    | TIA Dave

    Whatever you're running from the command line that works for you, it's
    not what you show us in your message. A shell funtion is NOT declared by

    function greet {
    echo "Hello"
    }

    but rather by:

    greet () {
    echo "Hello"
    }
    ..

    --
    JP

  3. Re: shell functions not working in 'at' or 'cron'

    On Fri, Jul 11, 2008, dthacker wrote:
    >I've got a script that runs fine from the command line that's bombing
    >out in 'at'. I build a little test case to show what's happening.
    >
    >The script:
    ># Testcase to figure out why
    ># functions are not working in 'at'


    When running jobs in cron or at, many environment variables are
    not set that are set in an interactive shell (e.g. TERM). One
    must be careful to set necessary environment variables in the
    ``at'' script.

    It's easy to check and compare the environment variables. First,
    find the environment in your interactive shell:

    env | sort > /tmp/env.shell

    Now create a very simple shell script that does the same thing in
    your ``at'' or ``cron'' job:

    #!/bin/sh
    env | sort > /tmp/env.at
    exit 0

    Then you can compare /tmp/env.shell and /env/at to see how they
    differ.

    Another thing you can do the help debug the script is to redirect
    its standard error to a file and use ``set -x'' to enable verbose
    output from the scripte

    #!/bin/sh
    exec 2>/tmp/debug.at
    set -x
    # the rest of your script here
    exit 0

    When the ``cron'' or ``at'' script runs, it will send debugging
    output to the /tmp/dbug.at file.

    Bill
    --
    INTERNET: bill@celestial.com Bill Campbell; Celestial Software LLC
    URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way
    Voice: (206) 236-1676 Mercer Island, WA 98040-0820
    Fax: (206) 232-9186

    "The man who does not read good books has no advantage over the man who
    can't read them." -- Mark Twain

  4. Re: shell functions not working in 'at' or 'cron'

    In article <4cbecb3c-7d7d-40af-bb13-48af850d2acb@k30g2000hse.googlegroups.com>,
    dthacker wrote:
    >I've got a script that runs fine from the command line that's bombing
    >out in 'at'. I build a little test case to show what's happening.
    >
    >The script:
    ># Testcase to figure out why
    ># functions are not working in 'at'
    >
    >function greet {
    > echo "Hello, Dave"
    >}
    >
    >echo "script starting"
    >greet
    >echo "script ending"
    >
    >Run from the command line:
    ># echo $SHELL
    >/bin/sh
    ># ./func-testcase.sh
    >script starting
    >Hello, Dave
    >script ending
    >#
    >
    >Now here's the output of the job from 'at':
    >To: root
    >Date: Fri, 11 Jul 2008 11:05:00 CDT
    >Status: R
    >
    >/upgrade_work/func-testcase.sh: function: not found
    >Hello, Dave
    >/upgrade_work/func-testcase.sh: syntax error at line 7: `}'
    >unexpected.
    >
    >When I invoke 'at' it warns me that my job will be run with /bin/sh,
    >but I'm using /bin/sh on the command line without a problem. What am
    >I missing?


    For whatever reason, your script is not being run by /bin/sh when you execute
    it from the command line. /bin/sh doesn't allow functions to be declared with
    'function'; ksh, zsh, and bash do. Declare your function with e.g.
    greet() { ... }, or if you expect to use other non-sh-isms in your programs,
    start the program with #!/bin/ksh or whatever is appropriate.

    John
    --
    John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/

+ Reply to Thread