目录:是一个目录

Directory: Is a directory

我已经设置了一个 cron 作业来 运行 一个 Python 脚本来抓取一些网页。

/etc/crontab

    GNU nano 2.3.1                                  File: crontab

    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=my_email_address@domain.com

    # For details see man 4 crontabs

    # Example of job definition:
    # .---------------- minute (0 - 59)
    # |  .------------- hour (0 - 23)
    # |  |  .---------- day of month (1 - 31)
    # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
    # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
    # |  |  |  |  |
    # *  *  *  *  * user-name  command to be executed

    */2 * * * * root /usr/bin scrapy crawl mycrawler

但是,电子邮件告诉我...

/bin/bash: /usr/bin: Is a directory

当我手动 运行 脚本时,它会将数据通过管道传输到我的数据库中,但是当 cron 作业执行脚本时,什么也没有...

/bin/bash: /usr/bin: Is a directory 消息暗指什么?!

/usr/bin

是类 Unix 操作系统上相当标准的目录,其中包含大部分可执行文件。

即您正在尝试让 cron 使用作为目录的可执行文件执行 "scrapy crawl mycrawler"。

您通常必须执行 bash 脚本(假设 bash 二进制文件在 /usr/bin 目录中):

*/2 * * * * root /usr/bin/bash scrapy.sh

或 python 命令(再次假设 python 二进制文件在 /usr/bin 目录中)

*/2 * * * * root /usr/bin/python scrapy.py

或者您可以将 scrapy 绝对路径添加到您的 PATH 变量中:

*/2 * * * * root scrapy crawl mycrawler

正如评论中所讨论的,最开始的错误是条目将 /usr/bin 放在了可执行文件应该位于的位置:

*/2 * * * * root /usr/bin scrapy crawl mycrawler
                 ^^^^^^^^
                 command

一旦固定为 scapy,最终的问题是 scrapy/usr/local/bin 而不是在你的 PATH 中。要更改此设置:

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin/

然后你应该可以做到:

 */2 * * * * root cd <project dir> && scrapy crawl mycrawler