What is the environment for cron?












0















When issues arise using cron to schedule events, a frequently-heard explanation is that cron runs with a different set of environment variables than a "normal" user (e.g. pi). That's all well and good, but what is the environment for the cron user? If one is to avoid errors due to an incorrect environment when using cron, it would be useful to know what that environment is.










share|improve this question



























    0















    When issues arise using cron to schedule events, a frequently-heard explanation is that cron runs with a different set of environment variables than a "normal" user (e.g. pi). That's all well and good, but what is the environment for the cron user? If one is to avoid errors due to an incorrect environment when using cron, it would be useful to know what that environment is.










    share|improve this question

























      0












      0








      0








      When issues arise using cron to schedule events, a frequently-heard explanation is that cron runs with a different set of environment variables than a "normal" user (e.g. pi). That's all well and good, but what is the environment for the cron user? If one is to avoid errors due to an incorrect environment when using cron, it would be useful to know what that environment is.










      share|improve this question














      When issues arise using cron to schedule events, a frequently-heard explanation is that cron runs with a different set of environment variables than a "normal" user (e.g. pi). That's all well and good, but what is the environment for the cron user? If one is to avoid errors due to an incorrect environment when using cron, it would be useful to know what that environment is.







      cron






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 20 hours ago









      SeamusSeamus

      2,8501321




      2,8501321






















          1 Answer
          1






          active

          oldest

          votes


















          4














          We can ask cron to tell us what its environment is.




          • Create a shell script in your home directory (~/) as follows (or with the editor of your choice):


          $ nano ~/envtst.sh



          • Enter/C+P the following in the editor:


          #!/bin/sh 
          echo "env report follows for user "$USER >> /home/pi/envtst.sh.out
          env >> /home/pi/envtst.sh.out
          echo "env report for user "$USER" concluded" >> /home/pi/envtst.sh.out
          echo " " >> /home/pi/envtst.sh.out



          • Save the file and exit the editor; then set the file permissions as executable, and open your crontab for editing:


          $ chmod a+rx ~/envtst.sh
          $ crontab -e



          • Enter the following line at the bottom of your crontab:


          * * * * *  /home/pi/envtst.sh >> /home/pi/envtst.sh.err 2>&1



          • Save and exit your crontab. Use tail to view the output & (hopefully) observe the environment for cron. If there's nothing in the file after a minute, view the file ~/envtst.sh.err for error messages, and adjust as required. (NOTE: If you want to clear all prior error messages after troubleshooting: $ > ~/envtst.sh.err )


          crontab: installing new crontab
          $ tail -f ~/envtst.sh.out
          env report follows for user
          HOME=/home/pi
          LOGNAME=pi
          PATH=/usr/bin:/bin
          LANG=en_GB.UTF-8
          SHELL=/bin/sh
          PWD=/home/pi
          env report for user concluded



          This will repeat every minute, so enter ^C to stop the tail listing, edit your crontab again to "comment out" (or delete) the line just added. Save and exit the editor.




          • Note in the tail output above that cron has a rather sparse environment; only six (6) variables are used to define it. Note the PATH consists of only two directories. This is why your crontab entry fails if, for example, you're trying to launch a Python script that resides in your home directory. Note also that the user name (aka LOGNAME iaw System V) isn't cron - it's pi!


          • If you're not familiar, with your own user environment, it's useful to compare it against the cron environment. We'll use the same shell script to add that to the "output" file ~/envtst.sh.out:



          $ ~/envtst.sh 
          $



          • To view the output, open ~/envtst.sh.outin your editor, or cat ~/envtst.sh.out to see it in your terminal. It will likely be a fairly extensive output; 30 lines of text, more or less. Note in particular the following lines (assuming you've run this as user pi) :


          USER=pi
          ...
          HOME=/home/pi
          LOGNAME=pi
          _=/home/pi/envtst.sh
          ...
          PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
          ...
          SHELL=/bin/bash



          • You'll notice numerous differences in the two environments. This will help create rational cron jobs, and help troubleshooting when they don't behave as you'd like.






          share|improve this answer


























          • Very nice! For some reason your $USER variable isn't set -- see env report for user concluded.

            – Mark Smith
            18 hours ago











          • @MarkSmith: If you're referring to the absence of $USER in the cron environment, it's not that it's not set... it's just not used in the version of cron on Raspbian (and Debian I think). Here's some more on that, and still more, and more. Likely more to this story, but I don't think $USER defined for cron in any Raspbian distro

            – Seamus
            11 hours ago











          • @MarkSmith: It just dawned on me the line you referenced: env report for user concluded. Yeah... :) I stuck that in the script to emphasize that $USER isn't defined :P

            – Seamus
            11 hours ago











          Your Answer






          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("schematics", function () {
          StackExchange.schematics.init();
          });
          }, "cicuitlab");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "447"
          };
          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%2fraspberrypi.stackexchange.com%2fquestions%2f95799%2fwhat-is-the-environment-for-cron%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          We can ask cron to tell us what its environment is.




          • Create a shell script in your home directory (~/) as follows (or with the editor of your choice):


          $ nano ~/envtst.sh



          • Enter/C+P the following in the editor:


          #!/bin/sh 
          echo "env report follows for user "$USER >> /home/pi/envtst.sh.out
          env >> /home/pi/envtst.sh.out
          echo "env report for user "$USER" concluded" >> /home/pi/envtst.sh.out
          echo " " >> /home/pi/envtst.sh.out



          • Save the file and exit the editor; then set the file permissions as executable, and open your crontab for editing:


          $ chmod a+rx ~/envtst.sh
          $ crontab -e



          • Enter the following line at the bottom of your crontab:


          * * * * *  /home/pi/envtst.sh >> /home/pi/envtst.sh.err 2>&1



          • Save and exit your crontab. Use tail to view the output & (hopefully) observe the environment for cron. If there's nothing in the file after a minute, view the file ~/envtst.sh.err for error messages, and adjust as required. (NOTE: If you want to clear all prior error messages after troubleshooting: $ > ~/envtst.sh.err )


          crontab: installing new crontab
          $ tail -f ~/envtst.sh.out
          env report follows for user
          HOME=/home/pi
          LOGNAME=pi
          PATH=/usr/bin:/bin
          LANG=en_GB.UTF-8
          SHELL=/bin/sh
          PWD=/home/pi
          env report for user concluded



          This will repeat every minute, so enter ^C to stop the tail listing, edit your crontab again to "comment out" (or delete) the line just added. Save and exit the editor.




          • Note in the tail output above that cron has a rather sparse environment; only six (6) variables are used to define it. Note the PATH consists of only two directories. This is why your crontab entry fails if, for example, you're trying to launch a Python script that resides in your home directory. Note also that the user name (aka LOGNAME iaw System V) isn't cron - it's pi!


          • If you're not familiar, with your own user environment, it's useful to compare it against the cron environment. We'll use the same shell script to add that to the "output" file ~/envtst.sh.out:



          $ ~/envtst.sh 
          $



          • To view the output, open ~/envtst.sh.outin your editor, or cat ~/envtst.sh.out to see it in your terminal. It will likely be a fairly extensive output; 30 lines of text, more or less. Note in particular the following lines (assuming you've run this as user pi) :


          USER=pi
          ...
          HOME=/home/pi
          LOGNAME=pi
          _=/home/pi/envtst.sh
          ...
          PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
          ...
          SHELL=/bin/bash



          • You'll notice numerous differences in the two environments. This will help create rational cron jobs, and help troubleshooting when they don't behave as you'd like.






          share|improve this answer


























          • Very nice! For some reason your $USER variable isn't set -- see env report for user concluded.

            – Mark Smith
            18 hours ago











          • @MarkSmith: If you're referring to the absence of $USER in the cron environment, it's not that it's not set... it's just not used in the version of cron on Raspbian (and Debian I think). Here's some more on that, and still more, and more. Likely more to this story, but I don't think $USER defined for cron in any Raspbian distro

            – Seamus
            11 hours ago











          • @MarkSmith: It just dawned on me the line you referenced: env report for user concluded. Yeah... :) I stuck that in the script to emphasize that $USER isn't defined :P

            – Seamus
            11 hours ago
















          4














          We can ask cron to tell us what its environment is.




          • Create a shell script in your home directory (~/) as follows (or with the editor of your choice):


          $ nano ~/envtst.sh



          • Enter/C+P the following in the editor:


          #!/bin/sh 
          echo "env report follows for user "$USER >> /home/pi/envtst.sh.out
          env >> /home/pi/envtst.sh.out
          echo "env report for user "$USER" concluded" >> /home/pi/envtst.sh.out
          echo " " >> /home/pi/envtst.sh.out



          • Save the file and exit the editor; then set the file permissions as executable, and open your crontab for editing:


          $ chmod a+rx ~/envtst.sh
          $ crontab -e



          • Enter the following line at the bottom of your crontab:


          * * * * *  /home/pi/envtst.sh >> /home/pi/envtst.sh.err 2>&1



          • Save and exit your crontab. Use tail to view the output & (hopefully) observe the environment for cron. If there's nothing in the file after a minute, view the file ~/envtst.sh.err for error messages, and adjust as required. (NOTE: If you want to clear all prior error messages after troubleshooting: $ > ~/envtst.sh.err )


          crontab: installing new crontab
          $ tail -f ~/envtst.sh.out
          env report follows for user
          HOME=/home/pi
          LOGNAME=pi
          PATH=/usr/bin:/bin
          LANG=en_GB.UTF-8
          SHELL=/bin/sh
          PWD=/home/pi
          env report for user concluded



          This will repeat every minute, so enter ^C to stop the tail listing, edit your crontab again to "comment out" (or delete) the line just added. Save and exit the editor.




          • Note in the tail output above that cron has a rather sparse environment; only six (6) variables are used to define it. Note the PATH consists of only two directories. This is why your crontab entry fails if, for example, you're trying to launch a Python script that resides in your home directory. Note also that the user name (aka LOGNAME iaw System V) isn't cron - it's pi!


          • If you're not familiar, with your own user environment, it's useful to compare it against the cron environment. We'll use the same shell script to add that to the "output" file ~/envtst.sh.out:



          $ ~/envtst.sh 
          $



          • To view the output, open ~/envtst.sh.outin your editor, or cat ~/envtst.sh.out to see it in your terminal. It will likely be a fairly extensive output; 30 lines of text, more or less. Note in particular the following lines (assuming you've run this as user pi) :


          USER=pi
          ...
          HOME=/home/pi
          LOGNAME=pi
          _=/home/pi/envtst.sh
          ...
          PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
          ...
          SHELL=/bin/bash



          • You'll notice numerous differences in the two environments. This will help create rational cron jobs, and help troubleshooting when they don't behave as you'd like.






          share|improve this answer


























          • Very nice! For some reason your $USER variable isn't set -- see env report for user concluded.

            – Mark Smith
            18 hours ago











          • @MarkSmith: If you're referring to the absence of $USER in the cron environment, it's not that it's not set... it's just not used in the version of cron on Raspbian (and Debian I think). Here's some more on that, and still more, and more. Likely more to this story, but I don't think $USER defined for cron in any Raspbian distro

            – Seamus
            11 hours ago











          • @MarkSmith: It just dawned on me the line you referenced: env report for user concluded. Yeah... :) I stuck that in the script to emphasize that $USER isn't defined :P

            – Seamus
            11 hours ago














          4












          4








          4







          We can ask cron to tell us what its environment is.




          • Create a shell script in your home directory (~/) as follows (or with the editor of your choice):


          $ nano ~/envtst.sh



          • Enter/C+P the following in the editor:


          #!/bin/sh 
          echo "env report follows for user "$USER >> /home/pi/envtst.sh.out
          env >> /home/pi/envtst.sh.out
          echo "env report for user "$USER" concluded" >> /home/pi/envtst.sh.out
          echo " " >> /home/pi/envtst.sh.out



          • Save the file and exit the editor; then set the file permissions as executable, and open your crontab for editing:


          $ chmod a+rx ~/envtst.sh
          $ crontab -e



          • Enter the following line at the bottom of your crontab:


          * * * * *  /home/pi/envtst.sh >> /home/pi/envtst.sh.err 2>&1



          • Save and exit your crontab. Use tail to view the output & (hopefully) observe the environment for cron. If there's nothing in the file after a minute, view the file ~/envtst.sh.err for error messages, and adjust as required. (NOTE: If you want to clear all prior error messages after troubleshooting: $ > ~/envtst.sh.err )


          crontab: installing new crontab
          $ tail -f ~/envtst.sh.out
          env report follows for user
          HOME=/home/pi
          LOGNAME=pi
          PATH=/usr/bin:/bin
          LANG=en_GB.UTF-8
          SHELL=/bin/sh
          PWD=/home/pi
          env report for user concluded



          This will repeat every minute, so enter ^C to stop the tail listing, edit your crontab again to "comment out" (or delete) the line just added. Save and exit the editor.




          • Note in the tail output above that cron has a rather sparse environment; only six (6) variables are used to define it. Note the PATH consists of only two directories. This is why your crontab entry fails if, for example, you're trying to launch a Python script that resides in your home directory. Note also that the user name (aka LOGNAME iaw System V) isn't cron - it's pi!


          • If you're not familiar, with your own user environment, it's useful to compare it against the cron environment. We'll use the same shell script to add that to the "output" file ~/envtst.sh.out:



          $ ~/envtst.sh 
          $



          • To view the output, open ~/envtst.sh.outin your editor, or cat ~/envtst.sh.out to see it in your terminal. It will likely be a fairly extensive output; 30 lines of text, more or less. Note in particular the following lines (assuming you've run this as user pi) :


          USER=pi
          ...
          HOME=/home/pi
          LOGNAME=pi
          _=/home/pi/envtst.sh
          ...
          PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
          ...
          SHELL=/bin/bash



          • You'll notice numerous differences in the two environments. This will help create rational cron jobs, and help troubleshooting when they don't behave as you'd like.






          share|improve this answer















          We can ask cron to tell us what its environment is.




          • Create a shell script in your home directory (~/) as follows (or with the editor of your choice):


          $ nano ~/envtst.sh



          • Enter/C+P the following in the editor:


          #!/bin/sh 
          echo "env report follows for user "$USER >> /home/pi/envtst.sh.out
          env >> /home/pi/envtst.sh.out
          echo "env report for user "$USER" concluded" >> /home/pi/envtst.sh.out
          echo " " >> /home/pi/envtst.sh.out



          • Save the file and exit the editor; then set the file permissions as executable, and open your crontab for editing:


          $ chmod a+rx ~/envtst.sh
          $ crontab -e



          • Enter the following line at the bottom of your crontab:


          * * * * *  /home/pi/envtst.sh >> /home/pi/envtst.sh.err 2>&1



          • Save and exit your crontab. Use tail to view the output & (hopefully) observe the environment for cron. If there's nothing in the file after a minute, view the file ~/envtst.sh.err for error messages, and adjust as required. (NOTE: If you want to clear all prior error messages after troubleshooting: $ > ~/envtst.sh.err )


          crontab: installing new crontab
          $ tail -f ~/envtst.sh.out
          env report follows for user
          HOME=/home/pi
          LOGNAME=pi
          PATH=/usr/bin:/bin
          LANG=en_GB.UTF-8
          SHELL=/bin/sh
          PWD=/home/pi
          env report for user concluded



          This will repeat every minute, so enter ^C to stop the tail listing, edit your crontab again to "comment out" (or delete) the line just added. Save and exit the editor.




          • Note in the tail output above that cron has a rather sparse environment; only six (6) variables are used to define it. Note the PATH consists of only two directories. This is why your crontab entry fails if, for example, you're trying to launch a Python script that resides in your home directory. Note also that the user name (aka LOGNAME iaw System V) isn't cron - it's pi!


          • If you're not familiar, with your own user environment, it's useful to compare it against the cron environment. We'll use the same shell script to add that to the "output" file ~/envtst.sh.out:



          $ ~/envtst.sh 
          $



          • To view the output, open ~/envtst.sh.outin your editor, or cat ~/envtst.sh.out to see it in your terminal. It will likely be a fairly extensive output; 30 lines of text, more or less. Note in particular the following lines (assuming you've run this as user pi) :


          USER=pi
          ...
          HOME=/home/pi
          LOGNAME=pi
          _=/home/pi/envtst.sh
          ...
          PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
          ...
          SHELL=/bin/bash



          • You'll notice numerous differences in the two environments. This will help create rational cron jobs, and help troubleshooting when they don't behave as you'd like.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 20 hours ago

























          answered 20 hours ago









          SeamusSeamus

          2,8501321




          2,8501321













          • Very nice! For some reason your $USER variable isn't set -- see env report for user concluded.

            – Mark Smith
            18 hours ago











          • @MarkSmith: If you're referring to the absence of $USER in the cron environment, it's not that it's not set... it's just not used in the version of cron on Raspbian (and Debian I think). Here's some more on that, and still more, and more. Likely more to this story, but I don't think $USER defined for cron in any Raspbian distro

            – Seamus
            11 hours ago











          • @MarkSmith: It just dawned on me the line you referenced: env report for user concluded. Yeah... :) I stuck that in the script to emphasize that $USER isn't defined :P

            – Seamus
            11 hours ago



















          • Very nice! For some reason your $USER variable isn't set -- see env report for user concluded.

            – Mark Smith
            18 hours ago











          • @MarkSmith: If you're referring to the absence of $USER in the cron environment, it's not that it's not set... it's just not used in the version of cron on Raspbian (and Debian I think). Here's some more on that, and still more, and more. Likely more to this story, but I don't think $USER defined for cron in any Raspbian distro

            – Seamus
            11 hours ago











          • @MarkSmith: It just dawned on me the line you referenced: env report for user concluded. Yeah... :) I stuck that in the script to emphasize that $USER isn't defined :P

            – Seamus
            11 hours ago

















          Very nice! For some reason your $USER variable isn't set -- see env report for user concluded.

          – Mark Smith
          18 hours ago





          Very nice! For some reason your $USER variable isn't set -- see env report for user concluded.

          – Mark Smith
          18 hours ago













          @MarkSmith: If you're referring to the absence of $USER in the cron environment, it's not that it's not set... it's just not used in the version of cron on Raspbian (and Debian I think). Here's some more on that, and still more, and more. Likely more to this story, but I don't think $USER defined for cron in any Raspbian distro

          – Seamus
          11 hours ago





          @MarkSmith: If you're referring to the absence of $USER in the cron environment, it's not that it's not set... it's just not used in the version of cron on Raspbian (and Debian I think). Here's some more on that, and still more, and more. Likely more to this story, but I don't think $USER defined for cron in any Raspbian distro

          – Seamus
          11 hours ago













          @MarkSmith: It just dawned on me the line you referenced: env report for user concluded. Yeah... :) I stuck that in the script to emphasize that $USER isn't defined :P

          – Seamus
          11 hours ago





          @MarkSmith: It just dawned on me the line you referenced: env report for user concluded. Yeah... :) I stuck that in the script to emphasize that $USER isn't defined :P

          – Seamus
          11 hours ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Raspberry Pi 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%2fraspberrypi.stackexchange.com%2fquestions%2f95799%2fwhat-is-the-environment-for-cron%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

          Bruad Bilen | Luke uk diar | NawigatsjuunCommonskategorii: BruadCommonskategorii: RunstükenWikiquote: Bruad

          Modern approach to radio buttons

          A word for worn pool table felt