pkgxdev / pkgx

run anything
https://pkgx.sh
Apache License 2.0
9.03k stars 1.36k forks source link

pkgx doesn't like to be called as a launchdaemon #894

Open magnusviri opened 11 months ago

magnusviri commented 11 months ago

I tried to daemonize stable diffusion and I get an error. Here's the launchd plist file.

<?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>stable-diffusion-webui</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/pkgx</string>
        <string>stable-diffusion-webui</string>
        <string>--api</string>
        <string>--listen</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>StandardOutPath</key>
    <string>/var/root/.local/share/stable-diffusion-webui/log.out</string>
    <key>StandardErrorPath</key>
    <string>/var/root/.local/share/stable-diffusion-webui/log.err</string>
</dict>
</plist>

This is how I load it: launchctl load /path/to/launchd-file.plist

This is the error (/var/root/.local/share/stable-diffusion-webui/log.err).

error: Uncaught Error: invalid absolute path: undefined
      throw new Error(`invalid absolute path: ${input}`);
            ^
    at new Path (https://deno.land/x/libpkgx@v0.15.0/src/utils/Path.ts:41:13)
    at Function.home (https://deno.land/x/libpkgx@v0.15.0/src/utils/Path.ts:24:12)
    at ConfigDefault (https://deno.land/x/libpkgx@v0.15.0/src/hooks/useConfig.ts:13:77)
    at useConfig (https://deno.land/x/libpkgx@v0.15.0/src/hooks/useConfig.ts:57:34)
    at usePantry (https://deno.land/x/libpkgx@v0.15.0/src/hooks/usePantry.ts:35:18)
    at file:///Users/builder/actions-runner/_work/pkgx/pkgx/src/prefab/install.ts:29:12

I haven't done anything to the pkgx install or to the stable diffusion package (except I did install it all as root).

If I instead run pkgx install stable-diffusion-webui and then try to run /usr/local/bin/stable-diffusion-webui then this is the error I get:

/usr/local/bin/stable-diffusion-webui: line 3: exec: pkgx: not found

I'm guessing the problem is that in the launchd environment /usr/local/bin/ isn't in the path.

michaelessiet commented 11 months ago

Have you tried installing it then running it as a normal user(not root)? Also yes, /usr/local/bin might need to be added to the path

mxcl commented 10 months ago

The first issue is a bug and I cannot RN imagine what the problem might be. Even though I wouldn't recommend running something as insane as stable diffusion web UI as root, pkgx should at least succeed.

The second issue is indeed that on macOS /usr/local/bin is not in the PATH apart from in shell sessions. Our shims expect pkgx to be in the PATH which I think is an acceptable requirement. This is standard for such tools.

mxcl commented 10 months ago
$ sudo -i
$ pkgx stable-diffusion-webui --api --listen

works. This runs sd-webui as root, also installing to /var/root/.pkgx

guess I better try the launch plist…

magnusviri commented 10 months ago

I was running this as root to try to get it to work. After getting it to work, I would've switched it to a daemon user. I was using ansible to setup multiple computers as AI render nodes (not sure that's the right name for that) and I got it to work by running nohup /usr/local/bin/pkgx stable-diffusion-webui --api --listen in the ansible script to start the process. Ansible uses ssh, so the ssh process on the nodes were able to run the command. The environment for launchdaemons was different enough that it didn't work.

I didn't think of testing this back then, but the way I've debugged launchd environments before was by setting up a reverse shell and then telnetting in and testing. I save the following script and call it with the launchdaemon.

#!/bin/zsh

set -u

if [ ! -e /tmp/lol ]; then
    mkfifo /tmp/lol;
fi

nc -l 8080 < /tmp/lol | /bin/bash -i > /tmp/lol 2>&1

rm /tmp/lol
mxcl commented 10 months ago

explanation of script: https://chat.openai.com/share/6805b199-a9e5-421d-bbd7-5f4b8b3c59bb

This script is essentially setting up a reverse shell. When someone connects to the server's port 8080, they are given access to an interactive Bash shell. The use of a named pipe allows for both input and output to be handled through the same Netcat connection.

magnusviri commented 10 months ago

Yeah, but if you're the first person to connect, then nobody else can. It's how I debug launchd because there's no other good way! It's a strange environment.