Python Forum
[SOLVED] [Linux] Script in cron stops after first run in loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] [Linux] Script in cron stops after first run in loop
#1
Hello,

For some reason, the following Python script that runs every hour through cron stops after the first run in the loop after downloading the RSS feed:

#!/usr/bin/python3

from bs4 import BeautifulSoup
import requests
import datetime as dt
from datetime import datetime

URL="https://www.acme.com/feed/"
resp = requests.get(URL)
soup = BeautifulSoup(resp.text,'xml')

print("<!doctype html>")
print('<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head>')
print('<body><h1>Acme.com RSS feed</h1><ul>')
for item in soup("item"):
        #Thu, 10 Nov 2022 18:15:41 +0000
        date_obj = dt.datetime.strptime(item.pubDate.string, "%a, %d %b %Y %H:%M:%S %z")
        pubDate = "{} {} {}".format(date_obj.day,date_obj.strftime("%b"),date_obj.year)

        link=item.link.string
        title=item.title.string
        print("<li><a href='{}'>{} - {}</a></li>".format(link,title,pubDate))
print("</ul>")

now = datetime.now()
date_time = now.strftime("%d/%m//%Y %H:%M")
print("Last updated: ",date_time)

print("</body></html>")
Here's crontab:
0 * * * * /root/feed.py > /usr/share/nginx/mysite/feed.html

And here's the outputt:
<!doctype html>
<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head>
<body><h1>Acme.com RSS feed</h1><ul>
<li><a href='https://www.acme.com/2022/11/15/latest-article/'>Latest article - 15 Nov 2022</a></li>


Any idea what it could be? I see nothing in /var/log/messsages.

Thank you.
Reply
#2
It looks good to me. You say there is nothing in /var/log/messsages. Did you also inspect /var/log/syslog?
You can force error messages also to be sent to your output file by appending " 2>&1 ". (Meaning: send stderr (2) to stdout (1).) like this:
Quote:0 * * * * /root/feed.py > /usr/share/nginx/mysite/feed.html 2>&1
Perhaps an error message appears in your output to help you understand what is happening.
Reply
#3
Sorry, I forgot to say: The script was OK when run manually.

But indeed, logging off and back on displayed this error…

-bash: warning: setlocale: LC_ALL: cannot change locale (UTF-8" or "en_US.UTF-8)

… which was solved by editing /etc/default/locale to go back to its original state…

LANG="C"
#BAD LANG="UTF-8" or "en_US.UTF-8"
#BAD LC_ALL="UTF-8" or "en_US.UTF-8"
#BAD LC_CTYPE="UTF-8" or "en_US.UTF-8"


… which also solved the cron issue.

Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is possible to run the python command to call python script on linux? cuten222 6 738 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  ModuleNotFoundError only if script is launched by cron Charles33 8 5,253 Jan-12-2024, 12:55 AM
Last Post: caicaifan
  pip stops waiting for python walker 6 1,068 Nov-28-2023, 06:55 PM
Last Post: walker
  Button to stop while loop from another script Absolutewind 5 918 Sep-25-2023, 11:20 PM
Last Post: deanhystad
  [SOLVED] [loop] Exclude ranges in… range? Winfried 2 1,475 May-14-2023, 04:29 PM
Last Post: Winfried
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,150 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  math formula does not give the same result as bash script [SOLVED] AlphaInc 3 981 Apr-02-2023, 07:21 PM
Last Post: AlphaInc
  [SOLVED] [BS] Why new tag only added at the end when defined outside the loop? Winfried 1 976 Sep-05-2022, 09:36 AM
Last Post: snippsat
  How to compile a Python script for a Windows / Linux executable? netanelst 2 1,340 May-24-2022, 07:02 AM
Last Post: netanelst
  Loop through list of ip-addresses [SOLVED] AlphaInc 7 4,004 May-11-2022, 02:23 PM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020