How to run a command inside a virtualenv using systemd

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











up vote
7
down vote

favorite












I believe this should be simple but I can't get it to work properly.



These are the commands I can run on command line:



cd /home/debian/ap

# Start a virtualenv
source venv-ap/bin/activate

# This needs to happen inside the virtualenv and takes ~20 seconds
crossbar start

# Outside the virtualenv, perhaps in a different command line window
python3 /home/debian/myscript.py


These commands have to be done in this order. Due to the virtualenv, the non-executable for crossbar, and the separate python script afterwards, I haven't been able to figure out the best way to get this to work. My current work-in-progress:



[Unit]
Description=Start CB
After=network.target

[Service]
Type=simple
User=debian
ExecStartPre=source /home/debian/ap/venv-ap/bin/activate
ExecStart=cd /home/debian/ap/ && crossbar start
Restart=always

[Install]
WantedBy=multi-user.target






share|improve this question






















  • Could you solve it yet? If so, how?
    – black
    Jul 13 at 23:44














up vote
7
down vote

favorite












I believe this should be simple but I can't get it to work properly.



These are the commands I can run on command line:



cd /home/debian/ap

# Start a virtualenv
source venv-ap/bin/activate

# This needs to happen inside the virtualenv and takes ~20 seconds
crossbar start

# Outside the virtualenv, perhaps in a different command line window
python3 /home/debian/myscript.py


These commands have to be done in this order. Due to the virtualenv, the non-executable for crossbar, and the separate python script afterwards, I haven't been able to figure out the best way to get this to work. My current work-in-progress:



[Unit]
Description=Start CB
After=network.target

[Service]
Type=simple
User=debian
ExecStartPre=source /home/debian/ap/venv-ap/bin/activate
ExecStart=cd /home/debian/ap/ && crossbar start
Restart=always

[Install]
WantedBy=multi-user.target






share|improve this question






















  • Could you solve it yet? If so, how?
    – black
    Jul 13 at 23:44












up vote
7
down vote

favorite









up vote
7
down vote

favorite











I believe this should be simple but I can't get it to work properly.



These are the commands I can run on command line:



cd /home/debian/ap

# Start a virtualenv
source venv-ap/bin/activate

# This needs to happen inside the virtualenv and takes ~20 seconds
crossbar start

# Outside the virtualenv, perhaps in a different command line window
python3 /home/debian/myscript.py


These commands have to be done in this order. Due to the virtualenv, the non-executable for crossbar, and the separate python script afterwards, I haven't been able to figure out the best way to get this to work. My current work-in-progress:



[Unit]
Description=Start CB
After=network.target

[Service]
Type=simple
User=debian
ExecStartPre=source /home/debian/ap/venv-ap/bin/activate
ExecStart=cd /home/debian/ap/ && crossbar start
Restart=always

[Install]
WantedBy=multi-user.target






share|improve this question














I believe this should be simple but I can't get it to work properly.



These are the commands I can run on command line:



cd /home/debian/ap

# Start a virtualenv
source venv-ap/bin/activate

# This needs to happen inside the virtualenv and takes ~20 seconds
crossbar start

# Outside the virtualenv, perhaps in a different command line window
python3 /home/debian/myscript.py


These commands have to be done in this order. Due to the virtualenv, the non-executable for crossbar, and the separate python script afterwards, I haven't been able to figure out the best way to get this to work. My current work-in-progress:



[Unit]
Description=Start CB
After=network.target

[Service]
Type=simple
User=debian
ExecStartPre=source /home/debian/ap/venv-ap/bin/activate
ExecStart=cd /home/debian/ap/ && crossbar start
Restart=always

[Install]
WantedBy=multi-user.target








share|improve this question













share|improve this question




share|improve this question








edited Dec 11 '17 at 20:55

























asked Dec 7 '17 at 23:38









sscirrus

1484




1484











  • Could you solve it yet? If so, how?
    – black
    Jul 13 at 23:44
















  • Could you solve it yet? If so, how?
    – black
    Jul 13 at 23:44















Could you solve it yet? If so, how?
– black
Jul 13 at 23:44




Could you solve it yet? If so, how?
– black
Jul 13 at 23:44










1 Answer
1






active

oldest

votes

















up vote
0
down vote













This doesn't work because source is a shell command, so systemd's ExecStart= or ExecStartPre= won't understand them directly... (BTW, the same is true for cd and the &&.)



You could achieve that by running a shell explicitly and running all your commands together there:



ExecStart=/bin/sh -c 'cd /home/debian/ap/ && source venv-ap/bin/activate && crossbar start'


But a better approach is, instead of sourcing the "activate" script, to use the python executable in the bin/ of your virtualenv directly.



If you look at virtualenv's usage document, you'll notice it says:




ENV/bin is created, where executables live - noticeably a new python. Thus running a script with #! /path/to/ENV/bin/python would run that script under this virtualenv’s python.




In other words, assuming crossbar is the Python script you want to run that requires the venv-ap virtualenv, simply begin crossbar with:



#!/home/debian/ap/venv-ap/bin/python


And it will automatically use the virtualenv whenever invoked.



Also possible, invoking the Python interpreter from the virtualenv directly, with:



ExecStart=/home/debian/ap/venv-ap/bin/python /path/to/crossbar start


(Also, regarding running in a specific directory, setting WorkingDirectory=/home/debian/ap is better than using a cd command. You don't need a shell that way, and systemd can do better error handling for you.)






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%2f409609%2fhow-to-run-a-command-inside-a-virtualenv-using-systemd%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













    This doesn't work because source is a shell command, so systemd's ExecStart= or ExecStartPre= won't understand them directly... (BTW, the same is true for cd and the &&.)



    You could achieve that by running a shell explicitly and running all your commands together there:



    ExecStart=/bin/sh -c 'cd /home/debian/ap/ && source venv-ap/bin/activate && crossbar start'


    But a better approach is, instead of sourcing the "activate" script, to use the python executable in the bin/ of your virtualenv directly.



    If you look at virtualenv's usage document, you'll notice it says:




    ENV/bin is created, where executables live - noticeably a new python. Thus running a script with #! /path/to/ENV/bin/python would run that script under this virtualenv’s python.




    In other words, assuming crossbar is the Python script you want to run that requires the venv-ap virtualenv, simply begin crossbar with:



    #!/home/debian/ap/venv-ap/bin/python


    And it will automatically use the virtualenv whenever invoked.



    Also possible, invoking the Python interpreter from the virtualenv directly, with:



    ExecStart=/home/debian/ap/venv-ap/bin/python /path/to/crossbar start


    (Also, regarding running in a specific directory, setting WorkingDirectory=/home/debian/ap is better than using a cd command. You don't need a shell that way, and systemd can do better error handling for you.)






    share|improve this answer
























      up vote
      0
      down vote













      This doesn't work because source is a shell command, so systemd's ExecStart= or ExecStartPre= won't understand them directly... (BTW, the same is true for cd and the &&.)



      You could achieve that by running a shell explicitly and running all your commands together there:



      ExecStart=/bin/sh -c 'cd /home/debian/ap/ && source venv-ap/bin/activate && crossbar start'


      But a better approach is, instead of sourcing the "activate" script, to use the python executable in the bin/ of your virtualenv directly.



      If you look at virtualenv's usage document, you'll notice it says:




      ENV/bin is created, where executables live - noticeably a new python. Thus running a script with #! /path/to/ENV/bin/python would run that script under this virtualenv’s python.




      In other words, assuming crossbar is the Python script you want to run that requires the venv-ap virtualenv, simply begin crossbar with:



      #!/home/debian/ap/venv-ap/bin/python


      And it will automatically use the virtualenv whenever invoked.



      Also possible, invoking the Python interpreter from the virtualenv directly, with:



      ExecStart=/home/debian/ap/venv-ap/bin/python /path/to/crossbar start


      (Also, regarding running in a specific directory, setting WorkingDirectory=/home/debian/ap is better than using a cd command. You don't need a shell that way, and systemd can do better error handling for you.)






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        This doesn't work because source is a shell command, so systemd's ExecStart= or ExecStartPre= won't understand them directly... (BTW, the same is true for cd and the &&.)



        You could achieve that by running a shell explicitly and running all your commands together there:



        ExecStart=/bin/sh -c 'cd /home/debian/ap/ && source venv-ap/bin/activate && crossbar start'


        But a better approach is, instead of sourcing the "activate" script, to use the python executable in the bin/ of your virtualenv directly.



        If you look at virtualenv's usage document, you'll notice it says:




        ENV/bin is created, where executables live - noticeably a new python. Thus running a script with #! /path/to/ENV/bin/python would run that script under this virtualenv’s python.




        In other words, assuming crossbar is the Python script you want to run that requires the venv-ap virtualenv, simply begin crossbar with:



        #!/home/debian/ap/venv-ap/bin/python


        And it will automatically use the virtualenv whenever invoked.



        Also possible, invoking the Python interpreter from the virtualenv directly, with:



        ExecStart=/home/debian/ap/venv-ap/bin/python /path/to/crossbar start


        (Also, regarding running in a specific directory, setting WorkingDirectory=/home/debian/ap is better than using a cd command. You don't need a shell that way, and systemd can do better error handling for you.)






        share|improve this answer












        This doesn't work because source is a shell command, so systemd's ExecStart= or ExecStartPre= won't understand them directly... (BTW, the same is true for cd and the &&.)



        You could achieve that by running a shell explicitly and running all your commands together there:



        ExecStart=/bin/sh -c 'cd /home/debian/ap/ && source venv-ap/bin/activate && crossbar start'


        But a better approach is, instead of sourcing the "activate" script, to use the python executable in the bin/ of your virtualenv directly.



        If you look at virtualenv's usage document, you'll notice it says:




        ENV/bin is created, where executables live - noticeably a new python. Thus running a script with #! /path/to/ENV/bin/python would run that script under this virtualenv’s python.




        In other words, assuming crossbar is the Python script you want to run that requires the venv-ap virtualenv, simply begin crossbar with:



        #!/home/debian/ap/venv-ap/bin/python


        And it will automatically use the virtualenv whenever invoked.



        Also possible, invoking the Python interpreter from the virtualenv directly, with:



        ExecStart=/home/debian/ap/venv-ap/bin/python /path/to/crossbar start


        (Also, regarding running in a specific directory, setting WorkingDirectory=/home/debian/ap is better than using a cd command. You don't need a shell that way, and systemd can do better error handling for you.)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 26 at 5:57









        Filipe Brandenburger

        3,695622




        3,695622



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f409609%2fhow-to-run-a-command-inside-a-virtualenv-using-systemd%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)