
我在 Ubuntu 16.04 下的 VPS 上使用 Apache 2.4.18 下的 mod_perl 腳本。腳本從磁碟開啟檔案(網頁範本)。有時,他們不再看到這些文件,伺服器錯誤日誌充滿了以下錯誤:
[時間] [:錯誤] [pid PID:tid TID] 檔案錯誤 - 檔案名稱: 找不到\n
open()
當 Perl函數傳回 false時,這些訊息會由 Template 套件產生。 Apache 重新啟動後,這些檔案再次可見,但只能持續一段時間。過了一段時間,問題又出現了。
這種行為的原因是什麼?
答案1
也許 mod_perl/apache 沒有正確關閉檔案句柄。一段時間後,當達到開啟檔案的最大數量(1024 個軟體檔案/4096 個硬檔案ulimit -Sn;ulimit -Hn
)時,apache 的重新啟動將關閉所有過時的句柄。
下次當錯誤發生時,您可以透過從 apache/perl 取得 pid 來研究這個理論。
# ps aux | grep apache
或者
# pidof apache
或 perl 模組(如果它有自己的進程)。
範例輸出:
28294
列出 PID 開啟的文件
# lsof -p 28294
# lsof -a -p 28294
# counting
# lsof -a -p 28294| wc -l
或者
# cd /proc/28294/fd
# ls -l | less
# count open files with
# ls -l | wc -l