cron job to run under conda virtual environment

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I have a Anaconda Python Virtual Environment set up and if I run my project while that virutal environment is activated everything runs great.



But I have a cronjob configured to run it every hour. I piped the output to a log because it wasn't running correctly.



crontab -e:



10 * * * * bash /work/sql_server_etl/src/python/run_parallel_workflow.sh >> /home/etlservice/cronlog.log 2>&1


I get this error in the cronlog.log:



Traceback (most recent call last):
File "__parallel_workflow.py", line 10, in <module>
import yaml
ImportError: No module named yaml


That is indicative of the cronjob somehow not running the file without the virtual environment activated.



To remedy this I added a line to the /home/user/.bash_profile file:



conda activate ~/anaconda3/envs/sql_server_etl/


Now when I login the environment is activated automatically.



However, the problem persists.



I tried one more thing. I changed the cronjob, (and I also tried this in the bash file the cronjob runs) to explicitly manually activate the environment each time it runs, but to no avail:



10 * * * * conda activate ~/anaconda3/envs/sql_server_etl/ && bash /work/sql_server_etl/src/python/run_parallel_workflow.sh >> /home/etlservice/cronlog.log 2>&1


Of course, nothing I've tried has fixed it. I really know nothing about linux so maybe there's something obvious I need to change.



So, is there anyway to specify that the cronjob should run under a virutal environment?







share|improve this question























    up vote
    0
    down vote

    favorite












    I have a Anaconda Python Virtual Environment set up and if I run my project while that virutal environment is activated everything runs great.



    But I have a cronjob configured to run it every hour. I piped the output to a log because it wasn't running correctly.



    crontab -e:



    10 * * * * bash /work/sql_server_etl/src/python/run_parallel_workflow.sh >> /home/etlservice/cronlog.log 2>&1


    I get this error in the cronlog.log:



    Traceback (most recent call last):
    File "__parallel_workflow.py", line 10, in <module>
    import yaml
    ImportError: No module named yaml


    That is indicative of the cronjob somehow not running the file without the virtual environment activated.



    To remedy this I added a line to the /home/user/.bash_profile file:



    conda activate ~/anaconda3/envs/sql_server_etl/


    Now when I login the environment is activated automatically.



    However, the problem persists.



    I tried one more thing. I changed the cronjob, (and I also tried this in the bash file the cronjob runs) to explicitly manually activate the environment each time it runs, but to no avail:



    10 * * * * conda activate ~/anaconda3/envs/sql_server_etl/ && bash /work/sql_server_etl/src/python/run_parallel_workflow.sh >> /home/etlservice/cronlog.log 2>&1


    Of course, nothing I've tried has fixed it. I really know nothing about linux so maybe there's something obvious I need to change.



    So, is there anyway to specify that the cronjob should run under a virutal environment?







    share|improve this question





















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a Anaconda Python Virtual Environment set up and if I run my project while that virutal environment is activated everything runs great.



      But I have a cronjob configured to run it every hour. I piped the output to a log because it wasn't running correctly.



      crontab -e:



      10 * * * * bash /work/sql_server_etl/src/python/run_parallel_workflow.sh >> /home/etlservice/cronlog.log 2>&1


      I get this error in the cronlog.log:



      Traceback (most recent call last):
      File "__parallel_workflow.py", line 10, in <module>
      import yaml
      ImportError: No module named yaml


      That is indicative of the cronjob somehow not running the file without the virtual environment activated.



      To remedy this I added a line to the /home/user/.bash_profile file:



      conda activate ~/anaconda3/envs/sql_server_etl/


      Now when I login the environment is activated automatically.



      However, the problem persists.



      I tried one more thing. I changed the cronjob, (and I also tried this in the bash file the cronjob runs) to explicitly manually activate the environment each time it runs, but to no avail:



      10 * * * * conda activate ~/anaconda3/envs/sql_server_etl/ && bash /work/sql_server_etl/src/python/run_parallel_workflow.sh >> /home/etlservice/cronlog.log 2>&1


      Of course, nothing I've tried has fixed it. I really know nothing about linux so maybe there's something obvious I need to change.



      So, is there anyway to specify that the cronjob should run under a virutal environment?







      share|improve this question











      I have a Anaconda Python Virtual Environment set up and if I run my project while that virutal environment is activated everything runs great.



      But I have a cronjob configured to run it every hour. I piped the output to a log because it wasn't running correctly.



      crontab -e:



      10 * * * * bash /work/sql_server_etl/src/python/run_parallel_workflow.sh >> /home/etlservice/cronlog.log 2>&1


      I get this error in the cronlog.log:



      Traceback (most recent call last):
      File "__parallel_workflow.py", line 10, in <module>
      import yaml
      ImportError: No module named yaml


      That is indicative of the cronjob somehow not running the file without the virtual environment activated.



      To remedy this I added a line to the /home/user/.bash_profile file:



      conda activate ~/anaconda3/envs/sql_server_etl/


      Now when I login the environment is activated automatically.



      However, the problem persists.



      I tried one more thing. I changed the cronjob, (and I also tried this in the bash file the cronjob runs) to explicitly manually activate the environment each time it runs, but to no avail:



      10 * * * * conda activate ~/anaconda3/envs/sql_server_etl/ && bash /work/sql_server_etl/src/python/run_parallel_workflow.sh >> /home/etlservice/cronlog.log 2>&1


      Of course, nothing I've tried has fixed it. I really know nothing about linux so maybe there's something obvious I need to change.



      So, is there anyway to specify that the cronjob should run under a virutal environment?









      share|improve this question










      share|improve this question




      share|improve this question









      asked Jul 12 at 18:37









      Legit Stack

      1011




      1011




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Found answer on stack over flow:



          https://stackoverflow.com/questions/3287038/cron-and-virtualenv



          The solution is to reference the python executable within the virtual environment itself. In my case, I changed the bash file to run this executable:



          /home/etlservice/anaconda3/envs/sql_server_etl/bin/python






          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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            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%2f454957%2fcron-job-to-run-under-conda-virtual-environment%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            Found answer on stack over flow:



            https://stackoverflow.com/questions/3287038/cron-and-virtualenv



            The solution is to reference the python executable within the virtual environment itself. In my case, I changed the bash file to run this executable:



            /home/etlservice/anaconda3/envs/sql_server_etl/bin/python






            share|improve this answer



























              up vote
              0
              down vote













              Found answer on stack over flow:



              https://stackoverflow.com/questions/3287038/cron-and-virtualenv



              The solution is to reference the python executable within the virtual environment itself. In my case, I changed the bash file to run this executable:



              /home/etlservice/anaconda3/envs/sql_server_etl/bin/python






              share|improve this answer

























                up vote
                0
                down vote










                up vote
                0
                down vote









                Found answer on stack over flow:



                https://stackoverflow.com/questions/3287038/cron-and-virtualenv



                The solution is to reference the python executable within the virtual environment itself. In my case, I changed the bash file to run this executable:



                /home/etlservice/anaconda3/envs/sql_server_etl/bin/python






                share|improve this answer















                Found answer on stack over flow:



                https://stackoverflow.com/questions/3287038/cron-and-virtualenv



                The solution is to reference the python executable within the virtual environment itself. In my case, I changed the bash file to run this executable:



                /home/etlservice/anaconda3/envs/sql_server_etl/bin/python







                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited Jul 12 at 19:04


























                answered Jul 12 at 18:51









                Legit Stack

                1011




                1011






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f454957%2fcron-job-to-run-under-conda-virtual-environment%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Peggy Mitchell

                    The Forum (Inglewood, California)

                    Palaiologos