经常使用的一个开发软件需要注册。网上找了一个注册证书服务程序,这个程序启动后会提供一个注册服务。软件启动的时候,提供这个服务监听的本地http端口,就能自动通过注册验证。但是这个注册证书服务程序是个命令行程序,每次手动启动这个程序实在太麻烦,我就想把这个程序做成一个后台服务,这样就节省了每次手动操作的时间。在macOS可以通过launchd
启动后台服务,关于launchd
,wiki2上介绍如下:
In computing, launchd, a unified service-management framework, starts, stops and manages daemons, applications, processes, and scripts.
There are two main programs in the launchd system: launchd and launchctl.
launchd manages the daemons at both a system and user level. Similar to xinetd, launchd can start daemons on demand. Similar to watchdogd, launchd can monitor daemons to make sure that they keep running. launchd also has replaced init as PID 1 on macOS and as a result it is responsible for starting the system at boot time.
Configuration files define the parameters of services run by launchd. Stored in the LaunchAgents and LaunchDaemons subdirectories of the Library folders, the property list-based files have approximately thirty different keys that can be set. launchd itself has no knowledge of these configuration files or any ability to read them - that is the responsibility of “launchctl”.
launchctl is a command line application which talks to launchd using IPC and knows how to parse the property list files used to describe launchd jobs, serializing them using a specialized dictionary protocol that launchd understands. launchctl can be used to load and unload daemons, start and stop launchd controlled jobs, get system utilization statistics for launchd and its child processes, and set environment settings.
具体操作步骤如下:
在/Users/{your_username}/Library/LaunchAgents
下创建com.myutils.licsrv.plist
文件
1 | <?xml version="1.0" encoding="UTF-8"?> |
将注册证书服务程序拷贝到/Users/{your_username}/Applications/myutils
目录下。
1 | > launchctl load com.myutils.licsrv.plist |
然后重启再测试一下,这样一个后台服务就创建好了。