cron not executing python3












0















I am running Raspbian Stretch and have a python 3 script that executes correctly when I am in the relevant directory and input it in the command line as:



sudo python3 Example.py


The shebang #! line is in the script as follows:



 #!/usr/bin/env python3


And made the script executable with:



sudo chmod +x Example.py


I've tried several variation of below within crontab -e to no avail



10 * * * * python3 /home/pi/Desktop/Example.py










share|improve this question



























    0















    I am running Raspbian Stretch and have a python 3 script that executes correctly when I am in the relevant directory and input it in the command line as:



    sudo python3 Example.py


    The shebang #! line is in the script as follows:



     #!/usr/bin/env python3


    And made the script executable with:



    sudo chmod +x Example.py


    I've tried several variation of below within crontab -e to no avail



    10 * * * * python3 /home/pi/Desktop/Example.py










    share|improve this question

























      0












      0








      0








      I am running Raspbian Stretch and have a python 3 script that executes correctly when I am in the relevant directory and input it in the command line as:



      sudo python3 Example.py


      The shebang #! line is in the script as follows:



       #!/usr/bin/env python3


      And made the script executable with:



      sudo chmod +x Example.py


      I've tried several variation of below within crontab -e to no avail



      10 * * * * python3 /home/pi/Desktop/Example.py










      share|improve this question














      I am running Raspbian Stretch and have a python 3 script that executes correctly when I am in the relevant directory and input it in the command line as:



      sudo python3 Example.py


      The shebang #! line is in the script as follows:



       #!/usr/bin/env python3


      And made the script executable with:



      sudo chmod +x Example.py


      I've tried several variation of below within crontab -e to no avail



      10 * * * * python3 /home/pi/Desktop/Example.py







      raspbian-stretch python-3 cron






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 1 at 17:17









      Dan B RaelinDan B Raelin

      257




      257






















          2 Answers
          2






          active

          oldest

          votes


















          5














          As is typical with crontab issues. The environment is not the same as your user or even a sudo/root shell , and env python3 may not return anything (env simply searches through



          Instead you should explicitly use the full path of python which can be found either as which python3 or which $(env python3) and use that full path in your crontab (typically /usr/bin/python3 but it is also valid to use one installed elsewhere , e.g. a virtualenv directory)



          If your script or program relies on being in the same directory, make sure to change directory



          cd /path/to/dir && /usr/bin/python3 ./Example.py






          share|improve this answer


























          • Note shebangs are ignored when called script is called explicitly as argument to python

            – crasic
            Mar 1 at 17:30











          • The script should also cd <relevant directory>.

            – Ingo
            Mar 1 at 19:46











          • @Ingo this is not strictly necessary unless Example.py uses relative paths to CWD in it's logic, using the full path of the script is also acceptable in many instances. I will edit for completeness

            – crasic
            Mar 1 at 20:34













          • Maybe, but I have seen many python scripts that cannot find its custom modules if not executed from its current directory.

            – Ingo
            Mar 1 at 21:28






          • 1





            @DanBRaelin In the general, no downside in doing so. in some cases if you have complex environment needs, a helper/caller shell script may be useful, be sure to call such a script with the right shell as crontab by default uses sh, i.e. bash -c './myhelper'

            – crasic
            Mar 2 at 6:15





















          1














          Try the following in crontab:



          sudo python3 /home/pi/Example.py



          You need the full path to Example.py



          You don't really need the shebang.



          Or sudo crontab -e for the root crontab with:



          python3 /home/pi/Example.py






          share|improve this answer



















          • 1





            The script should also cd <relevant directory>.

            – Ingo
            Mar 1 at 19:47













          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%2f94818%2fcron-not-executing-python3%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5














          As is typical with crontab issues. The environment is not the same as your user or even a sudo/root shell , and env python3 may not return anything (env simply searches through



          Instead you should explicitly use the full path of python which can be found either as which python3 or which $(env python3) and use that full path in your crontab (typically /usr/bin/python3 but it is also valid to use one installed elsewhere , e.g. a virtualenv directory)



          If your script or program relies on being in the same directory, make sure to change directory



          cd /path/to/dir && /usr/bin/python3 ./Example.py






          share|improve this answer


























          • Note shebangs are ignored when called script is called explicitly as argument to python

            – crasic
            Mar 1 at 17:30











          • The script should also cd <relevant directory>.

            – Ingo
            Mar 1 at 19:46











          • @Ingo this is not strictly necessary unless Example.py uses relative paths to CWD in it's logic, using the full path of the script is also acceptable in many instances. I will edit for completeness

            – crasic
            Mar 1 at 20:34













          • Maybe, but I have seen many python scripts that cannot find its custom modules if not executed from its current directory.

            – Ingo
            Mar 1 at 21:28






          • 1





            @DanBRaelin In the general, no downside in doing so. in some cases if you have complex environment needs, a helper/caller shell script may be useful, be sure to call such a script with the right shell as crontab by default uses sh, i.e. bash -c './myhelper'

            – crasic
            Mar 2 at 6:15


















          5














          As is typical with crontab issues. The environment is not the same as your user or even a sudo/root shell , and env python3 may not return anything (env simply searches through



          Instead you should explicitly use the full path of python which can be found either as which python3 or which $(env python3) and use that full path in your crontab (typically /usr/bin/python3 but it is also valid to use one installed elsewhere , e.g. a virtualenv directory)



          If your script or program relies on being in the same directory, make sure to change directory



          cd /path/to/dir && /usr/bin/python3 ./Example.py






          share|improve this answer


























          • Note shebangs are ignored when called script is called explicitly as argument to python

            – crasic
            Mar 1 at 17:30











          • The script should also cd <relevant directory>.

            – Ingo
            Mar 1 at 19:46











          • @Ingo this is not strictly necessary unless Example.py uses relative paths to CWD in it's logic, using the full path of the script is also acceptable in many instances. I will edit for completeness

            – crasic
            Mar 1 at 20:34













          • Maybe, but I have seen many python scripts that cannot find its custom modules if not executed from its current directory.

            – Ingo
            Mar 1 at 21:28






          • 1





            @DanBRaelin In the general, no downside in doing so. in some cases if you have complex environment needs, a helper/caller shell script may be useful, be sure to call such a script with the right shell as crontab by default uses sh, i.e. bash -c './myhelper'

            – crasic
            Mar 2 at 6:15
















          5












          5








          5







          As is typical with crontab issues. The environment is not the same as your user or even a sudo/root shell , and env python3 may not return anything (env simply searches through



          Instead you should explicitly use the full path of python which can be found either as which python3 or which $(env python3) and use that full path in your crontab (typically /usr/bin/python3 but it is also valid to use one installed elsewhere , e.g. a virtualenv directory)



          If your script or program relies on being in the same directory, make sure to change directory



          cd /path/to/dir && /usr/bin/python3 ./Example.py






          share|improve this answer















          As is typical with crontab issues. The environment is not the same as your user or even a sudo/root shell , and env python3 may not return anything (env simply searches through



          Instead you should explicitly use the full path of python which can be found either as which python3 or which $(env python3) and use that full path in your crontab (typically /usr/bin/python3 but it is also valid to use one installed elsewhere , e.g. a virtualenv directory)



          If your script or program relies on being in the same directory, make sure to change directory



          cd /path/to/dir && /usr/bin/python3 ./Example.py







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 2 at 4:32









          Seamus

          2,5591220




          2,5591220










          answered Mar 1 at 17:23









          crasiccrasic

          2,341318




          2,341318













          • Note shebangs are ignored when called script is called explicitly as argument to python

            – crasic
            Mar 1 at 17:30











          • The script should also cd <relevant directory>.

            – Ingo
            Mar 1 at 19:46











          • @Ingo this is not strictly necessary unless Example.py uses relative paths to CWD in it's logic, using the full path of the script is also acceptable in many instances. I will edit for completeness

            – crasic
            Mar 1 at 20:34













          • Maybe, but I have seen many python scripts that cannot find its custom modules if not executed from its current directory.

            – Ingo
            Mar 1 at 21:28






          • 1





            @DanBRaelin In the general, no downside in doing so. in some cases if you have complex environment needs, a helper/caller shell script may be useful, be sure to call such a script with the right shell as crontab by default uses sh, i.e. bash -c './myhelper'

            – crasic
            Mar 2 at 6:15





















          • Note shebangs are ignored when called script is called explicitly as argument to python

            – crasic
            Mar 1 at 17:30











          • The script should also cd <relevant directory>.

            – Ingo
            Mar 1 at 19:46











          • @Ingo this is not strictly necessary unless Example.py uses relative paths to CWD in it's logic, using the full path of the script is also acceptable in many instances. I will edit for completeness

            – crasic
            Mar 1 at 20:34













          • Maybe, but I have seen many python scripts that cannot find its custom modules if not executed from its current directory.

            – Ingo
            Mar 1 at 21:28






          • 1





            @DanBRaelin In the general, no downside in doing so. in some cases if you have complex environment needs, a helper/caller shell script may be useful, be sure to call such a script with the right shell as crontab by default uses sh, i.e. bash -c './myhelper'

            – crasic
            Mar 2 at 6:15



















          Note shebangs are ignored when called script is called explicitly as argument to python

          – crasic
          Mar 1 at 17:30





          Note shebangs are ignored when called script is called explicitly as argument to python

          – crasic
          Mar 1 at 17:30













          The script should also cd <relevant directory>.

          – Ingo
          Mar 1 at 19:46





          The script should also cd <relevant directory>.

          – Ingo
          Mar 1 at 19:46













          @Ingo this is not strictly necessary unless Example.py uses relative paths to CWD in it's logic, using the full path of the script is also acceptable in many instances. I will edit for completeness

          – crasic
          Mar 1 at 20:34







          @Ingo this is not strictly necessary unless Example.py uses relative paths to CWD in it's logic, using the full path of the script is also acceptable in many instances. I will edit for completeness

          – crasic
          Mar 1 at 20:34















          Maybe, but I have seen many python scripts that cannot find its custom modules if not executed from its current directory.

          – Ingo
          Mar 1 at 21:28





          Maybe, but I have seen many python scripts that cannot find its custom modules if not executed from its current directory.

          – Ingo
          Mar 1 at 21:28




          1




          1





          @DanBRaelin In the general, no downside in doing so. in some cases if you have complex environment needs, a helper/caller shell script may be useful, be sure to call such a script with the right shell as crontab by default uses sh, i.e. bash -c './myhelper'

          – crasic
          Mar 2 at 6:15







          @DanBRaelin In the general, no downside in doing so. in some cases if you have complex environment needs, a helper/caller shell script may be useful, be sure to call such a script with the right shell as crontab by default uses sh, i.e. bash -c './myhelper'

          – crasic
          Mar 2 at 6:15















          1














          Try the following in crontab:



          sudo python3 /home/pi/Example.py



          You need the full path to Example.py



          You don't really need the shebang.



          Or sudo crontab -e for the root crontab with:



          python3 /home/pi/Example.py






          share|improve this answer



















          • 1





            The script should also cd <relevant directory>.

            – Ingo
            Mar 1 at 19:47


















          1














          Try the following in crontab:



          sudo python3 /home/pi/Example.py



          You need the full path to Example.py



          You don't really need the shebang.



          Or sudo crontab -e for the root crontab with:



          python3 /home/pi/Example.py






          share|improve this answer



















          • 1





            The script should also cd <relevant directory>.

            – Ingo
            Mar 1 at 19:47
















          1












          1








          1







          Try the following in crontab:



          sudo python3 /home/pi/Example.py



          You need the full path to Example.py



          You don't really need the shebang.



          Or sudo crontab -e for the root crontab with:



          python3 /home/pi/Example.py






          share|improve this answer













          Try the following in crontab:



          sudo python3 /home/pi/Example.py



          You need the full path to Example.py



          You don't really need the shebang.



          Or sudo crontab -e for the root crontab with:



          python3 /home/pi/Example.py







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 1 at 19:15









          Steve AmorSteve Amor

          111




          111








          • 1





            The script should also cd <relevant directory>.

            – Ingo
            Mar 1 at 19:47
















          • 1





            The script should also cd <relevant directory>.

            – Ingo
            Mar 1 at 19:47










          1




          1





          The script should also cd <relevant directory>.

          – Ingo
          Mar 1 at 19:47







          The script should also cd <relevant directory>.

          – Ingo
          Mar 1 at 19:47




















          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%2f94818%2fcron-not-executing-python3%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

          Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

          ComboBox Display Member on multiple fields

          Is it possible to collect Nectar points via Trainline?