How to run system service before login in macOS 12.5.1?

195 Views Asked by At

I put the plist file in /Library/LaunchDaemons directory, chown to root and run sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.nginx.plist to enable and load it.

zyl@imac:~% ls -l  /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
-rw-r--r--  1 root  wheel  906  6  4 21:20 /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

Then I reboot, waiting at login screen for some minutes. Before I login with a user/password, the service does not run.

After I login and check the create time of /Users/xxxxx/stderr.log, it shows the file was created after login.

The Os is macOs 12.5.1 running at apple-m1. I don't know how to make my service running without login. Anyone knows please help.

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
     <key>Label</key> 
     <string>homebrew.mxcl.nginx</string> 
<!-- 
     <key>LimitLoadToSessionType</key> 
     <array> 
         <string>Aqua</string> 
         <string>Background</string> 
         <string>LoginWindow</string> 
         <string>StandardIO</string> 
         <string>System</string> 
     </array> 
--> 
     <key>UserName</key> 
     <string>root</string> 
     <key>GroupName</key>
     <string>wheel</string>
     <key>StandardErrorPath</key> 
     <string>/Users/xxxxx/stderr.log</string> 
     <key>StandardOutPath</key> 
     <string>/Users/xxxxx/stdout.log</string> 
  
     <key>ProgramArguments</key> 
     <array> 
         <string>/usr/local/opt/nginx/bin/nginx</string> 
         <string>-g</string> 
         <string>daemon off;</string> 
     </array> 
     <key>RunAtLoad</key> 
     <true/> 
     <key>WorkingDirectory</key> 
     <string>/usr/local</string> 
     <key>KeepAlive</key> 
     <true/> 
</dict> 
</plist> 
  
1

There are 1 best solutions below

3
Philippe On

With this /Library/LaunchDaemons/machine-test.plist

<?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>machine-test</string>
        <key>ProgramArguments</key>
        <array>
                <string>/bin/bash</string>
                <string>-c</string>
                <string>ps -ef > /tmp/ps.log</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
</dict>
</plist>

when I reboot Ventura, /tmp/ps.log contains only system processes, not my (UID 501) processes.