I have a python script main.py
that should run the scrapy executable , so I use Popen for this (using subprocess.call()
gives the same result). To simplify things, I just use the scrapy help argument.
import subprocess
...
p = subprocess.Popen(['scrapy', '-h'])
The script must be run inside the vitualenv on which scrapy is installed. When I activate virtualenv and run the script as the python main.py
command scrapy -h
executes, as expected.
Now I need this script to run as a systemd service. I created a system module for the script as follows:
[Unit]
Description=Scrapy test
After=network.target
[Service]
User={{ scrapy_user }}
WorkingDirectory={{ test_path }}
ExecStart={{ virtualenv_path }}/bin/python {{ test_path }}/main.py
[Install]
WantedBy=multi-user.target
sudo systemctl start scrapy-test
sudo systemctl start scrapy-test
p = subprocess.Popen(['scrapy', '-h'])
File "{{ test_path }}/main.py", line 52, in launch_spider
p = subprocess.Popen(['scrapy', '-h'])
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
, os.chdir('{{ virtualenv_path }}/bin')
Popen
, . , virtualenv, (1) ExecStart
(2) , .