简体   繁体   English

cron 未启动有效的 python 脚本

[英]cron not launching a working python script

I have more than some troubles getting cron working on my raspberry pi 3b.在我的树莓派 3b 上让 cron 工作时,我遇到了很多麻烦。 I wrote a python script and I have no issues launching it from thonny or terminal, it runs smoothly.我写了一个 python 脚本,从 thonny 或终端启动它没有问题,它运行顺利。

However, when launching the cron job in the following way (changing minutes and hours accordingly to test time):但是,当以以下方式启动 cron 作业时(根据测试时间更改分钟和小时):

25 21 * * * /usr/bin/python3 /home/pi/main.py >> /home/log/myscript.log 2>&1

I get the following error message:我收到以下错误消息:

Traceback (more recent call last):
   File "/home/pi/main.py", line 9, in <module>
       import tweepy
ImportError: No module named 'tweepy'

No matter if I call sudo crontab -e , nano crontab -e or cron -e .无论我调用sudo crontab -enano crontab -e还是cron -e I started investigating and I guessed that there where problems with the python version to call.我开始调查,我猜想 python 版本存在问题。

Therefore I substituted the python3 call with python3.5 , later with python3.5m and even with python3.8 with no results.因此,我用python3.5替换了python3调用,后来用python3.5m甚至python3.8没有结果。 In any attempt I tried launching the same command via terminal and it worked fine.在任何尝试中,我都尝试通过终端启动相同的命令,并且效果很好。 Tried to unistall and reinstall tweepy and checked the python used by Thonny which is the 3.5 version.尝试卸载并重新安装 tweepy 并检查了 Thonny 使用的 python 是 3.5 版本。

What can be trouble?有什么麻烦? It's driving me mad.它快把我逼疯了。

EDIT: I backed up my scripts and re-install raspberry pi to the latest version (5.10.17 linux kernel).编辑:我备份了我的脚本并将树莓派重新安装到最新版本(5.10.17 linux 内核)。 Again, I tried using the script with both Thonny and terminal and it run.同样,我尝试将脚本与 Thonny 和终端一起使用并运行。

Again, I had the same problem using crontab.同样,我在使用 crontab 时遇到了同样的问题。

However, I had an idea.不过,我有个主意。 I updated the tweepy version to the latest version available, it was like a desperate attempt and something changed.我将 tweepy 版本更新为可用的最新版本,这就像一次绝望的尝试,并且发生了一些变化。

Instead of having the error related to tweepy, cron gives me this: cron 没有与 tweepy 相关的错误,而是给了我这个:

Traceback (most recent call last):
  File "/home/pi/Documents/TwitterBot/main.py", line 17, in <module>
    a = soup.select('table > tr:has(> td > a:-soup-contains("San Pellegrino Terme")) td')
  File "/usr/lib/python3/dist-packages/bs4/element.py", line 1376, in select
    return soupsieve.select(selector, self, namespaces, limit, **kwargs)
  File "/usr/lib/python3/dist-packages/soupsieve/__init__.py", line 112, in select
    return compile(select, namespaces, flags, **kwargs).select(tag, limit)
  File "/usr/lib/python3/dist-packages/soupsieve/__init__.py", line 63, in compile
    return cp._cached_css_compile(pattern, namespaces, custom, flags)
  File "/usr/lib/python3/dist-packages/soupsieve/css_parser.py", line 205, in _cached_css_compile
    CSSParser(pattern, custom=custom_selectors, flags=flags).process_selectors(),
  File "/usr/lib/python3/dist-packages/soupsieve/css_parser.py", line 1010, in process_selectors
    return self.parse_selectors(self.selector_iter(self.pattern), index, flags)
  File "/usr/lib/python3/dist-packages/soupsieve/css_parser.py", line 852, in parse_selectors
    has_selector, is_html = self.parse_pseudo_class(sel, m, has_selector, iselector, is_html)
  File "/usr/lib/python3/dist-packages/soupsieve/css_parser.py", line 510, in parse_pseudo_class
    has_selector = self.parse_pseudo_open(sel, pseudo, has_selector, iselector, m.end(0))
  File "/usr/lib/python3/dist-packages/soupsieve/css_parser.py", line 654, in parse_pseudo_open
    sel.selectors.append(self.parse_selectors(iselector, index, flags))
  File "/usr/lib/python3/dist-packages/soupsieve/css_parser.py", line 852, in parse_selectors
    has_selector, is_html = self.parse_pseudo_class(sel, m, has_selector, iselector, is_html)
  File "/usr/lib/python3/dist-packages/soupsieve/css_parser.py", line 585, in parse_pseudo_class
    "'{}' pseudo-class is not implemented at this time".format(pseudo)
NotImplementedError: ':-soup-contains' pseudo-class is not implemented at this time

Which is a trouble related to the version of beautifulsup Cron is running.这是与 beautifulsup Cron 正在运行的版本有关的问题。

I tried to update beautifulsup to the latest version available with no results.我尝试将 beautifulsup 更新到可用的最新版本,但没有结果。

I am starting to run out of idea, Any help would be appreciated.我开始没有想法了,任何帮助将不胜感激。

If you are running a virtual environment your paths in a shell script need to be for that virtual environment.如果您正在运行虚拟环境,则 shell 脚本中的路径需要适用于该虚拟环境。 Here's how I run shell on AWS linux instance.这是我在 AWS linux 实例上运行 shell 的方法。 You can play around with this but it took me a while to figure it out.你可以玩这个,但我花了一段时间才弄清楚。 Maybe just add the long environment path in cron and skip the shell script.也许只是在 cron 中添加长环境路径并跳过 shell 脚本。

This is inside my_shell.sh这是在 my_shell.sh 里面

/usr/local/bin/pipenv run ~/.local/share/virtualenvs/ec2-user-zzkNbF-x/bin/python /home/ec2-user/my_path_to_file/my_file.py

This is my crontab setting这是我的 crontab 设置

0 21 * * mon-fri /home/ec2-user/my_path_to_file/my_shell.sh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM