命令行设置开机启动项
本文备注将一个启动脚本用命令行的方式加入到 macOS
开机启动项
-> 准备脚本
以 jenkins slave为例:
#!/bin/bash
cd /Users/admin/jenkins_ws/
java -jar agent.jar -jnlpUrl http://<jenkins-server>:8080/computer/mac/slave-agent.jnlp -secret e8fdcd526966ee132a7728b08a529b2929a09a52cbdcd13a9243f03f163884d1 -workDir "/Users/admin/jenkins_ws"
1
2
3
4
5
2
3
4
5
- 脚本路径为:
/Users/admin/jenkins_ws/start-slave.sh
- 给脚本添加执行权限
chmod +x start-slave.sh
-> 配置启动项
这里分为两步:
- 启动项配置
- 启动项配置放的位置
-> 启动项配置
先说配置,此处需要一个定制的 xml形式的配置,eg:
<?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>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:</string>
</dict>
<key>Label</key><!--服务命名-->
<string>com.jenkins-service</string>
<key>Program</key><!--启动脚本绝对路径-->
<string>/Users/admin/jenkins_ws/start-slave.sh</string>
<key>RunAtLoad</key> <!--开机启动-->
<true/>
<key>KeepAlive</key><!--是否保活-->
<dict>
<key>SuccessfulExit</key>
<false/>
<key>NetworkState</key>
<true/>
</dict>
<key>ThrottleInterval</key><!--seconds to wait between program invocations-->
<integer>10</integer>
<key>LaunchOnlyOnce</key><!--仅启动一次,会影响保活-->
<true/>
<key>StandardOutPath</key>
<string>/tmp/startup.stdout</string>
<key>StandardErrorPath</key>
<string>/tmp/startup.stderr</string>
<key>UserName</key><!--指定用户启动, 仅root用户启动时生效-->
<string>admin</string>
</dict>
</plist>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
- 配置脚本命名为
com.jenkins-service.plist
-> 启动项位置
配置脚本写好了,放的位置有多个选择
LaunchAgents
,当用户登录到图像界面后启动LaunchDaemons
系统启动后运行
具体的放置路径有:
~/Library/LaunchAgents
, Per-user agents provided by the user./Library/LaunchAgents
, Per-user agents provided by the administrator./Library/LaunchDaemons
, System-wide daemons provided by the administrator./System/Library/LaunchAgents
, Per-user agents provided by macOS./System/Library/LaunchDaemons
, System-wide daemons provided by macOS.
看需求选择,对于本例,我选择的路径为 /Library/LaunchDaemons/com.jenkins-service.plist
-> 启动
# 加载配置并启动,重启会自启动
sudo launchctl load /Library/LaunchDaemons/com.jenkins-service.plist
#查看服务是否启动
sudo launchctl list | grep jenkins
# 停止服务并取消自启动,下次重启不会重新运行
# -w参数标记该服务为 disabled,不加该参数则重启仍然会启动
sudo launchctl unload -w /Library/LaunchDaemons/com.jenkins-service.plist
# 标记该服务为 not disabled并启动
sudo launchctl load -w /Library/LaunchDaemons/com.jenkins-service.plist
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
launchctl start <label>
: Starts the job. This is usually reserved just for testing or debugging a particular job.launchctl stop <label>
: Stops the job. Opposite of start, and it's possible that the job will immediately restart if the job is configured to stay running.launchctl remove <label>
: Removes the job from launchd, but asynchronously. It will not wait for the job to actually stop before returning, so no error handling on this one.launchctl load <path>
: Loads and starts the job as long as the job is not "disabled."launchctl unload <path>
: Stops and unloads the job. The job will still restart on the next login/reboot.launchctl load -w <path>
: Loads and starts the job while also marking the job as "not disabled." The job will restart on the next login/reboot.launchctl unload -w <path>
: Stops and unloads and disables the job. The job will NOT restart on the next login/restart.
提示
如启动异常,可查看系统日志 tail -f /var/log/system.log
-> 参考
编辑此页 (opens new window)
更新于: 2019-12-12