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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。