How do I launch this .plist file on startup *before* I log in?

6,151

Solution 1

You don't even need launchctl load -w. If you save this plist as /Library/LaunchAgents/test.plist and it's owned by root, it should be loaded automatically on login:

<?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>test</string>
    <key>ProgramArguments</key>
    <array>
        <string>say</string>
        <string>a</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

When I tried saving another plist like that in the user library, the say commands overlapped though.

If LimitLoadToSessionType is set to LoginWindow, the program is run when the graphical login window is shown:

<key>LimitLoadToSessionType</key>
<string>LoginWindow</string>

It is not run at all if automatic login is enabled though.

Solution 2

Everything that is stored in ~ (your home folders) starts up or loads as soon as you log into your user. So if u want to start something at boot, you might want to have a look at the root library, so /Library/LauchAgents for your launchagents for example.

Share:
6,151

Related videos on Youtube

hobbes3
Author by

hobbes3

Updated on September 18, 2022

Comments

  • hobbes3
    hobbes3 over 1 year

    I copied the current full .plist file at the bottom of this question.

    Right now I have a .plist file that I loaded with launchctl -w load ~/Library/LaunchAgents/local.setgetscreenres.plist to make my OS X run at 2880x1800 (retina) resolution on start-up and this works fine.

    However, I don't really know how to control the order of .plist files in ~/Library/LaunchAgents and sometimes it will take up to a minute before the setgetscreenres command runs after logging in. Therefore I would like to run this command before I log in, like how iStat does.

    I understand that I need to move the .plist. file to /Library/LaunchAgents, chown root:wheel, and run sudo launchctl -w load, but that didn't work. Now I'm thinking there is something wrong/missing in my .plist XML.

    Is there something I need to add extra in order to run as root or launch from /Library/LaunchAgents?

    <?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>local.setgetscreenres</string>
        <key>ProgramArguments</key>
        <array>
            <string>/Users/hobbes3/Code/setgetscreenres</string>
            <string>2880</string>
            <string>1800</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>KeepAlive</key>
        <false/>
        <key>AbandonProcessGroup</key>
        <true/>
    </dict>
    </plist>
    
  • hobbes3
    hobbes3 about 11 years
    I put that .plist in /Library/LaunchAgents and the command didn't run on bootup.
  • hobbes3
    hobbes3 about 11 years
    That worked but I ended up not running the .plist as root because as soon as I log in, Mac resize the resolution to the "Scaled: Most space" setting on the user. Is there an option to make the user .plist load before all other .plist?
  • hobbes3
    hobbes3 about 11 years
    I think I got it with <key>Nice</key><integer>-20</integer>.