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 

    Loading/Unloading a job (a global daemon) :

    $ launchctl load/unload /Library/LaunchDaemons/

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

    $ launchctl start/stop/restart 

Simple Example





    Label
    org.wso2.am
    ServiceDescription
    WSO2 API Manager Server
    ProgramArguments
                 
        /bin/wso2server.sh
    
    RunAtLoad
    


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 ,  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

  • Booting Mac OS X
  • Run Services In Mac OS X

你可能感兴趣的:(MacOSX启动项)