Mac--Launchctl介绍

Launchctl    控制OS X系统里的启动进程(launch) 

在Mac里有一个命令行工具叫做:launchctl,可以用来控制服务的自动启动或者关闭。一般的语法是
sudo launchctl load /path/to/service.plistsudo launchctl unload /path/to/service.plist
一般plist文件放在这j几个地方:

/Library/LaunchDaemons/   由管理员定义的守护进程任务项 
/Library/LaunchAgents/      由管理员为用户定义的任务项 
~/Library/LaunchAgents/     由用户自己定义的任务项 

 /System/Library/LaunchAgents   由Mac OS X为用户定义的任务项

你可以写一个plist文件放到~/Library/Launch Agents/下面,文件里描述你的程序路径和启动参数,那么这个用户登录时就会启动这个程序了,而且是杀不了的哦
被杀了之后会自动重新启动
如果需要把它停止的话,运行一下命令
launchctl unload ~/Library/Launch Agents/com.your company.porduct
如果放到/Library/Launch Agents/下面的话,就是一开机就启动哦~

Launchctl :控制OS X系统里的启动进程(launch) 


执行定时脚本|设置开机启动步骤 
(1)编写执行脚本 
通常brew在安装软件时brew为我们自动生成。 
(2)去对应的目录下建立plist文件 
(3)加载服务

说明:Agents文件夹下的plist是需要用户登录后,才会加载的,而Daemons文件夹下得plist是只要开机,可以不用登录就会被加载

加载/卸载服务 
cd 进入指定 plist 文件 目录 
launchctl load *.plist #加载 
launchctl unload *.plist #取消 
launchctl list #查看服务

launchctl load -w   **.pist #设置开机启动并立即启动改服务

launchctl load **.pist #设置开机启动但不立即启动服务 
2.4 对服务设置别名方便操作 
vim ~/.bash_profile #编辑添加如下脚本 
alias nginx.start=’launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist’ 
alias nginx.stop=’launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist’ 
alias nginx.restart=’nginx.stop && nginx.start’ 
alias php-fpm.start=”launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist” 
alias php-fpm.stop=”launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist” 
alias php-fpm.restart=’php-fpm.stop && php-fpm.start’ 
alias mysql.start=”launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist” 
alias mysql.stop=”launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist” 
alias mysql.restart=’mysql.stop && mysql.start’ 
alias redis.start=”launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.redis.plist” 
alias redis.stop=”launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.redis.plist” 
alias redis.restart=’redis.stop && redis.start’ 
alias memcached.start=”launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist” 
alias memcached.stop=”launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist” 
alias memcached.restart=’memcached.stop && memcached.start’

 

 

plist介绍

Plist的全称是Property lists,是一种用来存储串行化后的对象的文件。属性列表文件的文件扩展名为.plist,因此通常被称为plist文件。Plist文件通常用于储存用户设置,也可以用于存储捆绑的信息。

Plist组织数据到命名值和列表值,主要通过几个主要的Core Foundation类型:CFString, CFNumber, CFBoolean, CFDate, CFData, CFArray, 和 CFDictionary。

Plist结构和内容

Property lists从基本的Core Foundation 类型:CFString,CFNumber,CFBoolean,CFDate,CFData构造。要建立一个复杂的数据结构从这些基本类型,你得把它们放在里面CFDictionary或CFArray里面。为了简化对Property lists的编程,任何属性列表类型也可以被引用通过使用类型CFPropertyListRef。

在一个CFDictionary,数据结构是以键-值对的形式,其中每个键是一个字符串,该键的值可以是一个CFString字符串,一个CFNumber,一个CFBoolean,一个CFDate,一个CFData,一个CFArray,或其他CFDictionary。当使用CFDictionary作为属性列表时,所有的键必须是字符串。

在一个CFArray,数据结构是以一个可以通过索引访问的对象的有序集合。在属性列表中,一个CFArray可以包含任何的基本属性列表类型,以及CFDictionary和其他CFArray的对象。

PROPERTY LIST XML 标签

当属性列表将Core Foundation对象集合转换成一个XML的属性列表,使用文件类型标签<plist>来包含所有的属性列表。下表中列出Core Foundation数据类型常用的其他标记:

Core Foundation数据类型等同的XML

Core Foundation类型

XML标签

CFString

<string>

CFNumber

<real> 或者 <integer>

CFBoolean

<true /> 或者<false />

CFDate

<date>

CFData

<data>

CFArray

<array>

CFDictionary

<dict>

 

posted on 2018-07-24 11:54  Jed_SH  阅读(9542)  评论(1编辑  收藏  举报