How can I run a cron command with existing environmental variables?

  • How can I run a cron command with existing environmental variables?

    If I am at a shell prompt I can type echo $ORACLE_HOME and get a path. This is one of my environmental variables that gets set in my ~/.profile. However, it seems that ~/.profile does not get loaded fron cron scripts and so my scripts fail because the $ORACLE_HOME variable is not set.

    In this question the author mentions creating a ~/.cronfile profile which sets up variables for cron, and then he does a workaround to load all his cron commands into scripts he keeps in his ~/Cron directory. A file like ~/.cronfile sounds like a good idea, but the rest of the answer seems a little cumbersome and I was hoping someone could tell me an easier way to get the same result.

    I suppose at the start of my scripts I could add something like source ~/.profile but that seems like it could be redundant.

    So how can I get make my cron scripts load the variables from my interactive-shell profile?

    How is adding `source ~/.profile` to a program redundant? Programs inherit their environment from the calling program. If that calling program is not your shell, then how will the decendant program get the environment that you want?

    I already wrote my answer to a similar question here. It simply uses `su -l` to setup a normal login environment including the $PATH for either root or other specific user.

  • Arcege

    Arcege Correct answer

    9 years ago

    In the crontab, before you command, add . $HOME/.profile. For example:

    0 5 * * * . $HOME/.profile; /path/to/command/to/run

    Cron knows nothing about your shell; it is started by the system, so it has a minimal environment. If you want anything, you need to have that brought in yourself.

    What does the `.` before the script do? (not sure how I would `man` that). Why is this different from `source`?

    The `.` command is the original command for `source`. They are equivalent within the shell and a bit easier to type, especially within a crontab. To get more info, type `help .` or search for `^SHELL BUILTIN COMMANDS` in the man page for `bash` or at the top of `man `zshbuiltins`. Running `type .` will tell you that the command is a builtin.

    Depending on Linux distributions, you may need to change `.profile` by `.bash_profile`. Check which `.profile` file exists in the user's home directory.

    @Arcege This doesn't work for me (Fedora Core 21), and I presume that's because the shell level drops back down. Instead, what DOES work is if you source it.

    It's likely that if it isn't working, it's because the SHELL for the cron script isn't set to bash, so it's not executing the same way you might expect.

    In Ubuntu, `.profile` just calls `.bashrc` which has explicit code to exit if run interactively.

    @cwd, did you try `man .`? It works on my system. (It pulls up the man page for Bash builtins, the first of which is `.`.)

    The file was called .bash_profile on my distro (Amazon linux). Worked great.

    I tried with . .bash_profile which works :-)

    The solution is not working for SunOS 5.9

    Was under the impression that sourcing the .profile and then using `;` would cause it to be run in another process that doesn't actually give them over to the command, and that it should be done as `$HOME/.profile && /some/command/here`

    This does not work for `.bashrc` on Ubuntu 18.04.3 LTS

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM