I have a python script main.py
that needs to start the scrapy executable, so I use Popen to do that (using subprocess.call()
has same result). To simplify things I just run the help argument of scrapy.
import subprocess
...
p = subprocess.Popen(['scrapy', '-h'])
The script needs to be run inside a vitualenv which has scrapy installed on it. When I activate the virtualenv and run the script as python main.py
the command scrapy -h
is executed as expected.
Now, I need this script to be run as a systemd service. I have created the systemd unit 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
When I enable and start the service with sudo systemctl start scrapy-test
the code runs normally until p = subprocess.Popen(['scrapy', '-h'])
when I get an exception raised
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
I have also tried to change the working directory by adding os.chdir('{{ virtualenv_path }}/bin')
just before Popen
, but I get the exact same exception. I am sure the script is run through the virtualenv because (1) it is set so in the ExecStart
attribute and (2) the script would raise exceptions earlier when importing modules that are not installed in the system's python.