MacOSX啟動項


Introduction & Overview

  1. 或許你會對MacOS啟動的哪些程序感到好奇
  2. MacOS啟動的時間貌似變長了些許
  3. 為什么有些來歷不明的程序總是在左右著你

Points

  • OSX : Powered up > Firmware In Complete Control(Hardware) > BootX Loader(Kernel) > BSD Subsystem > Aqua User Interface

  • BootX: Located in /System/Library/CoreServices > Loads Kernel Extensions (drivers, also known as kexts:/System/Library/Extensions)

  • Initialization:Data Structures > I/O Kit > Mounts the root filesystem > loads mach_init > Launches the BSD init process >

  • The init process launches:

    • shell scripts to start the system /etc/rc.boot and /etc/rc
    • sets the initial environment /etc/rc.common
    • load usefull functions/etc/hostconfig
  • /etc/rc enables the swap file, sets the language for the system, and hands off control to /sbin/SystemStarter.

  • SystemStarter:

    • examines /System/Library/StartupItems and /Library/StartupItems for applications that should be started at boot time.
  • Returned to init, which launchesgetty, the console entry launches Login Window (/System/Library/CoreServices/loginwindow.app).

  • Run Services in macOS

    • launchd is the cron in macOS
    • launchd distinguishes daemons and agents.
  • Two main components in the launchd service

    • Job Definition : Define the service in special XML file.
    • Operations : Control the service using command line utility.
  • Job Definition

    • The behavior of a daemons or agents are defined in a special XML file called a property list (*.plist) file.
  • Type Location Run On Be Half Of
    User Agents ~/Library/LaunchAgents Current Logined In User
    Global Agents /Library/LaunchAgents Current Logined In User
    Global Daemons /Library/LaunchDaemons Root Or the User Specify with Key Username
    System Agents /System/Library/LaunchAgents Current Logined In User
    System Daemons /System/Library/LaunchDaemons Root Or the User Specify with Key Username

    Operations.

    Getting information about available (loaded) jobs :

    $ launchctl list
    

    Getting information about a given job :

    $ launchctl list | grep <LABEL>
    

    Loading/Unloading a job (a global daemon) :

    $ launchctl load/unload /Library/LaunchDaemons/<LABEL>.plist
    

    Starting/Stop/Restart a job (a loaded job) :

    $ launchctl start/stop/restart <LABEL>
    

Simple Example

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
    "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>org.wso2.am</string>
    <key>ServiceDescription</key>
    <string>WSO2 API Manager Server</string>
    <key>ProgramArguments</key>
    <array>             
        <string>/bin/wso2server.sh</string>
    </array>
    <key>RunAtLoad</key>
    <false/>
</dict>
</plist>

Load & Run:

$ sudo launchctl load /Library/LaunchDaemons/org.wso2.am.plist
$ sudo launchctl start org.wso2.am

Test & Taste

$ sudo launchctl list | grep org.wso2.am

Bonus

#!/bin/bash

mkdir ~/.startup
# mac os startup item place , <sudo launchctl list> is ok also. 
ln -s /System/Library/StartupItems ~/.startup/sysStartupItems
ln -s /Library/StartupItems ~/.startup/libStartupItems
ln -s ~/Library/LaunchAgents ~/.startup/userAgents
ln -s /Library/LaunchAgents ~/.startup/libAgents
ln -s /Library/LaunchDaemons ~/.startup/libDaemons
ln -s /System/Library/LaunchDaemons ~/.startup/sysDaemons
ln -s /System/Library/LaunchAgents ~/.startup/sysAgents

function searchstartup(){
        grep -lrn $1 ~/.startup
}

Ref

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容