rhyst / linak-controller

A Python script to control Linak standing desks.
MIT License
346 stars 51 forks source link

Is there a way to make the server auto-run and persist on macOS? #49

Closed jaydisc closed 2 years ago

jaydisc commented 2 years ago

Thanks for a very useful piece of software!

I'm trying to get idasen-controller --server to launch at login, and persist. The prescribed method of doing this is by using a launchd.plist, which I have installed into ~/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>com.example.idasen-controller</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/idasen-controller</string>
        <string>--server</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>StandardOutPath</key>
    <string>/Users/jay/Library/Logs/idasen-controller.log</string>
    <key>StandardErrorPath</key>
    <string>/Users/jay//Library/Logs/idasen-controller.log</string>
</dict>
</plist>

Unfortunately, this generates a SIGABRT when run by launchd, but running idasen-controller --server in Terminal works just fine.

I tried specifying all the different process types, by adding a directive like this:

<key>ProcessType</key>
<string>Background</string>

And cycling through the other possible values of adaptive and interactive to no avail.

Unfortunately, I'm not even getting anything in StdOut or StdErr.

launchd describes its expectations ( man launchd.plist) as follows:

     Daemons or agents managed by launchd are expected to behave certain ways.

     A daemon or agent launched by launchd MUST NOT do the following in the process directly launched by launchd:

           •   Call daemon(3).
           •   Do the moral equivalent of daemon(3) by calling fork(2) and have the parent process exit(3) or _exit(2).

     A launchd daemon or agent should not perform the following as part of its initialization, as launchd will always
     implicitly perform them on behalf of the process.

           •   Redirect stdio(3) to /dev/null.

     A launchd daemon or agent need not perform the following as part of its initialization, since launchd can perform them on
     the process' behalf with the appropriate launchd.plist keys specified.

           •   Setup the user ID or group ID.
           •   Setup the working directory.
           •   chroot(2)
           •   setsid(2)
           •   Close "stray" file descriptors.
           •   Setup resource limits with setrlimit(2).
           •   Setup priority with setpriority(2).

Does launching idasen-controller --server meet launchd's expectations?

jaydisc commented 2 years ago

Looks like I was adding the wrong binary to Security & Privacy → Bluetooth. I got a bit confused between Xcode and Homebrew versions of python.