Skip to content

Instantly share code, notes, and snippets.

@shubhwip
Last active April 15, 2025 18:54
Show Gist options
  • Save shubhwip/d47b685ce1e5f67f6367d49a5aa578b0 to your computer and use it in GitHub Desktop.
Save shubhwip/d47b685ce1e5f67f6367d49a5aa578b0 to your computer and use it in GitHub Desktop.
This gist describe how to create and run launchctl service configuration for macOS. This tutorial shows how to configure a java service as a launchd service on macOS and do start, stop, status and automatic restart at the reboot using launchctl new APIs.
### Launching Services In macOS(Root User)
Step 1 : Create demo.plist in /Library/LaunchAgents/
Step 2 : Contents of demo.plist
In this demo, I am running a java application.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>k2agent</string>
<key>ProgramArguments</key>
<array>
<string>{{ JAVA_ABSOLUTE_LOCATION }}</string>
<string>-jar</string>
<string>{{ JAR_LOCATION }}</string>
</array>
<key>RunAtLoad</key>
<true/> <!-- run the program at login -->
<key>KeepAlive</key>
<true/> <!-- run the program again if it terminates -->
<key>WorkingDirectory</key>
<string>{{ INSTALL_DIRECTORY }}</string>
<key>StandardErrorPath</key>
<string>{{ INSTALL_DIRECTORY }}/logs/agent.err</string>
<key>StandardOutPath</key>
<string>{{ INSTALL_DIRECTORY }}/logs/agent.out</string>
</dict>
</plist>
Example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>siem</string>
<key>ProgramArguments</key>
<array>
<string>java</string>
<string>-jar</string>
<string>/Users/ec2-user/demo.jar</string>
</array>
<key>RunAtLoad</key>
<true/> <!-- run the program at login -->
<key>KeepAlive</key>
<true/> <!-- run the program again if it terminates -->
<key>WorkingDirectory</key>
<string>/Users/ec2-user</string>
<key>StandardErrorPath</key>
<string>/tmp/mycommand.err</string>
<key>StandardOutPath</key>
<string>/tmp/mycommand.out</string>
</dict>
</plist>
Step 3 : Starting the service using launchctl
launchctl enable system/Library/LaunchAgents/${SERVICE_NAME}.plist
launchctl bootstrap system /Library/LaunchAgents/${SERVICE_NAME}.plist
Step 4: Checking Service Status
launchctl list | grep ${SERVICE_NAME} | wc -l
launchctl print system/${SERVICE_NAME}
Step 5: Stopping a service
launchctl disable system/Library/LaunchAgents/${SERVICE_NAME}.plist
launchctl bootout system /Library/LaunchAgents/${SERVICE_NAME}.plist
Step 6: Restarting Launchd Service
launchctl kickstart -kp system/${SERVICE_NAME}
Step 7: Removing a launch daemon service[Cleanup]
launchctl remove ${SERVICE_NAME}
rm -rf /Library/LaunchAgents/${SERVICE_NAME}.plist
Note: In our case ${SERVICE_NAME} is demo
### Launching Services In macOS(Non-Root User)
Step 1 : Create demo.plist in /Users/${YOUR_USER_NAME}/Library/LaunchAgents/
Step 2 : Contents of demo.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>demo</string>
<key>ProgramArguments</key>
<array>
<string>{{ JAVA_ABSOLUTE_LOCATION }}</string>
<string>-jar</string>
<string>{{ JAR_ABSOLUTE_LOCATION }}</string>
</array>
<key>RunAtLoad</key>
<true/> <!-- run the program at login -->
<key>KeepAlive</key>
<true/> <!-- run the program again if it terminates -->
<key>WorkingDirectory</key>
<string>{{ INSTALLATION_PATH }}</string>
<key>StandardErrorPath></key>
<string>{{ INSTALLATION_PATH }}/logs/agent_error.log</string>
<key>StandardOutPath></key>
<string>{{ INSTALLATION_PATH }}/logs/agent_out.log</string>
</dict>
</plist>
Example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>siem</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/java</string>
<string>-jar</string>
<string>/Users/ec2-user/demo.jar</string>
</array>
<key>RunAtLoad</key>
<true/> <!-- run the program at login -->
<key>KeepAlive</key>
<true/> <!-- run the program again if it terminates -->
<key>WorkingDirectory</key>
<string>/Users/ec2-user</string>
<key>StandardErrorPath></key>
<string>/Users/ec2-user/err.log</string>
<key>StandardOutPath></key>
<string>/Users/ec2-user/out.log</string>
</dict>
</plist>
Step 3: Starting the service using launchctl first time
launchctl enable gui/${USER_ID}/${PLIST_FILE_LOCATION}
launchctl bootstrap gui/${USER_ID} ${PLIST_FILE_LOCATION}
Example:
launchctl enable gui/501/~/Library/LaunchAgents/demo.plist
launchctl bootstrap gui/501 ~/Library/LaunchAgents/demo.plist
Step 4: Checking Service Status
launchctl print gui/501/{{ SERVICE_NAME }}
Example:
launchctl print gui/501/demo
Step 5: Stopping a service
launchctl disable gui/${USER_ID}/${PLIST_FILE_LOCATION}
launchctl bootout gui/${USER_ID} ${PLIST_FILE_LOCATION}
Example:
launchctl disable gui/501/~/Library/LaunchAgents/demo.plist
launchctl bootout gui/501 ~/Library/LaunchAgents/demo.plist
Step 6: Restarting Launchd Service
launchctl kickstart -kp gui/501/demo
Step 7: Removing a launch daemon service[Cleanup]
Run Step 5 Commands
rm -rf ~/Library/LaunchAgents/demo.plist
Notes :
1. It is good to add absolute location of the executable like /bin/bash, /usr/bin/java etc.
2. Make sure that location specified in StandardErrorPath and StandardOutPath must have access given to user who is managing these services.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment