mac launchctl 守护进程

1,450 阅读2分钟

需求

我们有一些场景需要服务器,比如一台mac重启后自动启动某些应用,比如自重启服务器数据库,此时,我们可以用以下办法:

  1. pm2 重启
  2. launchctl 守护进程

pm2 重启

现在nodejs很多应用是用pm2部署的,pm2自带能够自重启的功能

设置步骤

  1. 先用pm2启动应用
pm2 start ./dist/client/ --watch --ignore-watch="node_modules"

然后用pm2 status 查看该应用是否显示正常

pm2 status
  1. pm2 startup 在终端执行pm2 startup,会输出如下:
[PM2] Init System found: launchd
[PM2] To setup the Startup Script, copy/paste the following command:
sudo env PATH=$PATH:/usr/local/bin /usr/local/lib/node_modules/pm2/bin/pm2 startup launchd -u tuxiuluo --hp /Users/tuxiuluo
  1. 此时,按照提示,复制上一步骤出现的一行命令,在终端执行 在终端执行:
sudo env PATH=$PATH:/usr/local/bin /usr/local/lib/node_modules/pm2/bin/pm2 startup launchd -u tuxiuluo --hp /Users/tuxiuluo

如果正常,终端会输出:

[PM2] Init System found: launchd
Platform launchd
Template
<?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>com.PM2</string>
	  <key>UserName</key>
	  <string>tuxiuluo</string>
    <key>KeepAlive</key>
    <true/>
	  <key>ProgramArguments</key>
	  <array>
		  <string>/bin/sh</string>
		  <string>-c</string>
		  <string>/usr/local/lib/node_modules/pm2/bin/pm2 resurrect</string>
	  </array>
	  <key>RunAtLoad</key>
	  <true/>
	  <key>OnDemand</key>
	  <false/>
	  <key>LaunchOnlyOnce</key>
	  <true/>
	  <key>EnvironmentVariables</key>
    <dict>
      <key>PATH</key>
      <string>/usr/local/bin</string>
      <key>PM2_HOME</key>
      <string>/Users/tuxiuluo/.pm2</string>
    </dict>
	  <key>StandardErrorPath</key>
	  <string>/tmp/com.PM2.err</string>
	  <key>StandardOutPath</key>
	  <string>/tmp/com.PM2.out</string>
  </dict>
</plist>

Target path
/Users/tuxiuluo/Library/LaunchAgents/pm2.tuxiuluo.plist
Command list
[
  'launchctl load -w /Users/tuxiuluo/Library/LaunchAgents/pm2.tuxiuluo.plist'
]
[PM2] Writing init configuration in /Users/tuxiuluo/Library/LaunchAgents/pm2.tuxiuluo.plist
[PM2] Making script booting at startup...
>>> Executing launchctl load -w /Users/tuxiuluo/Library/LaunchAgents/pm2.tuxiuluo.plist
[DONE]
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save

[PM2] Remove init script via:
$ pm2 unstartup launchd
  1. 可以看到,其实,这是利用了launchctl设置了一个自启动的应用

设置的自启动的配置文件路径在/Users/tuxiuluo/Library/LaunchAgents/pm2.tuxiuluo.plist

launchctl 守护进程

launchctl定义

launchctl经常可以用来做定时任务,英文解释如下:

In computing, launchd, a unified operating system service management framework, starts, stops and manages daemons, applications, processes, and scripts in macOS. It was introduced with Mac OS X Tiger and is licensed under the Apache License.— Wikipedia

英文官网地址

核心是定义一系列的自启动应用配置文件

TypeLacationRun on behalf of
User Agents~/Library/LaunchAgentsCurrent logged in user
Global Agents/Library/LaunchAgentsCurrent logged in user
Global Daemons/Library/LaunchDaemonsroot or the user sepcified with the key UserName
System Agents/System/Library/LaunchAgentsCurrent logged in user
System Daemons/System/Library/LaunchDaemonsroot or the user sepcified with the key UserName

启动顺序如下:

  1. 电脑启动,在未登录时,系统会扫描/System/Library/LaunchDaemons/ and /Library/LaunchDaemons/里面的配置
  2. 用户登录进来时,系统会扫描/System/Library/LaunchAgents, /Library/LaunchAgents, 以及用户的目录:~/Library/LaunchAgents directory里面的配置

有第一小节我们可以看到,pm2的自启动放在了User Agents

launchctl配置步骤

接下来我们自己自制一个自启动应用,用来每次启动电脑打印一次ls -al ~

去~/Library/LaunchAgents新建一个配置文件,名字叫做com.startup.test.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>com.startup.test</string>
	  <key>UserName</key>
	  <string>tuxiuluo</string>
    <key>KeepAlive</key>
    <true/>
	  <key>ProgramArguments</key>
	  <array>
		  <string>/bin/sh</string>
		  <string>ls -al ~</string>
	  </array>
	  <key>RunAtLoad</key>
	  <true/>
	  <key>OnDemand</key>
	  <false/>
	  <key>LaunchOnlyOnce</key>
	  <true/>
	  <key>StandardErrorPath</key>
	  <string>/Users/tuxiuluo/com.startup.test.err</string>
	  <key>StandardOutPath</key>
	  <string>/Users/tuxiuluo/com.startup.test.out</string>
  </dict>
</plist>

接着输入命令

launchctl load -w /Users/tuxiuluo/Library/LaunchAgents/com.startup.test.plist

接着,查看已装载的列表:

launchctl list

接着,重启电脑去/Users/tuxiuluo/com.startup.test.out查看有没有正确的输出

遇到的坑

权限问题

经过试验,设置在/Library/LaunchAgents的plist文件,权限需要是:

root:wheel,而且可执行权限时600,不然的话,设置了会没反应

sudo chown root:wheel /Library/LaunchDaemons/myfile.plist
sudo chmod 600 /Library/LaunchDaemons/myfile.plist