Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Launch ION in single command & enable autostart on boot #211

Open
thobson88 opened this issue Oct 26, 2024 · 5 comments
Open

Launch ION in single command & enable autostart on boot #211

thobson88 opened this issue Oct 26, 2024 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@thobson88
Copy link
Collaborator

Similar to this go-ipfs install.sh script that writes a launchd .plist file to ~/Library/LaunchAgents.

But our script will start ION, not just the IPFS daemon.

On macOS the service config file will be ~/Library/LaunchAgents/foundation.identity.ion.plist. On Linux it will be /etc/systemd/system/ion.service.

Then we can either start ION automatically on boot/login, or with a single command (on macOS):

launchctl load ~/Library/LaunchAgents/foundation.identity.ion.plist

or (on Linux):

sudo systemctl enable ion.service
@thobson88 thobson88 self-assigned this Oct 26, 2024
@thobson88 thobson88 added the enhancement New feature or request label Oct 26, 2024
@thobson88
Copy link
Collaborator Author

thobson88 commented Oct 26, 2024

Launchd notes (macOS)

Launchd docs: https://www.launchd.info/

See also: https://gist.github.com/johndturn/09a5c055e6a56ab61212204607940fa0

  1. These lines are essential:
    <key>KeepAlive</key>
    <true/>

Without them the launchctl load command will do nothing and report no error.

  1. If you get this:
Load failed: 5: Input/output error
Try running `launchctl bootstrap` as root for richer errors.

when running launchctl load, the service may already be running. If so the fix is to first run:

launchctl unload ~/Library/LaunchAgents/foo.bar.plist

and then run launchctl load again.

  1. Brew commands do not work in scripts called via launchd: brew: command not found. Instead of, e.g.
brew services start mongodb-community

use:

launchctl load $HOME/Library/LaunchAgents/homebrew.mxcl.mongodb-community.plist
  1. Dictionaries in a plist file look list this:
<key>EnvironmentVariables</key>
<dict>
  <key>IPFS_LAUNCHD</key>
  <string>{{IPFS_LAUNCHD}}</string>
  <key>MONGODB_LAUNCHD</key>
  <string>{{MONGODB_LAUNCHD}}</string>
</dict>

Note that the EnvironmentVariables must contain only one dictionary (with as many key-value pairs as needed).

  1. Bash scripts are not runnable via launchd in macOS until /bin/bash has been given Full Disk Access permission. To do this, open Security & Privacy settings, choose 'Full Disk Access', click the lock to make changes and enter your password. Then click the + button. Then hit cmd+shift+G, type /bin/bash and hit Enter. See this post on SO.

  2. To see what launchd processes are running, use launchctl list and pipe to grep, e.g.:

launchctl list | grep "ion.core"

and to stop one, use launchctl unload e.g.

launchctl unload ~/Library/LaunchAgents/foundation.identity.ion.core.plist
  1. All user agents in ~/Library/LaunchAgents are automatically run at user login.

  2. When running node.js scripts with launchd on macOS, the PATH env variable must be set in the .plist file (as launchd executes with a minimal PATH). See this SO post.

  3. If RunAtLoad is not set to true (in the .plist file) the agent will not run automatically when the user logs in. Presumably this command should be used instead (not yet tested):

launchctl start  ~/Library/LaunchAgents/foundation.identity.ion.plist

@thobson88
Copy link
Collaborator Author

thobson88 commented Oct 26, 2024

To check for the correct OS (in the install script):

os=$(uname)
if [ "$os" = "Linux" ]; then  
    echo "This is Linux"
elif [ "$os" = "Darwin" ]; then  
    echo "This is macOS"
fi

@thobson88
Copy link
Collaborator Author

thobson88 commented Oct 28, 2024

In progress on branch 211-launch-ion.

This is now working on macOS. After installing ION, run the install script:

./scripts/launchd/autostart_ion.sh

and then load the agent (this only needs to be done once):

launchctl unload ~/Library/LaunchAgents/foundation.identity.ion.plist

Then all ION services will start automatically on user login. 🚀

Next step: do the same for Linux.

@thobson88
Copy link
Collaborator Author

TODO: only write the .plist file if the contents are different to an existing one, and make a backup of any different .plist that already exists in a hidden .bkp folder inside ~/Library/LaunchAgents.

@thobson88
Copy link
Collaborator Author

TODO:

  • in autostart.sh, check that the expected binaries exist and otherwise try to discover them with, e.g., IPFS_BIN=$(which ipfs || echo ipfs)
  • for Linux, add a mongod.service template to be installed in ~/.config/systemd/user to create a user service (that can be started without sudo)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant