Python Forum

Full Version: [SOLVED] [Linux] Script in cron stops after first run in loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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.
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.