Python popen doesn't recognize scrapy when python script runs as systemd service

I have a python script main.pythat 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.pycommand scrapy -hexecutes, 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) , .

+4
3

:

ExecStart={{ virtualenv_path }}/bin/python {{ test_path }}/main.py

scrapy $VIRTUAL_ENV/bin, systemd $PATH.

:

import os
import subprocess
p = subprocess.Popen(['%s/bin/scrapy' % os.environ['VIRTUAL_ENV'], '-h'])
0

, , python p.wait():

import os
import subprocess
p = subprocess.Popen(['%s/bin/scrapy' % os.environ['VIRTUAL_ENV'], '-h'])
p.wait()

systemctl .

0

You must specify the current working directory when calling in Popento ensure execution scrapyfrom the correct directory.

0
source

Source: https://habr.com/ru/post/1688149/


All Articles