現在cronでPythonのスクレイピングプログラムを以下の設定で毎分定期実行しています。
cron
1* * * * * (. /home/admin/ProjectName/bin/activate && /usr/bin/python3 /home/admin/ProjectName/main.py && deactivate) > /tmp/log 2>&1
ただ数回実行されたあと、以下のエラーが出てプログラムがちゃんと走らなくなってしまいます。
====== WebDriver manager ====== Current google-chrome version is 99.0.4844 Get LATEST chromedriver version for 99.0.4844 google-chrome Driver [/root/.wdm/drivers/chromedriver/linux64/99.0.4844.51/chromedriver] found in cache Traceback (most recent call last): File "/home/admin/ProjectName/main.py", line 182, in <module> driver = webdriver.Chrome(service=chrome_service, options=options) File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/chrome/webdriver.py", line 70, in __init__ super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog", File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/chromium/webdriver.py", line 93, in __init__ RemoteWebDriver.__init__( File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 269, in __init__ self.start_session(capabilities, browser_profile) File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 360, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 425, in execute self.error_handler.check_response(response) File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed. (unknown error: DevToolsActivePort file doesn't exist) (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.) Stacktrace: #0 0x55eb4f8ac7d3 <unknown> #1 0x55eb4f608688 <unknown> #2 0x55eb4f62bb27 <unknown> #3 0x55eb4f62768f <unknown> #4 0x55eb4f662355 <unknown> #5 0x55eb4f65c5a3 <unknown> #6 0x55eb4f631ddc <unknown> #7 0x55eb4f632de5 <unknown> #8 0x55eb4f8dd49d <unknown> #9 0x55eb4f8f660c <unknown> #10 0x55eb4f8df205 <unknown> #11 0x55eb4f8f6ee5 <unknown> #12 0x55eb4f8d3070 <unknown> #13 0x55eb4f912488 <unknown> #14 0x55eb4f91260c <unknown> #15 0x55eb4f92bc6d <unknown> #16 0x7f5b3368c609 <unknown>
/tmpフォルダを見てみるとサーバー起動後にはなかったcom.google.Chromeというファイルが大量にできてしまうので、この一時ファイルが原因かと思い以下のcronも動かして10分前の一時ファイルを削除するようにしてみたのですが、問題は解決できず...
cron
1* * * * * /home/admin/cleanuptmp.sh > /tmp/log-cleanup 2>&1
bash(cleanuptmp.sh)
1#!/bin/bash 2 3find /tmp -name ".com.google.Chrome*" -amin +10 -exec rm -r {} \;
サーバーを再起動させたらまた最初だけは上手く動くようになるので、プログラムによって生成されている一時ファイルが原因かと思っているのですが、解決できずにおります。
原因がわかる方がおられましたらお知恵をお貸しいただけると助かります。
【実行環境】
・サーバー:さくらVPS
・OS:Ubuntu 20.04 amd64(64bit)
【追記】
実行しているプログラムの概要は以下の通りです。
chrome_service = service.Service(executable_path=ChromeDriverManager().install()) driver = webdriver.Chrome(service=chrome_service, options=options) def main(): # スクレイピング処理 # Webサイトから情報を取得し、LINEで通知しています。 # ファイルのダウンロードなどは行なっていません。 if __name__ == "__main__": lockfilePath = 'lockfile.lock' with open(lockfilePath , "w") as lockFile: try: fcntl.flock(lockFile, fcntl.LOCK_EX | fcntl.LOCK_NB) main() driver.delete_all_cookies() driver.close() driver.quit() except IOError: print('process already exists')
どのようなプログラムかわかりませんが、正常に終了せずにプログラムが起動した状態で残っていて、毎分実行しているので、どんどん残っているものが増えていって、リソース不足で起動できないということはありませんか?
プログラムの概要だけ追記させて頂きました。
一応close()やquit()で終了しているつもりなのですが、、
検索してみると、--disable-dev-shm-usage オプションを付けることで回避できるケースが多いように見受けられます。
ご回答ありがとうございます。オプション内容、記載しておらずすみません。
一応、オプション下記の通り設定しております。
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
UA = 'SetEnvIfNoCase User-Agent "NaverBot" getout'
options.add_argument(f'user-agent={UA}')
一回の実行は1分以内に終わるのでしょうか?(cronで1分指定なら30秒以内で終わるのが安全)
測定してみたところ1回の動作に3〜5分程度かかっております。
以下のサイトを参考に重複で起動しないようにしているのですが、やはりこのような処理の場合間隔をもっと開けたほうが良いのでしょうか...?
https://qiita.com/norami_dream/items/40d7ae85344365863565
flock() で LOCK_NB を付けていますので問題は無さそうですが、ログファイル(/tmp/log)には "process already exists" が出力されているのですよね?
chrome_service = service.Service(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=chrome_service, options=options)
の部分をロックの外に書く理由はなんでしょう? (素朴な疑問なのでこの欄で失礼します)
> melian様
はい、process already existsされておりましたがその前にChromeが起動してしまっておりました...
> quickquip様
記述をロック内に移動したら正常に動作するようになりました...!! ありがとうございました!
回答1件
あなたの回答
tips
プレビュー