bash - How do I run a shell script with export command in crontab?

06
2013-08
  • Rudra

    I have a shell script that exports values of variables when executed. The same values will be used in another script.

    #!/bin/sh
    export I="10"
    echo $I
    

    I will be using root access for cron. I tried this command:

    */5 * * * * /home/ubuntu/backup/.test.sh
    

    After it ran, I checked the environment variables, and nothing was updated.

    How do I run this script in cron and get it to properly export variables?

  • Answers
  • Paul

    export only makes variables available to sub processes, not to existing shells, or even shells that run later.

    Only those called by test.sh would see the new environment variables.

    While this answers your question, you may want to edit it to describe what you are trying to achieve - why you want to do this, so that you'll get better answers, different approaches.


  • Related Question

    centos - How to use crontab to run a script as nobody
  • Questioner

    This is on a CentOS machine. I'm trying to run a script as user nobody (or as a user with minimal permissions) at a certain time every day. Here is nobody:

    [root@CentOS % ~] grep "^nobody" /etc/passwd  
    nobody:x:99:99:Nobody:/:/sbin/nologin  
    

    here's what I've tried in root's crontab:

    setting the Environment variable SUDO_USER=nobody
    15 17 * * * sudo -u nobody /bin/bash /usr/local/bin/bashscript.sh
    15 17 * * * su -c /usr/local/bin/bashscript.sh nobody

    I'd like to keep the crontab entry in root's crontab if at all possible. I'd also prefer not fooling with user nobody's account, as I don't want to break anything else that might rely on those settings. I'm not adverse to creating another non-privileged account and giving them a real shell if that's the sticking point.

    I'll also admit to being a bit perplexed. I would assume this would be an everyday issue, except my brown belt in google-fu isn't helping much.


  • Related Answers
  • Oliver Salzburg

    I'm guessing you are posting the contents of crontab -e or crontab -l?

    This is the crontab file beloning to user "root", and that file does not support specifying a user to run the command as (as it's generally a file used for scheduling personal jobs).
    Look at /etc/crontab instead which is the system-wide crontab and has an additional field: the user field. Try adding a line like this to /etc/crontab:

    15 17 * * * nobody /usr/local/bin/bashscript.sh
    
  • Kitty

    su --shell=/bin/bash --session-command="/path/to/command -argument=something" username &

    Works for me and doesn't throw the "This account is currently not available." error even when the user doesn't have a valid login shell

  • Area 51

    Workaround instead of a real answer:

    You can add a comment to your crontab

    **#see crontab -u nobody -l for something that runs everyday at 1:15 AM**
    

    and then just add an entry to the user "nobody" crontab. I used:

    **15 1 *  *  *   /usr/local/bin/script.sh #comment**