cron ignores variables defined in “.bashrc” and “.bash_profile”












42















I have defined "SHELL" variable in /etc/crontab file:



[martin@martin ~]$ grep SHELL /etc/crontab 
SHELL=/usr/local/bin/bash
[martin@martin ~]$ file /usr/local/bin/bash
/usr/local/bin/bash: ELF 32-bit LSB executable, Intel 80386, version 1 (FreeBSD), dynamically linked (uses shared libs), for FreeBSD 8.0 (800107), stripped
[martin@martin ~]$


In addition, all my scripts in /etc/crontab file are started under user "martin". However /home/martin/.bash_profile(for login shell) and /home/martin/.bashrc(for non-logging shell) contain some variables which are ignored in case of cron job, but are used in case I log into machine over SSH or open new bash session. Why cron ignores those variables? Isn't cron simply executing "/usr/local/bin/bash my-script.sh" with permissions for user "martin"?










share|improve this question























  • Ubuntu users may like to note that Ubuntu's default .bashrc has a line that stops it from running in non-interactive shells.

    – joeytwiddle
    Oct 4 '18 at 8:38
















42















I have defined "SHELL" variable in /etc/crontab file:



[martin@martin ~]$ grep SHELL /etc/crontab 
SHELL=/usr/local/bin/bash
[martin@martin ~]$ file /usr/local/bin/bash
/usr/local/bin/bash: ELF 32-bit LSB executable, Intel 80386, version 1 (FreeBSD), dynamically linked (uses shared libs), for FreeBSD 8.0 (800107), stripped
[martin@martin ~]$


In addition, all my scripts in /etc/crontab file are started under user "martin". However /home/martin/.bash_profile(for login shell) and /home/martin/.bashrc(for non-logging shell) contain some variables which are ignored in case of cron job, but are used in case I log into machine over SSH or open new bash session. Why cron ignores those variables? Isn't cron simply executing "/usr/local/bin/bash my-script.sh" with permissions for user "martin"?










share|improve this question























  • Ubuntu users may like to note that Ubuntu's default .bashrc has a line that stops it from running in non-interactive shells.

    – joeytwiddle
    Oct 4 '18 at 8:38














42












42








42


10






I have defined "SHELL" variable in /etc/crontab file:



[martin@martin ~]$ grep SHELL /etc/crontab 
SHELL=/usr/local/bin/bash
[martin@martin ~]$ file /usr/local/bin/bash
/usr/local/bin/bash: ELF 32-bit LSB executable, Intel 80386, version 1 (FreeBSD), dynamically linked (uses shared libs), for FreeBSD 8.0 (800107), stripped
[martin@martin ~]$


In addition, all my scripts in /etc/crontab file are started under user "martin". However /home/martin/.bash_profile(for login shell) and /home/martin/.bashrc(for non-logging shell) contain some variables which are ignored in case of cron job, but are used in case I log into machine over SSH or open new bash session. Why cron ignores those variables? Isn't cron simply executing "/usr/local/bin/bash my-script.sh" with permissions for user "martin"?










share|improve this question














I have defined "SHELL" variable in /etc/crontab file:



[martin@martin ~]$ grep SHELL /etc/crontab 
SHELL=/usr/local/bin/bash
[martin@martin ~]$ file /usr/local/bin/bash
/usr/local/bin/bash: ELF 32-bit LSB executable, Intel 80386, version 1 (FreeBSD), dynamically linked (uses shared libs), for FreeBSD 8.0 (800107), stripped
[martin@martin ~]$


In addition, all my scripts in /etc/crontab file are started under user "martin". However /home/martin/.bash_profile(for login shell) and /home/martin/.bashrc(for non-logging shell) contain some variables which are ignored in case of cron job, but are used in case I log into machine over SSH or open new bash session. Why cron ignores those variables? Isn't cron simply executing "/usr/local/bin/bash my-script.sh" with permissions for user "martin"?







bash cron






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 14 '13 at 16:03









MartinMartin

4872471133




4872471133













  • Ubuntu users may like to note that Ubuntu's default .bashrc has a line that stops it from running in non-interactive shells.

    – joeytwiddle
    Oct 4 '18 at 8:38



















  • Ubuntu users may like to note that Ubuntu's default .bashrc has a line that stops it from running in non-interactive shells.

    – joeytwiddle
    Oct 4 '18 at 8:38

















Ubuntu users may like to note that Ubuntu's default .bashrc has a line that stops it from running in non-interactive shells.

– joeytwiddle
Oct 4 '18 at 8:38





Ubuntu users may like to note that Ubuntu's default .bashrc has a line that stops it from running in non-interactive shells.

– joeytwiddle
Oct 4 '18 at 8:38










6 Answers
6






active

oldest

votes


















63





+100









You can source the file you want at the top of the script or beginning of the job for the user that is executing the job. The "source" command is a built-in. You'd do the same thing if you made edits to those files to load the changes.



* * * * * source /home/user/.bash_profile; <command>


or



#!/bin/bash
source /home/user/.bash_profile

<commands>





share|improve this answer



















  • 2





    Note that "source" might not work if cron is not using the bash shell. I've added an answer that can handle the case when the shell is sh.

    – Jonathan
    May 3 '17 at 7:40











  • ^ source not found error fix

    – Danny Bullis
    Feb 1 '18 at 1:33





















20














Because it's not an interactive shell. The same happens when you open some terminals.



Have a look at this question: What is the .bashrc file? | Super User



And also at this one:



What's the difference between .bashrc, .bash_profile, and .environment? | Stack Overflow



Different scripts fire depending on if the connection is a login shell (or not), an interactive shell (or not), or both.



If you want to make bashrc you'll need to make this change:




When Bash is started non-interactively, to run a shell script, for
example, it looks for the variable BASH_ENV in the environment,
expands its value if it appears there, and uses the expanded value as
the name of a file to read and execute. Bash behaves as if the
following command were executed:



if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi 


but the value of the PATH variable is not used to search for the file name.



As noted above, if a non-interactive shell is invoked with the --login option, Bash attempts to read and execute commands from the login shell startup files.




Source: Bash Startup Files | Bash Reference Manual | gnu.org






share|improve this answer


























  • So if we set BASH_ENV inside Cron, cron bash scripts will source that because cron is non-interactive and non-login.

    – CMCDragonkai
    Dec 11 '15 at 7:19



















10














You may not be able to run source if the sh shell is being used. This can be changed by adding the following line in your crontab:



SHELL=/bin/bash
* * * * * source "/root/.bashrc"; <command>


You can also specify the environment:



BASH_ENV="/root/.bashrc"
* * * * * <command>


or you can use your local /home/user/.bashrc if it is a user cron job (e.g. crontab -e).



Note that .bash_profile can replace .bashrc, if it exists.



Credit: How to change cron shell (sh to bash)?






share|improve this answer


























  • this works well also for Acquia Cloud Scheduled jobs, which are basically cron jobs. You can do the same, like: SHELL=/bin/bash && source /home/YOUR_USER_NAME/.bash_profile && sh ....

    – Alejandro Moreno
    Jan 22 at 17:58





















0














bash acts differently whether it is a shell or a normal progamming language (like perl or python).



By designed, those settings in ~/.bash_profile, ~/.bashrc, etc. are for users to set things when bash plays the roll of a shell (login shell, interractive shell). Think about environment you have in a xterm (interractive shell) or in ssh sessions (login shell) or in consoles (login shell).



In other hand, bash is also a powerfull progamming language --think about many scripts for managing services in systemd-- which requires a different style of working. Example, when a developer write a system script or a bash program, he/she will not likely to source user defined ~/.bash_profile automatically. It is a normal program, not a shell. A normal program (including bash programs) would naturally inherrit setting in a current working evironement (shell), but not set them.



If we write a program for cron in bash --it is just happenly it is written in bash; in fact, we can write it in python or perl or any other progamming language-- then, we can have an option to sources bash's ~/.bash_profile (read: setting of user's shell, which happenly to be the same language of your programming language):



[ -f /home/user/.bash_profile ] && . /home/user/.bash_profile


However, what if that particular user do not use bash as his/her shell? He/she may use zsh, 'ksh,fish`, etc. So, that's practice does not really work when writing program for public use.



So, you can source ~/.bash_profile if you think it work. But, here, it is not about whether we are able to source a file, it is about how things should works in the system: the design concept. In short: we should view bash as something having 2 rolls: shell and progamming language. Then everything will be clear, easier to understand.






share|improve this answer

































    0














    Something else that might interfere with the sourcing of your .bashrc from a cronjob is any checks that this file does in order to detect interactive shells.



    For example, on Ubuntu 18.04, the default .bashrc for a user starts with this:



    # ~/.bashrc: executed by bash(1) for non-login shells.
    # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
    # for examples

    # If not running interactively, don't do anything
    case $- in
    *i*) ;;
    *) return;;
    esac


    and so sourcing it won't do anything useful since it will exit immediately.





    share








    New contributor




    Francois Marier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.




























      -1














      My way to deal with it was this:



      1) Putting my variables in (the end of) ~/.profile:



      myVarInDotProfile="someValue"


      2) Creating a Bash script for my (daily) cron tasks (~/cronDaily.sh) containing my commands plus repetitive sourcing of ~/.profle:



      source ~/.profile
      command ${myVarInDotProfile}/


      3) scheduling my script's execution from crontab, to run daily:



      0 0 * * * bash ~/cronDaily.sh


      My variable wasn't ignored and the commands ran successfully.





      Some may say such intense sourcing of ~/.profile is problematic. In my particular case I don't see why it is a problem but I would advice to consider creating a dedicated file for that.



      In general, there might be a better way to this but that's what worked for me after a lot of pain and it explains the principle that as of Bash 4.3.46, you cannot source a file from crontab.






      share|improve this answer

























        Your Answer








        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "106"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f67940%2fcron-ignores-variables-defined-in-bashrc-and-bash-profile%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        6 Answers
        6






        active

        oldest

        votes








        6 Answers
        6






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        63





        +100









        You can source the file you want at the top of the script or beginning of the job for the user that is executing the job. The "source" command is a built-in. You'd do the same thing if you made edits to those files to load the changes.



        * * * * * source /home/user/.bash_profile; <command>


        or



        #!/bin/bash
        source /home/user/.bash_profile

        <commands>





        share|improve this answer



















        • 2





          Note that "source" might not work if cron is not using the bash shell. I've added an answer that can handle the case when the shell is sh.

          – Jonathan
          May 3 '17 at 7:40











        • ^ source not found error fix

          – Danny Bullis
          Feb 1 '18 at 1:33


















        63





        +100









        You can source the file you want at the top of the script or beginning of the job for the user that is executing the job. The "source" command is a built-in. You'd do the same thing if you made edits to those files to load the changes.



        * * * * * source /home/user/.bash_profile; <command>


        or



        #!/bin/bash
        source /home/user/.bash_profile

        <commands>





        share|improve this answer



















        • 2





          Note that "source" might not work if cron is not using the bash shell. I've added an answer that can handle the case when the shell is sh.

          – Jonathan
          May 3 '17 at 7:40











        • ^ source not found error fix

          – Danny Bullis
          Feb 1 '18 at 1:33
















        63





        +100







        63





        +100



        63




        +100





        You can source the file you want at the top of the script or beginning of the job for the user that is executing the job. The "source" command is a built-in. You'd do the same thing if you made edits to those files to load the changes.



        * * * * * source /home/user/.bash_profile; <command>


        or



        #!/bin/bash
        source /home/user/.bash_profile

        <commands>





        share|improve this answer













        You can source the file you want at the top of the script or beginning of the job for the user that is executing the job. The "source" command is a built-in. You'd do the same thing if you made edits to those files to load the changes.



        * * * * * source /home/user/.bash_profile; <command>


        or



        #!/bin/bash
        source /home/user/.bash_profile

        <commands>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 14 '13 at 18:09









        gNU.begNU.be

        84663




        84663








        • 2





          Note that "source" might not work if cron is not using the bash shell. I've added an answer that can handle the case when the shell is sh.

          – Jonathan
          May 3 '17 at 7:40











        • ^ source not found error fix

          – Danny Bullis
          Feb 1 '18 at 1:33
















        • 2





          Note that "source" might not work if cron is not using the bash shell. I've added an answer that can handle the case when the shell is sh.

          – Jonathan
          May 3 '17 at 7:40











        • ^ source not found error fix

          – Danny Bullis
          Feb 1 '18 at 1:33










        2




        2





        Note that "source" might not work if cron is not using the bash shell. I've added an answer that can handle the case when the shell is sh.

        – Jonathan
        May 3 '17 at 7:40





        Note that "source" might not work if cron is not using the bash shell. I've added an answer that can handle the case when the shell is sh.

        – Jonathan
        May 3 '17 at 7:40













        ^ source not found error fix

        – Danny Bullis
        Feb 1 '18 at 1:33







        ^ source not found error fix

        – Danny Bullis
        Feb 1 '18 at 1:33















        20














        Because it's not an interactive shell. The same happens when you open some terminals.



        Have a look at this question: What is the .bashrc file? | Super User



        And also at this one:



        What's the difference between .bashrc, .bash_profile, and .environment? | Stack Overflow



        Different scripts fire depending on if the connection is a login shell (or not), an interactive shell (or not), or both.



        If you want to make bashrc you'll need to make this change:




        When Bash is started non-interactively, to run a shell script, for
        example, it looks for the variable BASH_ENV in the environment,
        expands its value if it appears there, and uses the expanded value as
        the name of a file to read and execute. Bash behaves as if the
        following command were executed:



        if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi 


        but the value of the PATH variable is not used to search for the file name.



        As noted above, if a non-interactive shell is invoked with the --login option, Bash attempts to read and execute commands from the login shell startup files.




        Source: Bash Startup Files | Bash Reference Manual | gnu.org






        share|improve this answer


























        • So if we set BASH_ENV inside Cron, cron bash scripts will source that because cron is non-interactive and non-login.

          – CMCDragonkai
          Dec 11 '15 at 7:19
















        20














        Because it's not an interactive shell. The same happens when you open some terminals.



        Have a look at this question: What is the .bashrc file? | Super User



        And also at this one:



        What's the difference between .bashrc, .bash_profile, and .environment? | Stack Overflow



        Different scripts fire depending on if the connection is a login shell (or not), an interactive shell (or not), or both.



        If you want to make bashrc you'll need to make this change:




        When Bash is started non-interactively, to run a shell script, for
        example, it looks for the variable BASH_ENV in the environment,
        expands its value if it appears there, and uses the expanded value as
        the name of a file to read and execute. Bash behaves as if the
        following command were executed:



        if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi 


        but the value of the PATH variable is not used to search for the file name.



        As noted above, if a non-interactive shell is invoked with the --login option, Bash attempts to read and execute commands from the login shell startup files.




        Source: Bash Startup Files | Bash Reference Manual | gnu.org






        share|improve this answer


























        • So if we set BASH_ENV inside Cron, cron bash scripts will source that because cron is non-interactive and non-login.

          – CMCDragonkai
          Dec 11 '15 at 7:19














        20












        20








        20







        Because it's not an interactive shell. The same happens when you open some terminals.



        Have a look at this question: What is the .bashrc file? | Super User



        And also at this one:



        What's the difference between .bashrc, .bash_profile, and .environment? | Stack Overflow



        Different scripts fire depending on if the connection is a login shell (or not), an interactive shell (or not), or both.



        If you want to make bashrc you'll need to make this change:




        When Bash is started non-interactively, to run a shell script, for
        example, it looks for the variable BASH_ENV in the environment,
        expands its value if it appears there, and uses the expanded value as
        the name of a file to read and execute. Bash behaves as if the
        following command were executed:



        if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi 


        but the value of the PATH variable is not used to search for the file name.



        As noted above, if a non-interactive shell is invoked with the --login option, Bash attempts to read and execute commands from the login shell startup files.




        Source: Bash Startup Files | Bash Reference Manual | gnu.org






        share|improve this answer















        Because it's not an interactive shell. The same happens when you open some terminals.



        Have a look at this question: What is the .bashrc file? | Super User



        And also at this one:



        What's the difference between .bashrc, .bash_profile, and .environment? | Stack Overflow



        Different scripts fire depending on if the connection is a login shell (or not), an interactive shell (or not), or both.



        If you want to make bashrc you'll need to make this change:




        When Bash is started non-interactively, to run a shell script, for
        example, it looks for the variable BASH_ENV in the environment,
        expands its value if it appears there, and uses the expanded value as
        the name of a file to read and execute. Bash behaves as if the
        following command were executed:



        if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi 


        but the value of the PATH variable is not used to search for the file name.



        As noted above, if a non-interactive shell is invoked with the --login option, Bash attempts to read and execute commands from the login shell startup files.




        Source: Bash Startup Files | Bash Reference Manual | gnu.org







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 5 '18 at 20:25









        Drakonoved

        7171621




        7171621










        answered Mar 14 '13 at 16:15









        Dave CDave C

        709314




        709314













        • So if we set BASH_ENV inside Cron, cron bash scripts will source that because cron is non-interactive and non-login.

          – CMCDragonkai
          Dec 11 '15 at 7:19



















        • So if we set BASH_ENV inside Cron, cron bash scripts will source that because cron is non-interactive and non-login.

          – CMCDragonkai
          Dec 11 '15 at 7:19

















        So if we set BASH_ENV inside Cron, cron bash scripts will source that because cron is non-interactive and non-login.

        – CMCDragonkai
        Dec 11 '15 at 7:19





        So if we set BASH_ENV inside Cron, cron bash scripts will source that because cron is non-interactive and non-login.

        – CMCDragonkai
        Dec 11 '15 at 7:19











        10














        You may not be able to run source if the sh shell is being used. This can be changed by adding the following line in your crontab:



        SHELL=/bin/bash
        * * * * * source "/root/.bashrc"; <command>


        You can also specify the environment:



        BASH_ENV="/root/.bashrc"
        * * * * * <command>


        or you can use your local /home/user/.bashrc if it is a user cron job (e.g. crontab -e).



        Note that .bash_profile can replace .bashrc, if it exists.



        Credit: How to change cron shell (sh to bash)?






        share|improve this answer


























        • this works well also for Acquia Cloud Scheduled jobs, which are basically cron jobs. You can do the same, like: SHELL=/bin/bash && source /home/YOUR_USER_NAME/.bash_profile && sh ....

          – Alejandro Moreno
          Jan 22 at 17:58


















        10














        You may not be able to run source if the sh shell is being used. This can be changed by adding the following line in your crontab:



        SHELL=/bin/bash
        * * * * * source "/root/.bashrc"; <command>


        You can also specify the environment:



        BASH_ENV="/root/.bashrc"
        * * * * * <command>


        or you can use your local /home/user/.bashrc if it is a user cron job (e.g. crontab -e).



        Note that .bash_profile can replace .bashrc, if it exists.



        Credit: How to change cron shell (sh to bash)?






        share|improve this answer


























        • this works well also for Acquia Cloud Scheduled jobs, which are basically cron jobs. You can do the same, like: SHELL=/bin/bash && source /home/YOUR_USER_NAME/.bash_profile && sh ....

          – Alejandro Moreno
          Jan 22 at 17:58
















        10












        10








        10







        You may not be able to run source if the sh shell is being used. This can be changed by adding the following line in your crontab:



        SHELL=/bin/bash
        * * * * * source "/root/.bashrc"; <command>


        You can also specify the environment:



        BASH_ENV="/root/.bashrc"
        * * * * * <command>


        or you can use your local /home/user/.bashrc if it is a user cron job (e.g. crontab -e).



        Note that .bash_profile can replace .bashrc, if it exists.



        Credit: How to change cron shell (sh to bash)?






        share|improve this answer















        You may not be able to run source if the sh shell is being used. This can be changed by adding the following line in your crontab:



        SHELL=/bin/bash
        * * * * * source "/root/.bashrc"; <command>


        You can also specify the environment:



        BASH_ENV="/root/.bashrc"
        * * * * * <command>


        or you can use your local /home/user/.bashrc if it is a user cron job (e.g. crontab -e).



        Note that .bash_profile can replace .bashrc, if it exists.



        Credit: How to change cron shell (sh to bash)?







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 3 '17 at 7:48

























        answered May 3 '17 at 7:37









        JonathanJonathan

        3751412




        3751412













        • this works well also for Acquia Cloud Scheduled jobs, which are basically cron jobs. You can do the same, like: SHELL=/bin/bash && source /home/YOUR_USER_NAME/.bash_profile && sh ....

          – Alejandro Moreno
          Jan 22 at 17:58





















        • this works well also for Acquia Cloud Scheduled jobs, which are basically cron jobs. You can do the same, like: SHELL=/bin/bash && source /home/YOUR_USER_NAME/.bash_profile && sh ....

          – Alejandro Moreno
          Jan 22 at 17:58



















        this works well also for Acquia Cloud Scheduled jobs, which are basically cron jobs. You can do the same, like: SHELL=/bin/bash && source /home/YOUR_USER_NAME/.bash_profile && sh ....

        – Alejandro Moreno
        Jan 22 at 17:58







        this works well also for Acquia Cloud Scheduled jobs, which are basically cron jobs. You can do the same, like: SHELL=/bin/bash && source /home/YOUR_USER_NAME/.bash_profile && sh ....

        – Alejandro Moreno
        Jan 22 at 17:58













        0














        bash acts differently whether it is a shell or a normal progamming language (like perl or python).



        By designed, those settings in ~/.bash_profile, ~/.bashrc, etc. are for users to set things when bash plays the roll of a shell (login shell, interractive shell). Think about environment you have in a xterm (interractive shell) or in ssh sessions (login shell) or in consoles (login shell).



        In other hand, bash is also a powerfull progamming language --think about many scripts for managing services in systemd-- which requires a different style of working. Example, when a developer write a system script or a bash program, he/she will not likely to source user defined ~/.bash_profile automatically. It is a normal program, not a shell. A normal program (including bash programs) would naturally inherrit setting in a current working evironement (shell), but not set them.



        If we write a program for cron in bash --it is just happenly it is written in bash; in fact, we can write it in python or perl or any other progamming language-- then, we can have an option to sources bash's ~/.bash_profile (read: setting of user's shell, which happenly to be the same language of your programming language):



        [ -f /home/user/.bash_profile ] && . /home/user/.bash_profile


        However, what if that particular user do not use bash as his/her shell? He/she may use zsh, 'ksh,fish`, etc. So, that's practice does not really work when writing program for public use.



        So, you can source ~/.bash_profile if you think it work. But, here, it is not about whether we are able to source a file, it is about how things should works in the system: the design concept. In short: we should view bash as something having 2 rolls: shell and progamming language. Then everything will be clear, easier to understand.






        share|improve this answer






























          0














          bash acts differently whether it is a shell or a normal progamming language (like perl or python).



          By designed, those settings in ~/.bash_profile, ~/.bashrc, etc. are for users to set things when bash plays the roll of a shell (login shell, interractive shell). Think about environment you have in a xterm (interractive shell) or in ssh sessions (login shell) or in consoles (login shell).



          In other hand, bash is also a powerfull progamming language --think about many scripts for managing services in systemd-- which requires a different style of working. Example, when a developer write a system script or a bash program, he/she will not likely to source user defined ~/.bash_profile automatically. It is a normal program, not a shell. A normal program (including bash programs) would naturally inherrit setting in a current working evironement (shell), but not set them.



          If we write a program for cron in bash --it is just happenly it is written in bash; in fact, we can write it in python or perl or any other progamming language-- then, we can have an option to sources bash's ~/.bash_profile (read: setting of user's shell, which happenly to be the same language of your programming language):



          [ -f /home/user/.bash_profile ] && . /home/user/.bash_profile


          However, what if that particular user do not use bash as his/her shell? He/she may use zsh, 'ksh,fish`, etc. So, that's practice does not really work when writing program for public use.



          So, you can source ~/.bash_profile if you think it work. But, here, it is not about whether we are able to source a file, it is about how things should works in the system: the design concept. In short: we should view bash as something having 2 rolls: shell and progamming language. Then everything will be clear, easier to understand.






          share|improve this answer




























            0












            0








            0







            bash acts differently whether it is a shell or a normal progamming language (like perl or python).



            By designed, those settings in ~/.bash_profile, ~/.bashrc, etc. are for users to set things when bash plays the roll of a shell (login shell, interractive shell). Think about environment you have in a xterm (interractive shell) or in ssh sessions (login shell) or in consoles (login shell).



            In other hand, bash is also a powerfull progamming language --think about many scripts for managing services in systemd-- which requires a different style of working. Example, when a developer write a system script or a bash program, he/she will not likely to source user defined ~/.bash_profile automatically. It is a normal program, not a shell. A normal program (including bash programs) would naturally inherrit setting in a current working evironement (shell), but not set them.



            If we write a program for cron in bash --it is just happenly it is written in bash; in fact, we can write it in python or perl or any other progamming language-- then, we can have an option to sources bash's ~/.bash_profile (read: setting of user's shell, which happenly to be the same language of your programming language):



            [ -f /home/user/.bash_profile ] && . /home/user/.bash_profile


            However, what if that particular user do not use bash as his/her shell? He/she may use zsh, 'ksh,fish`, etc. So, that's practice does not really work when writing program for public use.



            So, you can source ~/.bash_profile if you think it work. But, here, it is not about whether we are able to source a file, it is about how things should works in the system: the design concept. In short: we should view bash as something having 2 rolls: shell and progamming language. Then everything will be clear, easier to understand.






            share|improve this answer















            bash acts differently whether it is a shell or a normal progamming language (like perl or python).



            By designed, those settings in ~/.bash_profile, ~/.bashrc, etc. are for users to set things when bash plays the roll of a shell (login shell, interractive shell). Think about environment you have in a xterm (interractive shell) or in ssh sessions (login shell) or in consoles (login shell).



            In other hand, bash is also a powerfull progamming language --think about many scripts for managing services in systemd-- which requires a different style of working. Example, when a developer write a system script or a bash program, he/she will not likely to source user defined ~/.bash_profile automatically. It is a normal program, not a shell. A normal program (including bash programs) would naturally inherrit setting in a current working evironement (shell), but not set them.



            If we write a program for cron in bash --it is just happenly it is written in bash; in fact, we can write it in python or perl or any other progamming language-- then, we can have an option to sources bash's ~/.bash_profile (read: setting of user's shell, which happenly to be the same language of your programming language):



            [ -f /home/user/.bash_profile ] && . /home/user/.bash_profile


            However, what if that particular user do not use bash as his/her shell? He/she may use zsh, 'ksh,fish`, etc. So, that's practice does not really work when writing program for public use.



            So, you can source ~/.bash_profile if you think it work. But, here, it is not about whether we are able to source a file, it is about how things should works in the system: the design concept. In short: we should view bash as something having 2 rolls: shell and progamming language. Then everything will be clear, easier to understand.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 19 '18 at 16:21

























            answered Jan 19 '18 at 16:15









            Bach LienBach Lien

            1792




            1792























                0














                Something else that might interfere with the sourcing of your .bashrc from a cronjob is any checks that this file does in order to detect interactive shells.



                For example, on Ubuntu 18.04, the default .bashrc for a user starts with this:



                # ~/.bashrc: executed by bash(1) for non-login shells.
                # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
                # for examples

                # If not running interactively, don't do anything
                case $- in
                *i*) ;;
                *) return;;
                esac


                and so sourcing it won't do anything useful since it will exit immediately.





                share








                New contributor




                Francois Marier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.

























                  0














                  Something else that might interfere with the sourcing of your .bashrc from a cronjob is any checks that this file does in order to detect interactive shells.



                  For example, on Ubuntu 18.04, the default .bashrc for a user starts with this:



                  # ~/.bashrc: executed by bash(1) for non-login shells.
                  # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
                  # for examples

                  # If not running interactively, don't do anything
                  case $- in
                  *i*) ;;
                  *) return;;
                  esac


                  and so sourcing it won't do anything useful since it will exit immediately.





                  share








                  New contributor




                  Francois Marier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.























                    0












                    0








                    0







                    Something else that might interfere with the sourcing of your .bashrc from a cronjob is any checks that this file does in order to detect interactive shells.



                    For example, on Ubuntu 18.04, the default .bashrc for a user starts with this:



                    # ~/.bashrc: executed by bash(1) for non-login shells.
                    # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
                    # for examples

                    # If not running interactively, don't do anything
                    case $- in
                    *i*) ;;
                    *) return;;
                    esac


                    and so sourcing it won't do anything useful since it will exit immediately.





                    share








                    New contributor




                    Francois Marier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.










                    Something else that might interfere with the sourcing of your .bashrc from a cronjob is any checks that this file does in order to detect interactive shells.



                    For example, on Ubuntu 18.04, the default .bashrc for a user starts with this:



                    # ~/.bashrc: executed by bash(1) for non-login shells.
                    # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
                    # for examples

                    # If not running interactively, don't do anything
                    case $- in
                    *i*) ;;
                    *) return;;
                    esac


                    and so sourcing it won't do anything useful since it will exit immediately.






                    share








                    New contributor




                    Francois Marier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.








                    share


                    share






                    New contributor




                    Francois Marier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.









                    answered 7 mins ago









                    Francois MarierFrancois Marier

                    1033




                    1033




                    New contributor




                    Francois Marier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.





                    New contributor





                    Francois Marier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.






                    Francois Marier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.























                        -1














                        My way to deal with it was this:



                        1) Putting my variables in (the end of) ~/.profile:



                        myVarInDotProfile="someValue"


                        2) Creating a Bash script for my (daily) cron tasks (~/cronDaily.sh) containing my commands plus repetitive sourcing of ~/.profle:



                        source ~/.profile
                        command ${myVarInDotProfile}/


                        3) scheduling my script's execution from crontab, to run daily:



                        0 0 * * * bash ~/cronDaily.sh


                        My variable wasn't ignored and the commands ran successfully.





                        Some may say such intense sourcing of ~/.profile is problematic. In my particular case I don't see why it is a problem but I would advice to consider creating a dedicated file for that.



                        In general, there might be a better way to this but that's what worked for me after a lot of pain and it explains the principle that as of Bash 4.3.46, you cannot source a file from crontab.






                        share|improve this answer






























                          -1














                          My way to deal with it was this:



                          1) Putting my variables in (the end of) ~/.profile:



                          myVarInDotProfile="someValue"


                          2) Creating a Bash script for my (daily) cron tasks (~/cronDaily.sh) containing my commands plus repetitive sourcing of ~/.profle:



                          source ~/.profile
                          command ${myVarInDotProfile}/


                          3) scheduling my script's execution from crontab, to run daily:



                          0 0 * * * bash ~/cronDaily.sh


                          My variable wasn't ignored and the commands ran successfully.





                          Some may say such intense sourcing of ~/.profile is problematic. In my particular case I don't see why it is a problem but I would advice to consider creating a dedicated file for that.



                          In general, there might be a better way to this but that's what worked for me after a lot of pain and it explains the principle that as of Bash 4.3.46, you cannot source a file from crontab.






                          share|improve this answer




























                            -1












                            -1








                            -1







                            My way to deal with it was this:



                            1) Putting my variables in (the end of) ~/.profile:



                            myVarInDotProfile="someValue"


                            2) Creating a Bash script for my (daily) cron tasks (~/cronDaily.sh) containing my commands plus repetitive sourcing of ~/.profle:



                            source ~/.profile
                            command ${myVarInDotProfile}/


                            3) scheduling my script's execution from crontab, to run daily:



                            0 0 * * * bash ~/cronDaily.sh


                            My variable wasn't ignored and the commands ran successfully.





                            Some may say such intense sourcing of ~/.profile is problematic. In my particular case I don't see why it is a problem but I would advice to consider creating a dedicated file for that.



                            In general, there might be a better way to this but that's what worked for me after a lot of pain and it explains the principle that as of Bash 4.3.46, you cannot source a file from crontab.






                            share|improve this answer















                            My way to deal with it was this:



                            1) Putting my variables in (the end of) ~/.profile:



                            myVarInDotProfile="someValue"


                            2) Creating a Bash script for my (daily) cron tasks (~/cronDaily.sh) containing my commands plus repetitive sourcing of ~/.profle:



                            source ~/.profile
                            command ${myVarInDotProfile}/


                            3) scheduling my script's execution from crontab, to run daily:



                            0 0 * * * bash ~/cronDaily.sh


                            My variable wasn't ignored and the commands ran successfully.





                            Some may say such intense sourcing of ~/.profile is problematic. In my particular case I don't see why it is a problem but I would advice to consider creating a dedicated file for that.



                            In general, there might be a better way to this but that's what worked for me after a lot of pain and it explains the principle that as of Bash 4.3.46, you cannot source a file from crontab.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 19 '18 at 18:08

























                            answered Jan 19 '18 at 17:51









                            ArcticoolingArcticooling

                            921226




                            921226






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f67940%2fcron-ignores-variables-defined-in-bashrc-and-bash-profile%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Accessing regular linux commands in Huawei's Dopra Linux

                                Can't connect RFCOMM socket: Host is down

                                Kernel panic - not syncing: Fatal Exception in Interrupt