PlusMagi's Blog By Pitt Phunsanit Cybersecurity,macOS การจัดการและตรวจสอบ Launch Agents และ Launch Daemons บน macOS

การจัดการและตรวจสอบ Launch Agents และ Launch Daemons บน macOS

ระบบ macOS มีกลไกการทำงานเบื้องหลังที่เรียกว่า Launch Agents และ Launch Daemons สำหรับรันโปรแกรมหรือสคริปต์อัตโนมัติ การตรวจสอบไฟล์เหล่านี้ช่วยให้ผู้ใช้สามารถตรวจหาซอฟต์แวร์แปลกปลอม วิเคราะห์ปัญหาเครื่องช้า หรือจัดการโปรแกรมที่เปิดขึ้นมาพร้อมกับระบบได้

ตำแหน่งจัดเก็บหลัก ไฟล์การตั้งค่าจะอยู่ในรูปแบบ .plist โดยแยกตามสิทธิ์การใช้งานและขอบเขต ดังนี้:

  • ~/Library/LaunchAgents: สคริปต์หรือโปรแกรมที่ทำงานภายใต้ผู้ใช้งานปัจจุบัน (User-level)
  • /Library/LaunchAgents: สคริปต์ที่ทำงานสำหรับผู้ใช้งานทุกคนในเครื่อง (System-level)
  • /Library/LaunchDaemons: โปรแกรมระดับสูงที่ทำงานด้วยสิทธิ์ผู้ดูแลระบบ (Root-level) โดยไม่จำเป็นต้องมีผู้ใช้งานล็อกอิน

คำสั่งตรวจสอบเบื้องต้น สามารถตรวจสอบไฟล์การตั้งค่าทั้งหมดในตำแหน่งหลักพร้อมกันได้ด้วยคำสั่งเดียวผ่าน Terminal

ls ~/Library/LaunchAgents /Library/LaunchAgents /Library/LaunchDaemons 2>/dev/null

คำสั่ง 2>/dev/null มีไว้เพื่อซ่อนข้อความแจ้งเตือน (Error messages) ในกรณีที่บางโฟลเดอร์ไม่มีอยู่จริงหรือไม่มีสิทธิ์เข้าถึง ช่วยให้ผลลัพธ์ที่แสดงออกมาสะอาดและอ่านง่ายเฉพาะรายการที่มีอยู่จริงเท่านั้น เข่น

ls ~/Library/LaunchAgents /Library/LaunchAgents /Library/LaunchDaemons 2>/dev/null
/Library/LaunchAgents:
com.adobe.ARMDCHelper.cc24aef4a1b90ed56a725c38014c95072f92651fb65e1bf9c8e43c37a23d420d.plist com.microsoft.SyncReporter.plist
com.avast.userinit.plist                                                                     com.microsoft.update.agent.plist
com.google.keystone.agent.plist                                                              com.paragon-software.extfs.notification-agent.plist
com.google.keystone.xpcservice.plist                                                         com.paragon-software.ntfs.notification-agent.plist
com.microsoft.OneDriveStandaloneUpdater.plist

/Library/LaunchDaemons:
com.adobe.ARMDC.Communicator.plist                  com.google.keystone.daemon.plist                    com.paragon-software.extfs.loader.plist             dev.orbstack.OrbStack.privhelper.plist
com.adobe.ARMDC.SMJobBlessHelper.plist              com.microsoft.OneDriveStandaloneUpdaterDaemon.plist com.paragon-software.extfsd.plist                   io.macfuse.app.launchservice.daemon.plist
com.avast.init.plist                                com.microsoft.OneDriveUpdaterDaemon.plist           com.paragon-software.installer-extfs.plist
com.avast.update.plist                              com.microsoft.autoupdate.helper.plist               com.paragon-software.ntfs.loader.plist
com.google.GoogleUpdater.wake.system.plist          com.microsoft.office.licensingV2.helper.plist       com.paragon-software.ntfsd.plist

/Users/phunsanit/Library/LaunchAgents:
com.google.GoogleUpdater.wake.plist  com.google.keystone.agent.plist      com.google.keystone.xpcservice.plist homebrew.mxcl.ollama.plist

วิธีจัดการขั้นสูง

หลังจากได้รายชื่อไฟล์ .plist มาแล้ว หากต้องการตรวจสอบสถานะการทำงานจริง (ว่ากำลังรันอยู่หรือไม่) สามารถใช้คำสั่ง launchctl ร่วมด้วย

  • ดูรายการทั้งหมดที่ระบบโหลดไว้
    launchctl list
  • หยุดและยกเลิกการโหลดสคริปต์
    launchctl unload /path/to/file.plist
  • โหลดสคริปต์กลับเข้ามาใหม่
    launchctl load /path/to/file.plist

การตรวจสอบรายการเหล่านี้เป็นประจำจะช่วยให้คุณควบคุมความปลอดภัยและประสิทธิภาพของเครื่อง Mac ได้ดียิ่งขึ้น