Knut Harald Støre wrote:
I have tried using launchctl to make it start automatically, but it’s not working.
...
How can I make this work from crontab?
...
How can I make this work inside an Applescript?
First of all, you're going to have to pick one method and stick with it. All of these are going to be difficult for someone who's never done this before. All of these are radically different approaches.
I just followed someone else’s instructions online. I contacted him about this, but he couldn’t figure out why it didn’t work on my system.
Please never do that. As difficult as it might be to help someone figure something out here in the forums, we can't also try to debug some other random instructions from the internet, along with instructions on how to undo the damage caused by said random internet instructions. I don't care what you've heard, or what people on the internet have told you, the internet is wrong.
If I assume that anything you've tried thus far hasn't irreparably damaged your system, here is what you need to do.
Note that, at a bare minimum, the task you've been given by the developers of this zigbee2mqtt software is very difficult. It's built upon decades of software and system admin practices on Linux that aren't even good practices on Linux. Now you're trying to port all of that to a Mac. This is a difficult task even for people very experienced. They usually fail.
First of all, you want to use launchctl. This isn't necessarily the easiest solution. Your solution is already difficult. This is simply the solution that requires the least amount of additional expertise on top of what you already have to do.
Write a little script that sets up your environment and runs the script. When you run Terminal, your environment has already been setup. The launchctl environment is different. The easiest way to bridge that gap is to have launchctl directly run a shell environment in much the same way that Terminal does. Hopefully this will be enough.
So you want a zsh script that runs the following:
cd /Applications/zigbee2mqtt/
npm start
So create a file named zigbee2mqtt.sh with the contents of the above file. Put the file in a convenient, dedicated location such as: $HOME/Library/Scripts. You may have to create this directory. You could put it directly in your home directory too, but there is a chance it could get accidentally removed. The hidden Library folder is designed to be less likely to be damaged like this.
You'll also want to correct those commands to be more shell-script friendly. Change "npm" that you specify the full path to npm. I have no idea what that path should be on your computer. It all depends on how you installed it. This way, your script is independent of whatever path you have set at the time. This doesn't guarantee the script will survive accidents, but it's good practice.
Try to manually run your script by doing "sh /Users/<your short user name>/Library/Scripts/zigbee2mqtt.sh". If that works, then go to the next step. (Replace "<your short user name>" with what that is.)
Create a launchd plist file named something like "org.zigbee2mqtt.plist". Do not create the file inside Library/LaunchAgents. Create it somewhere else and move it into that folder only when it is ready to go.
The code should look something like this:
<?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.zigbee2mqtt</string>
<key>ProgramArguments</key>
<array>
<string>/bin/sh</string>
<string>/Users/<your short user name>/Library/Scripts/zigbee2mqtt.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
again, replace "<your short user name>" with what that is.
Please note that this is extremely delicate and difficult to test. I don't want to log out of my system, so I tested it with:
<?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.zigbee2mqtt</string>
<key>ProgramArguments</key>
<array>
<string>/bin/zsh</string>
<string>/Users/jdaniel/Library/Scripts/zigbee2mqtt.sh</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>9</integer>
<key>Minute</key>
<integer>33</integer>
</dict>
</dict>
</plist>
Where my zigbee2mqtt.sh consists of:
date >> /tmp/mydate.txt
Your script is significantly more difficult. I strongly recommend trying something simple like this until you get it working. Run:
launchctl bootstrap gui/502 $HOME/Library/LaunchAgents/zigbee2mqtt.plist
to load it and check /tmp/mydate.txt
and then run:
launchctl bootout gui/502/org.zigbee2mqtt
to unload it, make some fixes, and try again.