我正在使用Ubuntu 20.04.2 LTS的linode服务器上运行python脚本(python 3.8.5(。它从互联网上收集一些数据,并将其写入SQLite数据库。我用这个命令运行这个过程:
python3 -u my_script.py &>>logs.txt &
它运行了几个小时,然后停止,logs.txt文件中没有任何错误。它没有完成它应该做的事情(它应该运行大约20个小时(,当我在本地的windows机器上运行它时,它不会停止。
我在这里读到了几个主题,它可能是";失忆杀手";扼杀了我的进程,但是:
1.我在/var/log/syslog中查找了它的信息,没有任何被杀的信息。以下是在运行python脚本期间编写的系统日志:
Apr 9 17:15:01 localhost CRON[286303]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Apr 9 17:17:02 localhost CRON[286349]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Apr 9 17:25:01 localhost CRON[286501]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Apr 9 17:30:01 localhost CRON[286612]: (root) CMD ([ -x /etc/init.d/anacron ] && if [ ! -d /run/systemd/system ]; then /usr/sbin/invoke-rc.d anacron start >/dev/null; fi)
Apr 9 17:32:45 localhost systemd[1]: Started Run anacron jobs.
Apr 9 17:32:45 localhost anacron[286672]: Anacron 2.3 started on 2021-04-09
Apr 9 17:32:45 localhost anacron[286672]: Normal exit (0 jobs run)
Apr 9 17:32:45 localhost systemd[1]: anacron.service: Succeeded.
Apr 9 17:35:01 localhost CRON[286737]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Apr 9 17:45:01 localhost CRON[286959]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Apr 9 17:52:13 localhost systemd[1]: session-1602.scope: Succeeded.
Apr 9 17:55:01 localhost CRON[287176]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Apr 9 18:05:01 localhost CRON[287366]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Apr 9 18:15:01 localhost CRON[287595]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Apr 9 18:17:01 localhost CRON[287637]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Apr 9 18:25:01 localhost CRON[287835]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Apr 9 18:30:01 localhost CRON[287972]: (root) CMD ([ -x /etc/init.d/anacron ] && if [ ! -d /run/systemd/system ]; then /usr/sbin/invoke-rc.d anacron start >/dev/null; fi)
Apr 9 18:34:17 localhost systemd[1]: Started Run anacron jobs.
Apr 9 18:34:17 localhost anacron[288091]: Anacron 2.3 started on 2021-04-09
Apr 9 18:34:17 localhost anacron[288091]: Normal exit (0 jobs run)
Apr 9 18:34:17 localhost systemd[1]: anacron.service: Succeeded.
Apr 9 18:35:01 localhost CRON[288121]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Apr 9 18:45:01 localhost CRON[288403]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
- 当我用top命令监视内存使用情况时,脚本使用的RAM永远不会超过四分之一
请帮我找出可能导致脚本停止的原因。以下是python脚本:
import sqlite3
import requests
from bs4 import BeautifulSoup
from multiprocessing.dummy import Pool as Threadpool
from shutil import copyfile
from datetime import datetime
def get_ratings(object):
url = object['url']
try:
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
object['factor1'] = soup.select_one('#factor1').attrs['content']
object['factor2'] = soup.select_one('#factor2').attrs['content']
object['factor3'] = soup.select_one('#factor3').attrs['content']
for k, v in object.items():
if v == 'no':
object[k] = False
elif v == 'yes':
object[k] = True
except Exception as e:
print(e)
return object
counter = 0
while True:
counter += 1
print(counter)
con = sqlite3.connect('test.db')
cur = con.cursor()
cur.execute('SELECT id, url FROM projects WHERE factor1 IS NULL LIMIT 500')
all_objects = [{'id': id, 'url': url} for id, url in cur.fetchall()]
if len(all_objects) == 0:
break
print('Objects: ', len(all_objects))
cur.close()
con.close()
thpool = Threadpool(4)
results = thpool.map(get_ratings, all_objects)
con = sqlite3.connect('test.db')
cur = con.cursor()
for object in results:
try:
cur.execute('UPDATE projects SET factor1=:factor1, factor2=:factor2, '
'factor3=:factor3 WHERE id=:id', object)
con.commit()
except Exception as e:
print(e)
continue
cur.close()
con.close()
if counter % 50 == 0:
day = datetime.utcnow().day
hour = datetime.utcnow().hour
minute = datetime.utcnow().minute
copyfile('test.db', f'test_{hour}_{minute}_{day}.db')
但它没有抛出任何错误(并且它正确地将收集到的所有数据插入数据库(,我认为问题出在它运行的linode服务器上。我是linux的新手,不知道如何解决这个问题。我能以某种方式找出进程停止知道其PID的原因吗?
此外,我在服务器上同时运行了另一个python命令:
python3 -m http.server 4747 &>/dev/null &
它会以某种方式导致脚本停止吗?
更新:我注意到,如果我不关闭与服务器的SSH连接,脚本可以正常工作,不会停止。但是,如果服务器始终处于打开状态,ssh连接的中断如何导致脚本停止?为什么脚本在关闭连接后没有立即失败,而是运行了一段时间?
我的最佳猜测是,在运行此脚本的某个时刻,您会失去与服务器的连接,这首先会导致cur.execute((返回none,cur.fetchall((返回empty,并导致len(all_objects(==0返回true。这样就可以毫无错误地突破while循环。尝试将con=sq3lite.connect('test.db'(更改为
sqlite3.connect('test.db?mode=rw', uri=True)
这会导致数据库以特定权限打开,如果数据库不存在,则会导致以下错误。
sqlite3.OperationalError: unable to open database file
另一种选择可能是,您的cur.execute((调用没有按您的预期工作,并且实际上在您的预期结束条件之前为某个查询返回null。