gpt4 book ai didi

python - Crontab 停止 - Ubuntu 服务器的管道损坏

转载 作者:行者123 更新时间:2023-12-04 19:09:55 24 4
gpt4 key购买 nike

AWS/EC2/ubuntu 的第一次用户在这里。我在使用 crontab 的 ubuntu 服务器上运行 python 脚本(网络爬虫,使用 chromedriver、selenium、beautifulsoup 等)时遇到问题。这是 AWS 上的 EC2 实例。
我已经让 crontab 作为一个简单的测试工作,创建了一个简单的 txt 文件,上面写着“你好”。但是,这个当前的 cronjob 一直失败:

这很简单,每天中午 12:17 执行一个脚本:

    ubuntu@ip-172-31-31-200:~$ crontab -l
17 12 * * * python3 BandsInTown_Scraper_SF.py

作业不执行。我的 crontab 状态为:
    ubuntu@ip-172-31-31-200:~$ sudo service cron status
● cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2019-11-02 00:18:36 UTC; 1 weeks 0 days ago
Docs: man:cron(8)
Main PID: 830 (cron)
Tasks: 1 (limit: 1152)
CGroup: /system.slice/cron.service
└─830 /usr/sbin/cron -f

Nov 09 16:17:01 ip-172-31-31-200 CRON[27857]: pam_unix(cron:session): session opened for user root by (uid=0)
Nov 09 16:17:01 ip-172-31-31-200 CRON[27858]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Nov 09 16:17:01 ip-172-31-31-200 CRON[27857]: pam_unix(cron:session): session closed for user root
Nov 09 17:17:01 ip-172-31-31-200 CRON[27908]: pam_unix(cron:session): session opened for user root by (uid=0)
Nov 09 17:17:01 ip-172-31-31-200 CRON[27909]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Nov 09 17:17:01 ip-172-31-31-200 CRON[27908]: pam_unix(cron:session): session closed for user root
Nov 09 18:15:01 ip-172-31-31-200 cron[830]: (ubuntu) RELOAD (crontabs/ubuntu)
Nov 09 18:17:01 ip-172-31-31-200 CRON[28242]: pam_unix(cron:session): session opened for user root by (uid=0)
Nov 09 18:17:01 ip-172-31-31-200 CRON[28243]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Nov 09 18:17:01 ip-172-31-31-200 CRON[28242]: pam_unix(cron:session): session closed for user root

而且,当我深入研究过去失败的 cron 作业的日志时,我得到:
    Nov  5 02:17:01 ip-172-31-31-200 CRON[14524]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Nov 5 02:26:58 ip-172-31-31-200 systemd[1]: Starting Message of the Day...
Nov 5 02:26:58 ip-172-31-31-200 50-motd-news[14532]: * Kata Containers are now fully integrated in Charmed Kubernetes 1.16!
Nov 5 02:26:58 ip-172-31-31-200 50-motd-news[14532]: Yes, charms take the Krazy out of K8s Kata Kluster Konstruction.
Nov 5 02:26:58 ip-172-31-31-200 50-motd-news[14532]: https://ubuntu.com/kubernetes/docs/release-notes
Nov 5 02:26:58 ip-172-31-31-200 systemd[1]: Started Message of the Day.
Nov 5 02:36:28 ip-172-31-31-200 systemd-timesyncd[577]: Network configuration changed, trying to establish connection.
Nov 5 02:36:28 ip-172-31-31-200 systemd-timesyncd[577]: Synchronized to time server 91.189.89.198:123 (ntp.ubuntu.com).
Nov 5 03:06:28 ip-172-31-31-200 systemd-timesyncd[577]: Network configuration changed, trying to establish connection.
Nov 5 03:06:28 ip-172-31-31-200 systemd-timesyncd[577]: Synchronized to time server 91.189.89.198:123 (ntp.ubuntu.com).
Nov 5 03:17:01 ip-172-31-31-200 CRON[14621]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Nov 5 03:36:28 ip-172-31-31-200 systemd-timesyncd[577]: Network configuration changed, trying to establish connection.
Nov 5 03:36:28 ip-172-31-31-200 systemd-timesyncd[577]: Synchronized to time server 91.189.89.198:123 (ntp.ubuntu.com).
Nov 5 03:39:01 ip-172-31-31-200 CRON[14646]: (root) CMD ( test -x /etc/cron.daily/popularity-contest && /etc/cron.daily/popularity-contest --crond)
Nov 5 04:06:27 ip-172-31-31-200 systemd-timesyncd[577]: Network configuration changed, trying to establish connection.
Nov 5 04:06:27 ip-172-31-31-200 systemd-timesyncd[577]: Synchronized to time server 91.189.89.198:123 (ntp.ubuntu.com).
Nov 5 04:17:01 ip-172-31-31-200 CRON[14675]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Nov 5 04:27:25 ip-172-31-31-200 systemd[1]: Started Session 144 of user ubuntu.
Nov 5 04:36:27 ip-172-31-31-200 systemd-timesyncd[577]: Network configuration changed, trying to establish connection.
Nov 5 04:36:27 ip-172-31-31-200 systemd-timesyncd[577]: Synchronized to time server 91.189.89.198:123 (ntp.ubuntu.com).
Nov 5 04:41:04 ip-172-31-31-200 systemd[1]: Started Session 145 of user ubuntu.
Nov 5 04:41:09 ip-172-31-31-200 crontab[14897]: (ubuntu) LIST (ubuntu)

Network configuration changed, trying to establish connection.正在关注并想知道这是否是根本原因。 ubuntu 服务器是否需要启动并运行才能从 crontab 开始工作?我问是因为当我在终端上登录 ubuntu,然后让它运行一段时间并且我的计算机进入休眠状态时,我收到以下错误:
ubuntu@ip-172-31-31-200:~/bandsintown$ packet_write_wait: Connection to 3.132.213.71: Broken pipe

这是我问题的根本原因吗?我认为在 AWS 上运行代码的全部意义在于它可以独立于我的本地机器运行(即,如果互联网中断,如果终端退出 Ubuntu,等等)。

最佳答案

这可能是路径问题,我会尝试:

17 12 * * * cd /path/to/script && /path/to/python3 BandsInTown_Scraper_SF.py



也许保存输出以供以后查看:

17 12 * * * cd /path/to/script && /path/to/python3 BandsInTown_Scraper_SF.py > /tmp/out 2>&1

关于python - Crontab 停止 - Ubuntu 服务器的管道损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58782376/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com