Run python code from cron in an Anaconda environment












1















I need to run a python script using a specific anaconda environment, since I need to execute this script several times per day I would like to do it using the linux crontab file (/etc/crontab).



When I launch the script from the linux command line it works fine:



~/anaconda2/envs/py36/bin/python ~/production/web_scraping.py


I added the process to the crontab file:



SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
57 08 * * * user ~/anaconda2/envs/py36/bin/python ~/production/web_scraping.py | touch ~/test.txt


The line is correctly executed by cron, this is the cron log:



user@exmachina:~$ sudo cat /var/log/syslog | grep web_*
Jan 2 08:57:02 localhost CRON[10854]: (user) CMD (~/anaconda2/envs/py36/bin/python ~/web_scraping.py | touch ~/test.txt)


I added | touch test.txt to the cron line to ensure the line is executed, and the test.txt file is generated.



The python script uses Selenium to extract data from a web page, by means of the chrome webdriver. Can this be the reason the script runs fine from the command line but not from the cron file?



Thanks for your help!










share|improve this question





























    1















    I need to run a python script using a specific anaconda environment, since I need to execute this script several times per day I would like to do it using the linux crontab file (/etc/crontab).



    When I launch the script from the linux command line it works fine:



    ~/anaconda2/envs/py36/bin/python ~/production/web_scraping.py


    I added the process to the crontab file:



    SHELL=/bin/sh
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    # m h dom mon dow user command
    57 08 * * * user ~/anaconda2/envs/py36/bin/python ~/production/web_scraping.py | touch ~/test.txt


    The line is correctly executed by cron, this is the cron log:



    user@exmachina:~$ sudo cat /var/log/syslog | grep web_*
    Jan 2 08:57:02 localhost CRON[10854]: (user) CMD (~/anaconda2/envs/py36/bin/python ~/web_scraping.py | touch ~/test.txt)


    I added | touch test.txt to the cron line to ensure the line is executed, and the test.txt file is generated.



    The python script uses Selenium to extract data from a web page, by means of the chrome webdriver. Can this be the reason the script runs fine from the command line but not from the cron file?



    Thanks for your help!










    share|improve this question



























      1












      1








      1








      I need to run a python script using a specific anaconda environment, since I need to execute this script several times per day I would like to do it using the linux crontab file (/etc/crontab).



      When I launch the script from the linux command line it works fine:



      ~/anaconda2/envs/py36/bin/python ~/production/web_scraping.py


      I added the process to the crontab file:



      SHELL=/bin/sh
      PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
      # m h dom mon dow user command
      57 08 * * * user ~/anaconda2/envs/py36/bin/python ~/production/web_scraping.py | touch ~/test.txt


      The line is correctly executed by cron, this is the cron log:



      user@exmachina:~$ sudo cat /var/log/syslog | grep web_*
      Jan 2 08:57:02 localhost CRON[10854]: (user) CMD (~/anaconda2/envs/py36/bin/python ~/web_scraping.py | touch ~/test.txt)


      I added | touch test.txt to the cron line to ensure the line is executed, and the test.txt file is generated.



      The python script uses Selenium to extract data from a web page, by means of the chrome webdriver. Can this be the reason the script runs fine from the command line but not from the cron file?



      Thanks for your help!










      share|improve this question
















      I need to run a python script using a specific anaconda environment, since I need to execute this script several times per day I would like to do it using the linux crontab file (/etc/crontab).



      When I launch the script from the linux command line it works fine:



      ~/anaconda2/envs/py36/bin/python ~/production/web_scraping.py


      I added the process to the crontab file:



      SHELL=/bin/sh
      PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
      # m h dom mon dow user command
      57 08 * * * user ~/anaconda2/envs/py36/bin/python ~/production/web_scraping.py | touch ~/test.txt


      The line is correctly executed by cron, this is the cron log:



      user@exmachina:~$ sudo cat /var/log/syslog | grep web_*
      Jan 2 08:57:02 localhost CRON[10854]: (user) CMD (~/anaconda2/envs/py36/bin/python ~/web_scraping.py | touch ~/test.txt)


      I added | touch test.txt to the cron line to ensure the line is executed, and the test.txt file is generated.



      The python script uses Selenium to extract data from a web page, by means of the chrome webdriver. Can this be the reason the script runs fine from the command line but not from the cron file?



      Thanks for your help!







      cron python anaconda






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 '18 at 6:35







      jordi vidal

















      asked Jan 2 '18 at 10:42









      jordi vidaljordi vidal

      64




      64






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Instead of | touch ~/test.txt it would be more informative to use | tee ~/test.txt (or tee -a).



          Consider incorporating env|sort; id; date in the output you log.



          You were quite vague in describing the selenium script, and the way in which it "does not run fine" from cron. Did all imports succeed, or is PYTHONPATH perhaps not correct? Would it help to run conda env update -f environment.yml and then have the cron job source activate xxx so the job pulls in appropriate library versions?



          A few variables, including PATH and LD_LIBRARY_PATH, can be important for your browser, and when you view the logged values you may see they differ from the interactive environment where selenium is working fine. Part of setting up a cron job is copying aspects of the environment so the job will succeed.






          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%2f414283%2frun-python-code-from-cron-in-an-anaconda-environment%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









            0














            Instead of | touch ~/test.txt it would be more informative to use | tee ~/test.txt (or tee -a).



            Consider incorporating env|sort; id; date in the output you log.



            You were quite vague in describing the selenium script, and the way in which it "does not run fine" from cron. Did all imports succeed, or is PYTHONPATH perhaps not correct? Would it help to run conda env update -f environment.yml and then have the cron job source activate xxx so the job pulls in appropriate library versions?



            A few variables, including PATH and LD_LIBRARY_PATH, can be important for your browser, and when you view the logged values you may see they differ from the interactive environment where selenium is working fine. Part of setting up a cron job is copying aspects of the environment so the job will succeed.






            share|improve this answer




























              0














              Instead of | touch ~/test.txt it would be more informative to use | tee ~/test.txt (or tee -a).



              Consider incorporating env|sort; id; date in the output you log.



              You were quite vague in describing the selenium script, and the way in which it "does not run fine" from cron. Did all imports succeed, or is PYTHONPATH perhaps not correct? Would it help to run conda env update -f environment.yml and then have the cron job source activate xxx so the job pulls in appropriate library versions?



              A few variables, including PATH and LD_LIBRARY_PATH, can be important for your browser, and when you view the logged values you may see they differ from the interactive environment where selenium is working fine. Part of setting up a cron job is copying aspects of the environment so the job will succeed.






              share|improve this answer


























                0












                0








                0







                Instead of | touch ~/test.txt it would be more informative to use | tee ~/test.txt (or tee -a).



                Consider incorporating env|sort; id; date in the output you log.



                You were quite vague in describing the selenium script, and the way in which it "does not run fine" from cron. Did all imports succeed, or is PYTHONPATH perhaps not correct? Would it help to run conda env update -f environment.yml and then have the cron job source activate xxx so the job pulls in appropriate library versions?



                A few variables, including PATH and LD_LIBRARY_PATH, can be important for your browser, and when you view the logged values you may see they differ from the interactive environment where selenium is working fine. Part of setting up a cron job is copying aspects of the environment so the job will succeed.






                share|improve this answer













                Instead of | touch ~/test.txt it would be more informative to use | tee ~/test.txt (or tee -a).



                Consider incorporating env|sort; id; date in the output you log.



                You were quite vague in describing the selenium script, and the way in which it "does not run fine" from cron. Did all imports succeed, or is PYTHONPATH perhaps not correct? Would it help to run conda env update -f environment.yml and then have the cron job source activate xxx so the job pulls in appropriate library versions?



                A few variables, including PATH and LD_LIBRARY_PATH, can be important for your browser, and when you view the logged values you may see they differ from the interactive environment where selenium is working fine. Part of setting up a cron job is copying aspects of the environment so the job will succeed.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 6 '18 at 23:41









                J_HJ_H

                26113




                26113






























                    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%2f414283%2frun-python-code-from-cron-in-an-anaconda-environment%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

                    How to make a Squid Proxy server?

                    Is this a new Fibonacci Identity?

                    Touch on Surface Book