2016-11-17 79 views
0

我有一个运行的Python Scrapy抓取的多个实例的脚本,爬行是INT /root/crawler/batchscript.py无法更改目录使用bash脚本 - cron的

/root/crawler/我有scrapy履带。

爬行者工作得很好。

batchscript.py看起来是这样的,(只张贴代码培训相关)

from scrapy.settings import Settings 
from scrapy.utils.project import get_project_settings 
from amazon_crawler.spiders.amazon_scraper import MySpider 

process = CrawlerProcess(get_project_settings()) 

当我运行batchscrip.py/root/crawler/目录刮刀运行正常。

但是,当我从这个目录以外使用python /root/crawler/batchscript.py运行它然后它不会按预期运行(设置不正确导入),get_project_settings()是空的。

我试图创建一个bash脚本太 创建bash脚本称为batchinit.sh

#!/bin/bash 
alias batchscript="cd /root/crawler/" 
python batchscript.py 

和行为是相同:(

当我运行batchinit.sh/root/crawler/目录刮板 运行好。

B当我从这个目录之外运行它使用bash /root/crawler/batchinit.sh然后它不按预期运行, (设置不正确导入),get_project_settings()是 空。

为什么我这么做?什么是最终目标?

我想为这个脚本创建一个cronjob。我试图使用上面提到的命令安排cronjob,但是我有上面提到的问题。

+1

你想通过在shell脚本中定义别名来做什么?为什么不把cd/root/crawler /放在该行而不是将其别名化为批处理脚本? –

+0

scrapy和amazon_crawler模块在哪里?他们是在一个虚拟的环境? – lucasnadalutti

+0

这可能有所帮助:http://stackoverflow.com/a/22466264/2874789 –

回答

2

使用bash,你总是可以做:

cd /root/crawler && python batchscript.py 

它总是使用绝对路径在cron作业引用程序/可执行文件良策。