This section describes how to manage macOS services and provides examples.

Link: medium.com/swlh/how-to…

Author: Medium.com/@kosalasana…

Published: May 30, 2019-5 minutes reading

In computing, Launchd is a unified operating system services management framework for starting, stopping, and managing daemons, applications, processes, and scripts in macOS. It was introduced with Mac OS X Tiger and is licensed under the Apache license. – Wikipedia

If you’re familiar with any version of Linux, you’ve certainly dealt with cron assignments. Basically, Launchd is the Cron of macOS. Launchd can do a lot more than execute cron-style scripts. Like Systemd for Linux, Launchd is an alternative to many older Unix tools such as Init, Inetd, cron, etc.

At its core, Launchd distinguishes between daemons and agents. Daemons are system-wide services that always run in the background, while agents describe general services that are executed at user-specific events. Daemons and agents are managed by the Launchd framework and can be controlled using the Launchctl terminal command.

There are two main components to the launchd service creation process that you need to follow.

  1. Task definition. Services are defined in special XML files.
  2. Operation. Use a command line tool to control the service.

📋 Working Definition

The behavior of the daemon or agent is defined in a special XML file called a property list (.plist) file. Depending on where it is stored, it will be treated as a daemon or agent. Services (daemons and agents) are saved as.plist files, which can be stored in the following folders according to your request.

Photo credit: launchd.info

Launchd supports more than 36 different configuration keys in the property list file, with only the primary keys required for basic services described below. (From Launchd. plist(5) Manual)

Label <string> This required key uniquely identifies the job to launchd.Program <string> This key defines what to start.  If this key is missing, then the first element of the array of strings provided to the ProgramArguments will be used instead. This key is required in the absence of the ProgramArguments key.ProgramArguments <array of strings> Use this one if your executable requires command line options. This key is required in the absence of the Program key.RunAtLoad <boolean> This optional key is used to control whether your job is launched once at the time the job is loaded. The default is false.StartInterval <integer> This optional key causes the job to be started every N seconds. If the system is asleep, the job will be started the next time the computer wakes up. If multiple intervals transpire before the computer is woken, those events will be coalesced into one event upon wake from sleep.StartCalendarInterval <dictionary of integers or array of dictionary of integers> This optional key causes the job to be started every calendar interval as specified. Missing arguments are considered to be wildcard. The semantics are much like crontab(5) . Minute <integer> The minute on  which this job will be run. Hour <integer> The hour on which this job will be run. Day <integer> The day on which this job will be run. Weekday <integer> The weekday on which this job will be run (0 and 7 are Sunday). Month <integer> The month on which this job will be run.Copy the code

Refer to the launchd.plist(5) manual or launchd.info for detailed definitions of other keys in the property list file.

⚠️ Please note: the name of the property list file should end with “.plist “. Also, note that the launchd property list file is named

⚙ operation

This section defines how to get information about the Launchd service and how to load, unload, start, and stop the jobs. All of this can be done with the launchctl command line tool.

Launchctl interconnects with launchd to load and unload daemons/agents and generally control launchd. Launchctl supports receiving subcommands on the command line, interactively, or even from standard input redirection. – launchctl (1) manual

Some of the common commands in launchctl are defined below.

  • Get information about available (load) jobs:
$ launchctl list
Copy the code
  • Gets information about a job.
$ launchctl list | grep <LABEL>
Copy the code
  • Loads a job (global daemon).
$ launchctl load /Library/LaunchDaemons/<LABEL>.plist
Copy the code
  • Unmount a job (a global daemon).
$ launchctl unload /Library/LaunchDaemons/<LABEL>.plist
Copy the code
  • Start a job (a loaded job).
$ launchctl start <LABEL>
Copy the code
  • Stop a work (work that has been loaded).
$ launchctl stop <LABEL>
Copy the code
  • Restart a job (a loaded job).
$ launchctl restart <LABEL>
Copy the code

⚠️ Please note: if you are using Daemon jobs, use sudo permission in the above commands.

🎗 Simple example (using the WSO2 API manager

In this article, I want to run a WSO2 API Manager server as a service on a macOS system as an example to understand the methods explained above. WSO2 API Manager addresses The complete API lifecycle management, monetization, and policy enforcement issues and has been named a leader in The Forrester Wave™. API Management Solutions, named leader in the q4 2018 report.

  1. Download the latest WSO2 API Manager release from the WSO2 official website.
  2. Extract the API Manager compression package and go to the WSO2 API Manager directory. (We’ll call this directory’s path )
  3. Download and install OpenJDK 8 or Oracle JDK 1.8.* and set the JAVA_HOME environment variable.
  4. Create a properties file named “org.wso2.am.plist “in “/Library/LaunchDaemons/”. Open the “org.wso2.am.plist “file with a text editor and copy the following to the property list (.plist) file. Replace with the path of the product directory.

      
<! DOCTYPEplist 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.wso2.am</string>
    <key>ServiceDescription</key>
    <string>WSO2 API Manager Server</string>
    <key>ProgramArguments</key>
    <array>             
        <string><PRODUCT_HOME>/bin/wso2server.sh</string>
    </array>
    <key>RunAtLoad</key>
    <false/>
</dict>
</plist>
Copy the code
  1. Run the following command to load and start the WSO2 API Manager as a service using the launchctl tool.
$ sudo launchctl load /Library/LaunchDaemons/org.wso2.am.plist
$ sudo launchctl start org.wso2.am
Copy the code
  1. To check the status of the job, run the following command and you will be able to observe the required information about the job given in the image.
$ sudo launchctl list | grep org.wso2.am
Copy the code

WSO2 API Manager runs as a service with PID 27787

  1. Run the following commands to stop the running process and uninstall the daemon service from the Launchd framework.
$ sudo launchctl stop org.wso2.am
$ sudo launchctl unload /Library/LaunchDaemons/org.wso2.am.plist
Copy the code

So, with an understanding of a simple example of the Launchd framework, you can now use Launchd to create scripts that do things like clean files, control the application server on schedule, or run an application when a file is present.

Cheers! ! 🍺 🍺

More details on the Launchd framework and the Launchctl tool.

www.launchd.info

www.manpagez.com/man/5/launc…

www.manpagez.com/man/1/launc…


www.deepl.com translation