萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Linux yum 报错 Loaded plugins / Existing lock 解決:刪除 yum.pid 重啟服務

執行 yum 時出現 Existing lock /var/run/yum.pid: another copy is running as pid XXXX,重啟無效。原因是上次 yum 進程異常終止殘留 PID 檔,且 yum-updatesd 守護進程仍持有鎖。本文提供標準清理流程。

yum 鎖定錯誤

錯誤輸出範例

Loaded plugins: fastestmirror, security
Existing lock /var/run/yum.pid: another copy is running as pid 4733.
Another app is currently holding the yum lock; waiting for it to exit...
The other application is: yum-updatesd-he
Memory : 22 M RSS (249 MB VSZ)
Started: Fri Jul 6 12:18:29 2012 - 53:47 ago
State  : Sleeping, pid: 4733

標準解決步驟

  1. 確認進程確實不存在或可安全終止:
    # ps -fp 4733
    # kill 4733   # 若仍存活
    
  2. 刪除殘留 PID 檔:
    # rm -f /var/run/yum.pid
    
  3. 重啟 yum-updatesd 服務(CentOS 5/6):
    # /sbin/service yum-updatesd restart
    # 或
    # systemctl restart yum-updatesd   # CentOS 7+
    
  4. 驗證:
    # yum clean all
    # yum makecache
    

進階:禁用/移除 yum-updatesd(建議)

yum-updatesd 為舊版自動更新守護進程,易造成鎖衝突,現代系統建議使用 yum-cron / dnf-automatic / dnf5-automatic 定時任務替代。

# CentOS 6
# chkconfig yum-updatesd off
# service yum-updatesd stop
# yum remove yum-updatesd

# CentOS 7+
# systemctl disable --now yum-updatesd
# yum remove yum-updatesd

預防鎖衝突最佳實務

  • 避免同時開啟多個終端執行 yum/dnf。
  • 腳本中使用 flock -n /var/run/yum.pid -c "yum -y install ..." 確保串行。
  • 定期 yum clean all 清理快取減少 I/O 競爭。
  • 生產環境鎖定版本、使用本地鏡像源、離線 RPM 部署。

DNF 時代的鎖處理

# dnf 使用 /var/run/dnf.pid
# rm -f /var/run/dnf.pid
# systemctl restart dnf-makecache.timer

快速清理 PID 鎖、重啟守護進程、禁用舊式自動更新,讓包管理器恢復正常運作。

copyright © 萬盛學電腦網 all rights reserved