diff --git a/_posts/2022-03-19-get-application-launch-at-macos-startup.md b/_posts/2022-03-19-get-application-launch-at-macos-startup.md new file mode 100644 index 0000000..ad21604 --- /dev/null +++ b/_posts/2022-03-19-get-application-launch-at-macos-startup.md @@ -0,0 +1,70 @@ +--- +layout: post +title: "How to Get Applications to Launch at System Startup on macOS" +date: 2022-03-19 23:32:00 +0800 +categories: note macos +--- + +macOS provides a task management framework called **launchd**. You can create tasks by placing XML files in a specific directory. **launchd** will run the task at a specified time according to the configuration in the XML. It is suitable for automating customized tasks, such as starting a web server at startup, backing up files regularly, etc. + +This is an example of starting a *Rails on Ruby* service at boot time. + +**Preparation** + +At first, determine the command to be run. In my case, the command to start *Rails on Ruby* is ` +rails server --environment=development --binding=0.0.0.0 --port=4000 --using=puma`, and the work directory is `/Users/xyw/git/Today`. + +**Create a Service** + +Create a **plist** file in `~/Library/LaunchAgents/` directory. This is my plist file to start rails: + +```xml + + + Label + com.wxyucs.today + KeepAlive + + RunAtLoad + + WorkingDirectory + /Users/xyw/git/Today + ProgramArguments + + /Users/xyw/.rbenv/shims/rails + server + --environment=development + --binding=0.0.0.0 + --port=4000 + --using=puma + + StandardErrorPath + /tmp/today.err + StandardOutPath + /tmp/today.out + + +``` + + + +**Enable the Service** + +Now, enable the service: + +```bash +$ launchctl load ~/Library/LaunchAgents/com.example.app.plist +$ launchctl start com.example.app +``` + +**Disable the Service** + +If you no longer need this service at startup, you can disable with this command: + +```bash +$ launchctl stop com.example.app +``` + +**Reference** + +- https://www.launchd.info/