How to run a command inside a virtualenv using systemd

Clash 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
systemd
add a comment |Â
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
systemd
Could you solve it yet? If so, how?
â black
Jul 13 at 23:44
add a comment |Â
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
systemd
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
systemd
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
add a comment |Â
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
add a comment |Â
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/binis created, where executables live - noticeably a new python. Thus running a script with#! /path/to/ENV/bin/pythonwould 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.)
add a comment |Â
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/binis created, where executables live - noticeably a new python. Thus running a script with#! /path/to/ENV/bin/pythonwould 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.)
add a comment |Â
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/binis created, where executables live - noticeably a new python. Thus running a script with#! /path/to/ENV/bin/pythonwould 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.)
add a comment |Â
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/binis created, where executables live - noticeably a new python. Thus running a script with#! /path/to/ENV/bin/pythonwould 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.)
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/binis created, where executables live - noticeably a new python. Thus running a script with#! /path/to/ENV/bin/pythonwould 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.)
answered Jul 26 at 5:57
Filipe Brandenburger
3,695622
3,695622
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Could you solve it yet? If so, how?
â black
Jul 13 at 23:44